logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gary Gregory <garydgreg...@gmail.com>
Subject Re: Re: [log4j2_2.1] How to write different log-messages to different log-files?
Date Mon, 10 Nov 2014 12:41:08 GMT
You can also use Properties to avoid repeating PatternLayout patterns.

Gary

On Mon, Nov 10, 2014 at 7:08 AM, Remko Popma <remko.popma@gmail.com> wrote:

> This would be "standard":
>
> <Configuration status="warn">
>   <Appenders>
>     <File name="debug-appender" fileName="debug.log">
>       <PatternLayout pattern="%d %p %c{1.} [%t] %m%n" />
>     </File>
>     <File name="info-appender" fileName="info.log">
>       <PatternLayout pattern="%d %p %c{1.} [%t] %m%n" />
>     </File>
>   </Appenders>
>     <File name="error-appender" fileName="error.log">
>       <PatternLayout pattern="%d %p %c{1.} [%t] %m%n" />
>     </File>
>   <Loggers>
>     <Root>
>       <AppenderRef ref="debug-appender" level="trace" />
>       <AppenderRef ref="info-appender"  level="info" />
>       <AppenderRef ref="error-appender" level="warn" />
>     </Root>
>   </Loggers>
> </Configuration>
>
> This results in:
>
>    - debug.log - gets trace, debug, info, warn, error, fatal messages
>    - info.log - gets info, warn, error, fatal messages
>    - error.log - gets warn, error, fatal messages
>
> If I understand correctly, you want
>
>    - debug.log - should ONLY get trace and debug, no other level messages
>    - info.log - should ONLY get info level messages (no warn, error, fatal)
>    - error.log - is fine as defined above
>
> To achieve this, you need to apply filtering in addition to the config
> above.
> The filters that Alexey pointed out look good.
>
> You use these filters by placing them inside the configuration for the
> appender that you want to filter.
>
> For example:
>
> <Configuration status="warn">
>   <Appenders>
>     <File name="debug-appender" fileName="debug.log">
>       <PatternLayout pattern="%d %p %c{1.} [%t] %m%n" />
>       <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>
>     </File>
> ...
>
>
> On Mon, Nov 10, 2014 at 7:46 PM, Alexey Zavizionov <
> alexey.zavizionov@gmail.com> wrote:
>
> > 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 <joey.lv@7road.com> 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 <joey.lv@7road.com> 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 <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
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> > > For additional commands, e-mail: log4j-user-help@logging.apache.org
> > >
> >
>



-- 
E-Mail: garydgregory@gmail.com | ggregory@apache.org
Java Persistence with Hibernate, Second Edition
<http://www.manning.com/bauer3/>
JUnit in Action, Second Edition <http://www.manning.com/tahchiev/>
Spring Batch in Action <http://www.manning.com/templier/>
Blog: http://garygregory.wordpress.com
Home: http://garygregory.com/
Tweet! http://twitter.com/GaryGregory

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message