LOG4J Configuration Practices

Source: Internet
Author: User

When using log4j to print the application log, a colleague asked me if the different level of the same logger log could be output to a different file? This one... I told him. Of course, in our production environment using log4j has always been all logger error above level log input to the error log file, convenient system monitoring.

In fact, this is easy to achieve, we are to add 2 appender,appender in logger to set different min and Max level, as follows:

<logger name= "com ..." additivity= "false" >   <appender-ref ref= "normal"/>   <appender-ref ref= " Error "/></logger>

<pre class= "Prettyprint prettyprinted" style= "margin-top:0px"; margin-bottom:10px; padding:5px; BORDER:1PX solid RGB (214, 214, 214); width:549.765625px; border-radius:0px; font-size:12px; Overflow:auto; Background-color:rgb (238, 238, 238); " ><appender name= "Normal" class= "Org.apache.log4j.FileAppender" >   <param name= "file" value= "$ {log}/normal.log "/>   <param name=" append "value=" true "/>   <layout class=" Org.apache.log4j.PatternLayout ">      <param name=" Conversionpattern "value="%m%n "/>   </layout>   <filter>       <param name= "Levelmax" value= "warn"/ >   </filter></appender><appender name= "error" class= "Org.apache.log4j.FileAppender" >   <param name= "file" value= "${log}/error.log"/>   <param name= "append" value= "true "/>   <layout class=" Org.apache.log4j.PatternLayout ">      <param name= "Conversionpattern" value= "%m%n"/>   </layout>    <filter>       <param name= "levelmin" value= "error"/>   </filter> </appender>

Note: In a Web application test environment that uses log4j, we often combine the log4jconfiglistener of spring to implement dynamic update of log. The following example shows a web-fin/log4j.xml change every 6 seconds.

<context-param>         <param-name>log4jConfigLocation</param-name>         <param-value> web-inf/log4j.xml</param-value>     </context-param>       <context-param>         <param-name >log4jRefreshInterval</param-name>         <param-value>6000</param-value>     </ context-param>       <listener>         <listener-class> Org.springframework.web.util.log4jconfiglistener</listener-class>     </listener>  

LOG4J Configuration Practices

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.