开发者

Logback to log different messages to two files

开发者 https://www.devze.com 2022-12-23 11:53 出处:网络
I am using logback/slf4j to do my logging. I want to parse my log file to analyze some data, so instead of parsing a great big file (mostly consisting of debug statements) I want to have two logger in

I am using logback/slf4j to do my logging. I want to parse my log file to analyze some data, so instead of parsing a great big file (mostly consisting of debug statements) I want to have two logger instances which each log to a separate file; one for analytics and one for all p开发者_JAVA百科urpose logging. Does anyone know if this is possible with Logback, or any other logger for that matter?


It's very possible to do something like this in logback. Here's an example configuration:

<?xml version="1.0"?>
<configuration>
    <appender name="FILE" class="ch.qos.logback.core.FileAppender">
        <file>logfile.log</file>
        <append>true</append>
        <encoder>
            <pattern>%-4relative [%thread] %-5level %logger{35} - %msg %n</pattern>
        </encoder>
    </appender>
    <appender name="ANALYTICS-FILE" class="ch.qos.logback.core.FileAppender">
        <file>analytics.log</file>
        <append>true</append>
        <encoder>
            <pattern>%-4relative [%thread] %-5level %logger{35} - %msg %n</pattern>
        </encoder>
    </appender>
    <!-- additivity=false ensures analytics data only goes to the analytics log -->
    <logger name="analytics" level="DEBUG" additivity="false">
        <appender-ref ref="ANALYTICS-FILE"/>
    </logger>
    <root>
        <appender-ref ref="FILE"/>
    </root>
</configuration>

Then you'd setup two separate loggers, one for everything and one to log analytics data like so:

Logger analytics = LoggerFactory.getLogger("analytics");


You can have as many loggers as you wish. But, it's better you have one for each package that you need to log differently. Then all the classes in that package and its sub-packages will get the that specific logger. They all can share the root logger and send their log data to root logger appender using additivity="true". Here's an example:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

<property name="pattern" value="%date{HH:mm:ss.SSS} %-5p %logger{36} 
%X{akkaSource} [%file:%line] - %m%n" />

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
        <pattern>%date{HH:mm:ss.SSS} %-5p %logger{36} %X{akkaSource} [%file:%line] - %m%n</pattern>
    </encoder>
</appender>

<appender name="abc" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${catalina.base}/logs/worker.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <fileNamePattern>${catalina.base}/logs/worker-%d{yyyy-MM-dd_HH}.log</fileNamePattern>
        <maxHistory>360</maxHistory>
    </rollingPolicy>
    <encoder>
        <pattern>${pattern}</pattern>
    </encoder>
</appender>

<appender name="xyz" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${catalina.base}/logs/transformer.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <fileNamePattern>${catalina.base}/logs/transformer-%d{yyyy-MM-dd_HH}.log</fileNamePattern>
        <maxHistory>360</maxHistory>
    </rollingPolicy>
    <encoder>
        <pattern>${pattern}</pattern>
    </encoder>
</appender>

<logger name="com.xxx.yyy.zzz" level="INFO" additivity="true">
    <appender-ref ref="xyz"/>
</logger>

<logger name="com.aaa.bbb.ccc" level="INFO" additivity="true">
    <appender-ref ref="abc"/>
</logger>

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


in my case I wanted to leave class names as log name

private static final Logger log = LoggerFactory.getLogger(ScheduledPost.class);

and as I had few such classes, so my logback.xml

<!--additivity=false ensures this log data only goes to the this log, and no one more -->
<logger name="xxx.xxx.xxx.ScheduledPost" level="DEBUG" additivity="false">
    <appender-ref ref="ASYNC_SCHEDULE_LOG_FILE"/>
</logger>
<logger name="xxx.xxx.xxx.GcmPost" level="DEBUG" additivity="false">
    <appender-ref ref="ASYNC_SCHEDULE_LOG_FILE"/>
</logger>
<logger name="xxx.xxx.xxx.PushUtils" level="DEBUG" additivity="false">
    <appender-ref ref="ASYNC_SCHEDULE_LOG_FILE"/>
</logger>
0

精彩评论

暂无评论...
验证码 换一张
取 消