my question is I want to write the different-level logs to different files.

for example,  here is java code:

logger.trace("trace log");
logger.debug("debug log");
logger.info("info log");
logger.warn("warn log");
logger.error("error log");
logger.fatal("fatal log");

I want to the log messages "trace log" and "debug log" are written into 
debug.log file, the "info log" is written into info.log file, and the "warn 
log"/"error log"/"fatal log" are written into error.log file.







From: Remko Popma
Date: 2014-11-10 17:47
To: Log4J Users List
Subject: Re: [log4j2_2.1] How to write different log-messages to different 
log-files?
Really? The stackoverflow link is not an exact match, but you should be able to 
apply the same technique to achieve what you want, if I understand your 
question correctly. 

Sent from my iPhone

> On 2014/11/10, at 18:41, joey.lv <[email protected]> wrote:
> 
> Hi Remko, 
> 
> Thanks for your reply, but I don't think it is what I wanted.
> 
> 
> 
> 
> 
> 
> 
> From: Remko Popma
> Date: 2014-11-10 17:22
> To: Log4J Users List
> Subject: Re: [log4j2_2.1] How to write different log-messages to different 
> log-files?
> 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 <[email protected]> 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: [email protected]
> For additional commands, e-mail: [email protected]

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to