Hi Alexey,
Thanks for your reply, I noticed the element <Filters/>, and I re-tested my
code, here is the final log42j.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="Console" target="SYSTEM_OUT">
<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">
<Filters>
<ThresholdFilter level="fatal" onMatch="DENY"
onMismatch="NEUTRAL"/>
<ThresholdFilter level="error" onMatch="DENY"
onMismatch="NEUTRAL"/>
<ThresholdFilter level="warn" onMatch="DENY"
onMismatch="NEUTRAL"/>
<ThresholdFilter level="info" onMatch="DENY"
onMismatch="NEUTRAL"/>
<ThresholdFilter level="debug" onMatch="ACCEPT"
onMismatch="NEUTRAL"/>
<!--
<ThresholdFilter level="trace" onMatch="ACCEPT"
onMismatch="DENY"/>
-->
</Filters>
<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">
<Filters>
<ThresholdFilter level="fatal" onMatch="DENY"
onMismatch="NEUTRAL"/>
<ThresholdFilter level="error" onMatch="DENY"
onMismatch="NEUTRAL"/>
<ThresholdFilter level="warn" onMatch="DENY"
onMismatch="NEUTRAL"/>
<ThresholdFilter level="info" onMatch="ACCEPT"
onMismatch="DENY"/>
</Filters>
<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">
<Filters>
<ThresholdFilter level="warn" onMatch="ACCEPT"
onMismatch="DENY"/>
</Filters>
<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="Console"/>
<appender-ref ref="app_debug"/>
<appender-ref ref="app_info"/>
<appender-ref ref="app_error"/>
</Logger>
</Loggers>
</configuration>
From: Alexey Zavizionov
Date: 2014-11-10 18:46
To: Log4J Users List; joey.lv
Subject: Re: Re: [log4j2_2.1] How to write different log-messages to different
log-files?
seems it would be like this:
trace and debug to debug.log file:
<Filters>
<ThresholdFilter level="info" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="warn" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="fatal" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
"info log" is written into info.log file:
<Filters>
<ThresholdFilter level="warn" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="fatal" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
and the "warn log"/"error log"/"fatal log" are written into error.log file
<Filters>
<ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
On Mon, Nov 10, 2014 at 12:07 PM, joey.lv <[email protected]> wrote:
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]