开发者

LOG4J: One appender by message level

开发者 https://www.devze.com 2023-03-23 09:56 出处:网络
I want have X number of log4j appenders, one to messages level in files, and all print in console. But I not get.

I want have X number of log4j appenders, one to messages level in files, and all print in console. But I not get.

My actual log4j.properties:

log4j.rootLogger=ALL, CONSOLA

log4j.appender.CONSOLA=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLA.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLA.layout.ConversionPattern=%-4r %d %-5p %c - %m%n

log4j.appender.WARNLOG=org.apache.log4j.FileAppender
log4j.appender.WARNLOG.threshold=WARN
log4j.appender.WARNLOG.file=XX/web/WEB-INF/serverlogs/warn-server.log
log4j.appender.WARNLOG.layout=org.apache.log4j.PatternLayout
log4j.appender.WARNLOG.layout.ConversionPattern=%-4r %d %-5p %c - %m%n
log4j.appender.WARNLOG.append=true
log4j.appender.WARNLOG.MaxFileSize=250KB
log4j.appender.WARNLOG.MaxBackupIndex=backup

log4j.appender.FATALLOG=org.apache.log4j.FileAppender
log4j.appender.FATALLOG.threshold=FATAL
log4j.appender.FATALLOG.file=XX/web/WEB-INF/serverlogs/fatal-server.log
log4j.appender.FATALLOG.layout=org.apache.log4j.PatternLayout
log4j.appender.FATALLOG.layout.ConversionPattern=%-4r %d %-5p %c - %m%n
log4j.appender.FATALLOG.append=true
log4j.appender.FATALLOG.MaxFileSize=250KB
log4j.appender.FATALLOG.MaxBackupIndex=backup

log4j.logger.com.XX=WARN, WARNLOG
log4j.logger.com.XX=FATAL, FATALLOG

SOLVED:

I use LevelRangeFilter

<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>

    <开发者_StackOverflow;appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%-4r %d %-5p %c - %m%n"/>
        </layout>
    </appender>

    <appender name="FATALLOG" class="org.apache.log4j.FileAppender">
        <param name="File" value="xx/web/WEB-INF/serverlogs/fatal-server.log"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%-4r %d %-5p %c - %m%n"/>
        </layout>
        <filter class="org.apache.log4j.varia.LevelRangeFilter">
            <param name="LevelMax" value="FATAL"/>
            <param name="LevelMin" value="FATAL"/>
            <param name="AcceptOnMatch" value="true"/>
        </filter>
    </appender>

    <appender name="WARNLOG" class="org.apache.log4j.FileAppender">
        <param name="File" value="xx/web/WEB-INF/serverlogs/warn-server.log"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%-4r %d %-5p %c - %m%n"/>
        </layout>
        <filter class="org.apache.log4j.varia.LevelRangeFilter">
            <param name="LevelMax" value="WARN"/>
            <param name="LevelMin" value="WARN"/>
            <param name="AcceptOnMatch" value="true"/>
        </filter>
    </appender>

    <logger name="com.xx">
        <appender-ref ref="CONSOLE" /> 
        <appender-ref ref="FATALLOG" /> 
        <appender-ref ref="WARNLOG" /> 
    </logger>

</log4j:configuration>


You must have different loggers or use the LevelMatchFilter.

For more information you can search into the complete Log4j manual.

You can found it here or here


Use a LevelMatchFilter or adapt it.

0

精彩评论

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