将单独的日志级别记录到log4j2属性文件中的单独文件中 [英] Log separate log levels to separate files in log4j2 properties file

查看:173
本文介绍了将单独的日志级别记录到log4j2属性文件中的单独文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法我们可以为不同的日志级别创建单独的日志文件. 我只想将错误"日志记录到一个文件中,将信息"日志记录到另一个文件中. 我在log4j2.properties中没有找到解决方案.这是我得到的log4j2.xml,它工作正常.谁能帮我在属性文件中写同样的内容.

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. -我不想为此更改任何代码.我正在专门寻找log4j2.properties.

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

预先感谢

推荐答案

ThresholdFilter用于根据日志级别过滤消息.为了获得不同的日志文件,每个追加程序都应具有适当的阈值过滤器.它应该是这样的(带有xml):

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.

您可以找到有关过滤器的更多信息 ThresholdFilter文档

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

这篇关于将单独的日志级别记录到log4j2属性文件中的单独文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆