简体   繁体   中英

How to separately logging JBoss server specific logs with JBoss Logging and application specific logs with log4j?

We're working on a migration of our application from WebSphere to JBoss EAP 6.4. I read on the Internet that since JBoss EAP 6.x, JBoss use its own logging framework called JBoss Logging instead of log4j previously.

We have already a log4j.properties file for application specific logs, which store logs in separate files according the log level (log-error.log, log-info.log, ...), but actually, they are also logged in the console and in the server.log file.

In the one hand , we would like application logs to be logged only with our existing log4j.properties configuration file, but also prevent them to be logged in server.log and console too.

In the other hand , we would like server specific logs to be logged with JBoss Logging framework, so in the server.log and in the console.

To sum up, we want to go from :

application logs + server logs => server.log/console + separate file logs

To this :

application logs => seperate file logs
server logs => server.log/console

Does somebody know how to achieve this ? Does anyone have already configured a JBoss server in a similar way ?

Thanks you,

Regards.

Don't understand why but log4j also logs in stdout so JBoss Logging catch these logs and appends them again to JBoss console and server.log like this :

2017-08-08 15:14:20,304 INFO [stdout] <<My log4j log message>> .

I succeeded for the moment to prevent this by adding that code to standalone.xml file :

<logger category="stdout" use-parent-handlers="false">
    <level name="OFF"/>
</logger>

Isn't this a bad practise ?

Thanks,

If you include a log4j.proeprties file in the WAR/WEB-INF/classes or EAR/META-INF it should just work. If you're seeing application specific logging in all places then my guess would be you have a console appender configured in your log4j.properties file.

I have spent a few days figuring this out, and I think I have something that works. I am using EAP 6.4.

First, the solution mentioned above about adding code to standalone.xml makes the output from my solution described below more easy to read in the console. If you don't do that step, you get some extra line feeds in the console.

What I did is create a log4j.xml file, but I did NOT call it log4j.xml . For my 2 applications, I called it servicelog4j.xml . This means that JBoss will not find it.

Then when I am creating the log4j logger, I do the following (this is in a static method for my class called Log4jLogger ):

public static Logger getLogger(Class aClass) {
    ClassLoader classLoader = Log4jLogger.class.getClassLoader();
    InputStream log4JStream = classLoader.getResourceAsStream("servicelog4j.xml");
    if (log4JStream != null) {
        new DOMConfigurator().doConfigure(log4JStream, LogManager.getLoggerRepository());
    }

    Logger log = Logger.getLogger(aClass);
    return log;
}

Now, the file servicelog4j.xml is in the resources folder of my Eclipse project, so that when the application is deployed, it will get copied to WEB-INF/classes/servicelog4j.xml .

The contents of that file are a standard log4j file. I have a standard file appender and a console appender. The file appenders for each application point to different files.

To have debugging code for just my classes appear in the console (and in the file), I place the following in servicelog4j.xml (since all my code is in the package com.mycompany.mypackage ):

  <logger name="com.mycompany.mypackage">
      <level value="debug" />
      <appender-ref ref="CONSOLE" />
  </logger>
  <root>
      <priority value ="info" />
      <appender-ref ref="FILE" />
  </root>

With the above configuration, I get all messages in the Eclipse console as well as the file. If I just remove the <logger> section, then the debug messages go nowhere, which is what I'd want in production.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM