Log separate log levels to separate files in log4j

2019-09-13 10:08发布

Is there any way We can create separate log files for different log levels. All I want is to log "error" logs to one file and "info" logs to another file. I did not find any solution to do this in log4j2.properties. Here is the log4j2.xml which I got and it works fine. Can anyone help me writing the same in properties file.

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Properties>
<Property name="log-path">logs</Property>
</Properties>
<Appenders>
<Console name="console-log" target="SYSTEM_OUT">
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"/>
</Console>
<RollingFile name="trace-log" fileName="${log-path}/mycuteblog-trace.log"
filePattern="${log-path}/mycuteblog-trace-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
</RollingFile>
<RollingFile name="error-log" fileName="${log-path}/mycuteblog-error.log"
filePattern="${log-path}/mycuteblog-error-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="com.mycuteblog.log4j2" level="debug" additivity="false">
<appender-ref ref="trace-log" level="debug"/>
<appender-ref ref="error-log" level="error"/>
<appender-ref ref="console-log" level="debug"/>
</Logger>
<Root level="info" additivity="false">
<AppenderRef ref="console-log"/>
</Root>
</Loggers>
</Configuration>

P.S. - I do not want to make any code change for this. I am looking for specifically log4j2.properties.

Thanks in Advance

1条回答
祖国的老花朵
2楼-- · 2019-09-13 10:47

The ThresholdFilter serves for filtering messages according to the log level. To get the different log files each appender should have the appropriate threshold filter. It should be something like this (with xml):

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Properties>
        <Property name="log-path">logs</Property>
        <Property name="log-pattern">[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1}- %msg%n"/</Property>
    </Properties>
    <Appenders>
        <Console name="console-log" target="SYSTEM_OUT">
            <PatternLayout>
                <pattern>${log-pattern}</pattern>
            </PatternLayout>
        </Console>
        <RollingFile name="trace-log" fileName="${log-path}/mycuteblog-trace.log" filePattern="${log-path}/mycuteblog-trace-%d{yyyy-MM-dd}.log">
            <ThresholdFilter level="ERROR" onMatch="DENY" onMismatch="ACCEPT"/>
            <PatternLayout>
                <pattern>${log-pattern}</pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
            </Policies>
        </RollingFile>
        <RollingFile name="error-log" fileName="${log-path}/mycuteblog-error.log" filePattern="${log-path}/mycuteblog-error-%d{yyyy-MM-dd}.log">
            <ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
            <PatternLayout>
                <pattern>${log-pattern}</pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
            </Policies>
        </RollingFile>
    </Appenders>
    ...

Pay attention, that the log pattern is defined as a property, since the same pattern is used for all three appenders.

I cannot help with configuration as a properties file, I never used it.

You can find more about filters here and on the ThresholdFilter documentation

查看更多
登录 后发表回答