Log 4j Classification of storage methods by level

Source: Internet
Author: User
Tags log4j

LOG4J is currently the industry standard for log records, and its powerful, well-developed features can meet most of our needs. Provide the perfect log processing method for our application system.

As we know, the log levels in log4j are divided into six levels such as log, DEBUG, INFO, WARN, ERROR, and fatal. Its flexible configuration mechanism is also a major reason why log4j is good enough to use class inheritance to package, class, and root-level configurations. In my study of the process of log4j, once distressed as if log4j can not be classified according to append category, can only have a global, and then affect all the append. One drawback is that mixing all the logs needed is not always convenient when viewing and analyzing.

A few days ago, I downloaded a Jive forum, suddenly found that his log processing system is in accordance with the target, level classification, the different types of logs stored in different places, I am very happy to open the Jive release package for viewing, I am a little disappointed that the jive is using their own log implementation method, No use of log4j. I feel that I have found a way to deal with this problem, and I can modify some of the log4j code myself, always to achieve this goal.

In reference to a long time good, found the filter method, the use of Levelmatchfilter class can be very good to complete the work. Unfortunately, using the filter class, you must use the Log4j.xml configuration, the transition from log4j.properties to XML is not difficult, let's refer to the entire XML configuration file:

<?xml version= "1.0" encoding= "UTF-8"?>

<! DOCTYPE log4j:configuration SYSTEM "LOG4J.DTD" >

<log4j:configuration xmlns:log4j= ' http://jakarta.apache.org/log4j/' >

<appender name= "STDOUT" class= "Org.apache.log4j.ConsoleAppender" >

<layout class= "Org.apache.log4j.PatternLayout" >

<param name= "Conversionpattern" value= "%-4r [%t]%-5p%c%x-%m%n"/>

</layout>

<filter class= "Org.apache.log4j.varia.LevelRangeFilter" >

<param name= "Levelmin" value= "info"/>

<param name= "Levelmax" value= "info"/>

<param name= "Acceptonmatch" value= "true"/>

</filter>

</appender>

<appender name= "File_debug" class= "Org.apache.log4j.RollingFileAppender" >

<param name= "File" value= "Log_debug.log"/>

<layout class= "Org.apache.log4j.PatternLayout" >

<param name= "Conversionpattern" value= "%-4r [%t]%-5p%c%x-%m%n"/>

</layout>

<filter class= "Org.apache.log4j.varia.LevelRangeFilter" >

<param name= "Levelmin" value= "DEBUG"/>

<param name= "Levelmax" value= "DEBUG"/>

<param name= "Acceptonmatch" value= "true"/>

</filter>

</appender>

<appender name= "File_error" class= "Org.apache.log4j.RollingFileAppender" >

<param name= "File" value= "Log_error.log"/>

<layout class= "Org.apache.log4j.PatternLayout" >

<param name= "Conversionpattern" value= "%-4r [%t]%-5p%c%x-%m%n"/>

</layout>

<filter class= "Org.apache.log4j.varia.LevelRangeFilter" >

<param name= "Levelmin" value= "ERROR"/>

<param name= "Levelmax" value= "ERROR"/>

<param name= "Acceptonmatch" value= "true"/>

</filter>

</appender>

<appender name= "File_log" class= "Org.apache.log4j.RollingFileAppender" >

<param name= "File" value= "Log_log.log"/>

<layout class= "Org.apache.log4j.PatternLayout" >

<param name= "Conversionpattern" value= "%-4r [%t]%-5p%c%x-%m%n"/>

</layout>

</appender>

<!--adjustment using those append

<appender-ref ref= "File_log"/>

-

<root>

<level value = "Debug"/>

<appender-ref ref= "STDOUT"/>

<appender-ref ref= "File_error"/>

<appender-ref ref= "File_debug"/>

</root>

</log4j:configuration>

Please note the contents of the Green logo processing:

<filter class= "Org.apache.log4j.varia.LevelRangeFilter" >

<param name= "Levelmin" value= "ERROR"/>

<param name= "Levelmax" value= "ERROR"/>

<param name= "Acceptonmatch" value= "true"/>

</filter>

Here, we use the class Org.apache.log4j.varia.LevelRangeFilter to filter the log level by configuring its levelmin and Levelmax parameters so that we can specify the level of the log to different destinations. We use <appender-ref ref= "File_error"/> and <appender-ref ref= "File_debug"/>

Two files target, the log belonging to the error is stored in the File_error file, the debug log is stored in the File_debug file.

With log filtering, does the storage of different spaces cause performance problems, and I've done a test here, see the code:

Package com.guipei.test;

import org.apache.log4j.*;

/**

* @author Guipei

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.