Alok Sharma
Alok Sharma

Reputation: 31

logback to log4j2 async logging migration

I need to choose a logging framework to replace log4j. I chose slf4j + logback for logging initially and wrote the following config which outputs application log and apache cxf's logs in separate files and outputs spring/hibernate logs on console:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
        <Pattern>.%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg %n
        </Pattern>
    </encoder>
    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
        <level>TRACE</level>
    </filter>
</appender>

<appender name="dailyRollingFileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <FileNamePattern>D:\\MYDIR\\logs\\app_logback.%d{yyyy-MM-dd}.log</FileNamePattern>
        <maxHistory>30</maxHistory>         
    </rollingPolicy>

    <encoder>
        <Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{35} - %msg %n</Pattern>
    </encoder>      
</appender>

<appender name="minuteRollingFileAppenderCxf" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <FileNamePattern>D:\\MYDIR\\logs\\app_logback_cxf.%d{yyyy-MM-dd_HH-mm}.log</FileNamePattern>
        <maxHistory>30</maxHistory>         
    </rollingPolicy>

    <encoder>
        <Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{35} - %msg %n</Pattern>
    </encoder>      
</appender>

<logger name="com.mycustomcode" additivity="false">
    <level value="DEBUG" />
    <appender-ref ref="dailyRollingFileAppender"/>
</logger>

<logger name="org.apache" additivity="false">
    <level value="INFO" />
    <appender-ref ref="minuteRollingFileAppenderCxf"/>
</logger>

<root>
    <level value="INFO" />
    <appender-ref ref="consoleAppender" />
</root>
</configuration>

However, i came to know about async logging in log4j2 and I want to implement async logging using log4j2 in my setup for which I wrote the following config:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console">
  <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>

<RandomAccessFile name="RandomAccessFile" fileName="D:\\MYDIR\\logs\\app_log4j2.log" immediateFlush="false" append="false">
  <PatternLayout>
    <Pattern>%d %p %c{1.} [%t] %m %ex%n</Pattern>
  </PatternLayout>
</RandomAccessFile>

<RandomAccessFile name="RandomAccessFile2" fileName="D:\\MYDIR\\logs\\app_log4j2_cxf.log" immediateFlush="false" append="false">
  <PatternLayout>
    <Pattern>%d %p %c{1.} [%t] %m %ex%n</Pattern>
  </PatternLayout>
</RandomAccessFile>
</Appenders>

<Loggers>
<AsyncLogger name="com.mycustomcode" level="debug" includeLocation="true" additivity="false">
  <AppenderRef ref="RandomAccessFile"/>
</AsyncLogger>

<AsyncLogger name="org.apache" level="debug" includeLocation="true" additivity="false">
  <AppenderRef ref="RandomAccessFile2"/>
</AsyncLogger>

<Root level="debug" includeLocation="false">
  <AppenderRef ref="Console"/>
</Root>
</Loggers>

</Configuration>

the log4j2 config works, but it is only printing the logs for "com.mycustomcode" properly in its file (app_log4j2.log). Nothing gets printed for "org.apache" in the file app_log4j2_cxf.log - in fact, it is blank and its log gets printed to the server console.

Can somebody please point out what am I doing wrong? Thanks

Upvotes: 1

Views: 7010

Answers (2)

Remko Popma
Remko Popma

Reputation: 36754

Does your code that uses this configuration uses a Logger with a name that starts with "org.apache"? If not, you would not see any output for this logger.

To verify this, use the above configuration with this code:

Logger logger = LogManager.getLogger("org.apache.anything");
logger.debug("test");

Upvotes: 0

user5121658
user5121658

Reputation: 11

logback also has async support. Check AsyncAppender

http://logback.qos.ch/manual/appenders.html

Upvotes: 1

Related Questions