[Reprint]log4j output multiple custom log files, dynamic configuration path

Source: Internet
Author: User
Tags first row custom name log4j

Http://blog.csdn.net/wiwipetter/archive/2009/07/30/4390579.aspx

1. log4j output multiple custom log files
The powerful function of log4j is unquestionable, but in practical application it is unavoidable to have a function to output independent log files, how to separate the required content from the original log, and form a separate log file. In fact, as long as the existing log4j based on a little configuration can easily achieve this function.

First look at a common log4j.properties file, which logs log in console and Myweb.log files:

Log4j.rootlogger=debug, stdout, logfile

Log4j.category.org.springframework=error
Log4j.category.org.apache=info

Log4j.appender.stdout=org.apache.log4j.consoleappender
Log4j.appender.stdout.layout=org.apache.log4j.patternlayout
log4j.appender.stdout.layout.conversionpattern=%d%p [%c]-%m%n

Log4j.appender.logfile=org.apache.log4j.rollingfileappender
Log4j.appender.logfile.file=${myweb.root}/web-inf/log/myweb.log
log4j.appender.logfile.maxfilesize=512kb
Log4j.appender.logfile.maxbackupindex=5
Log4j.appender.logfile.layout=org.apache.log4j.patternlayout
log4j.appender.logfile.layout.conversionpattern=%d%p [%c]-%m%n

If you want to output different files for different classes (take cn.com.Test as an example), first define them in Test.java:
private static Log logger = Logfactory.getlog (Test.class);
Then add in the Log4j.properties:
Log4j.logger.cn.com.test= DEBUG, Test
Log4j.appender.test=org.apache.log4j.fileappender
Log4j.appender.test.file=${myweb.root}/web-inf/log/test.log
Log4j.appender.test.layout=org.apache.log4j.patternlayout
log4j.appender.test.layout.conversionpattern=%d%p [%c]-%m%n
That is, let the logger in cn.com.Test use the configuration made by Log4j.appender.test.

However, if you need to output multiple log files in the same class. In fact, the truth is the same, first in the Test.java definition:
private static Log Logger1 = Logfactory.getlog ("MyTest1");
private static Log Logger2 = Logfactory.getlog ("MyTest2");
Then add in the Log4j.properties:
log4j.logger.mytest1= DEBUG, Test1
Log4j.appender.test1=org.apache.log4j.fileappender
Log4j.appender.test1.file=${myweb.root}/web-inf/log/test1.log
Log4j.appender.test1.layout=org.apache.log4j.patternlayout
log4j.appender.test1.layout.conversionpattern=%d%p [%c]-%m%n

Log4j.logger.mytest2= DEBUG, Test2
Log4j.appender.test2=org.apache.log4j.fileappender
Log4j.appender.test2.file=${myweb.root}/web-inf/log/test2.log
Log4j.appender.test2.layout=org.apache.log4j.patternlayout
log4j.appender.test2.layout.conversionpattern=%d%p [%c]-%m%n
That is, give it a custom name (such as "myTest1" here) when using logger, and then make a corresponding configuration in log4j.properties. Don't forget to use different logger for different logs (such as Logger1.info ("abc") for output to Test1.log.

Another problem is that these custom logs are output to the log that Log4j.rootlogger is configured by default, and how they can only be exported to their own specified log. Don't worry, there's a switch here:
Log4j.additivity.myTest1 = False
It is used to set whether the output is also in the log configured by Log4j.rootlogger, and false is not exported to other places. Note that the "MyTest1" here is the custom name you gave logger in the program.
If you say, I just do not want to output this log at the same time to Log4j.rootlogger configured logfile, stdout I also want to output at the same time. It's good to do, put your log4j.logger.myTest1 = DEBUG, test1 changed to the next type on OK.
Log4j.logger.mytest1=debug, Test1, stdout


2. Dynamic Configuration Path

If the log path required by the program needs constant change, and it is not possible to change the configuration file every time, then two methods should be adopted.

First Kind

LOG4J's configuration file supports Windows environment variables, which are similar in format to velocity:${env}, we use environment variables to represent paths that can change. "Log4j.appender.test1.file=${myweb.root}/web-inf/log/test1.log" has been used above.

Second Kind

This method is not a configuration file, but in the program with code configuration, the code is alive, so the path can certainly write live. Examples are as follows:


View Plaincopy to Clipboardprint?
Logger myTest = Logger.getlogger ("MyTest");

Layout Layout = new Patternlayout ("%d%p [%c]-%m%n");

Appender Appender = new Fileappender (layout, logfilepath);

Mytest.addappender (Appender);
Logger myTest = Logger.getlogger ("MyTest"); Layout Layout = new Patternlayout ("%d%p [%c]-%m%n"); Appender Appender = new Fileappender (layout, logfilepath); Mytest.addappender (Appender);


Attached: The format meaning of the Conversionpattern parameter


The full name of the class to which the%c output log information belongs
%d output log point-in-time date or time, the default format is ISO8601, you can also specify the format after, such as:%d{yyy-mm-dd HH:mm:ss}, output similar: 2002-10-18-22:10:28
Class name of the class to which the%f output log information belongs
%l the location where the output log event occurs, where the statement of the output log information is in the first row of the class in which it resides
%m the information specified in the output code, such as the message in log
%n output a carriage return line feed, Windows platform "\ r \ n", UNIX platform "\ n"
%p output priority, that is, debug,info,warn,error,fatal. If you are calling debug () output, debug, and so on
%r output the number of milliseconds it takes to boot from the application to output the log information
%t output The name of the thread that generated the log event

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.