logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Remko Popma <remko.po...@gmail.com>
Subject Re: [log4j2_2.1] How to write different log-messages to different log-files?
Date Mon, 10 Nov 2014 09:22:31 GMT
The way to do this is to send all events to your error.log (appender-ref level="trace"),
then in your appender declaration you add filters that accept warn, error and fatal messages,
and deny info, debug and trace messages. 

An example is here:
 http://stackoverflow.com/questions/24695133/log4j2-filter-particular-level-in-apender


Sent from my iPhone

> On 2014/11/10, at 16:16, joey.lv <joey.lv@7road.com> wrote:
> 
> 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?

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Mime
View raw message