logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From joey.lv <joey...@7road.com>
Subject [log4j2_2.1] How to write different log-messages to different log-files?
Date Mon, 10 Nov 2014 07:16:40 GMT
I want to write the 'trace' and 'debug' messages to debug.log, 'info' messages to info.log,
and 'warn','error','fafal' messages to error.log

Here is a sample java code,
package com.r7oad.udsp.common.cache.test;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Log4j2Test {
    private static final Logger logger = LogManager.getLogger(Log4j2Test.class);
    public static void main(String[] args){
        logger.trace("Hello world - trace log");
        logger.debug("Hello world - debug log");
        logger.info("Hello world - info log");
        logger.warn("Hello world - warn log");
        logger.error("Hello world - error log");
        logger.fatal("Hello world - fatal log");
    }
}


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

<configuration debug="off" monitorInterval="1800">
    <Properties>
        <Property name="log-path">d://logs</Property>
    </Properties>

    <Appenders>
        <Console name="STDOUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36}.%M()/%L  - %msg%xEx%n"/>
        </Console>


        <File name="app_debug" fileName="${log-path}/app/debug.log" append="false">
            <PatternLayout pattern="%d{yyyy.MM.dd HH:mm:ss z} %-5level %class{36}.%M()/%L
- %msg%xEx%n"/>
        </File>
        <File name="app_info" fileName="${log-path}/app/info.log" append="false">
            <PatternLayout pattern="%d{yyyy.MM.dd HH:mm:ss z} %-5level %class{36}.%M()/%L
- %msg%xEx%n"/>
        </File>
        <File name="app_error" fileName="${log-path}/app/error.log" append="false">
            <PatternLayout pattern="%d{yyyy.MM.dd HH:mm:ss z} %-5level %class{36}.%M()/%L
- %msg%xEx%n"/>
        </File>
    </Appenders>
    <Loggers>
        <Logger name="com.r7oad.udsp" level="trace"  additivity="false">
            <appender-ref ref="STDOUT"/>
            <appender-ref ref="app_debug">
                <ThresholdFilter level="INFO"  onMatch="DENY"  onMismatch="NEUTRAL"/>
                <ThresholdFilter level="TRACE" onMatch="ACCEPT"  onMismatch="DENY"/>
            </appender-ref>
            <appender-ref ref="app_info">
                <ThresholdFilter level="WARN"  onMatch="DENY"  onMismatch="NEUTRAL"/>
                <ThresholdFilter level="INFO" onMatch="ACCEPT"  onMismatch="DENY"/>
            </appender-ref>
            <appender-ref ref="app_error">
                <thresholdFilter level="WARN" onMatch="ACCEPT" onMismatch="DENY"/>
            </appender-ref>
        </Logger>
    </Loggers>
</configuration>


messages in debug.log file. (correct)
2014.11.10 15:08:13 CST TRACE com.r7oad.udsp.common.cache.test.Log4j2Test.main()/9 - Hello
world - trace log
2014.11.10 15:08:13 CST DEBUG com.r7oad.udsp.common.cache.test.Log4j2Test.main()/10 - Hello
world - debug log



messages in info.log file. ( not correct)
2014.11.10 15:08:13 CST TRACE com.r7oad.udsp.common.cache.test.Log4j2Test.main()/9 - Hello
world - trace log
2014.11.10 15:08:13 CST DEBUG com.r7oad.udsp.common.cache.test.Log4j2Test.main()/10 - Hello
world - debug log
2014.11.10 15:08:13 CST INFO  com.r7oad.udsp.common.cache.test.Log4j2Test.main()/11 - Hello
world - info log


messages in error.log file.  (correct)
2014.11.10 15:08:13 CST WARN  com.r7oad.udsp.common.cache.test.Log4j2Test.main()/12 - Hello
world - warn log
2014.11.10 15:08:13 CST ERROR com.r7oad.udsp.common.cache.test.Log4j2Test.main()/13 - Hello
world - error log
2014.11.10 15:08:13 CST FATAL com.r7oad.udsp.common.cache.test.Log4j2Test.main()/14 - Hello
world - fatal log


so, how to config the <ThresholdFilter/> to achieve my purpose?
Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message