将单独的日志级别记录到 log4j2 属性文件中的单独文件

2024-01-11

有什么办法可以为不同的日志级别创建单独的日志文件吗?

我想要的只是记录error记录到一个文件并info记录到另一个文件。

我没有找到任何解决方案来做到这一点log4j2.properties。这里是log4j2.xml我得到了并且效果很好。谁能帮我在属性文件中写入相同的内容?

该 XML 文件使用Log4j2 FAQ 中的方法和集合level在 AppenderRef 上 https://logging.apache.org/log4j/log4j-2.4/faq.html#config_sep_appender_level:

<?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>

附: - 我不想为此更改任何代码。我正在专门寻找log4j2.properties.

提前致谢。


尝试 LevelRangeFilter

Try LevelRangeFilter https://howtodoinjava.com/log4j2/levelrangefilter-example/:如果您使用 log4j2 进行日志记录,请将此行包含在您的 Appender 配置中

<LevelRangeFilter minLevel="ERROR" maxLevel="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>

上面的行将错误日志与其他日志分开

如果将 minLevel 和 maxLevel 指定为 INFO,则仅考虑 INFO 日志,而不考虑以上级别的日志。

<LevelRangeFilter minLevel="INFO" maxLevel="INFO" onMatch="ACCEPT" onMismatch="DENY"/>

下面log4j.xml文件对我有用:

<?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">
            <LevelRangeFilter minLevel="TRACE" maxLevel="TRACE" onMatch="ACCEPT" onMismatch="DENY"/>
            <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="info-log"
                     fileName="${log-path}/mycuteblog-info.log"
                     filePattern="${log-path}/mycuteblog-info-%d{yyyy-MM-dd}.log">
            <LevelRangeFilter minLevel="INFO" maxLevel="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
            <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">
            <LevelRangeFilter minLevel="ERROR" maxLevel="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
            <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>
        <Root level="info" additivity="false">
            <AppenderRef ref="console-log"/>
            <AppenderRef ref="trace-log"/>
            <AppenderRef ref="error-log"/>
            <AppenderRef ref="info-log"/>
        </Root>
    </Loggers>
</Configuration>

如需更多参考,请点击此链接:https://howtodoinjava.com/log4j2/multiple-appenders/ https://howtodoinjava.com/log4j2/multiple-appenders/

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将单独的日志级别记录到 log4j2 属性文件中的单独文件 的相关文章

随机推荐