Java Custom log output file (log4j log file output multiple custom log files) _java

Source: Internet
Author: User
Tags first row log4j

log4j output multiple custom log files

If you need to output a separate log file in the actual application, how can you separate the required content from the original log and form a separate log file?

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

Copy Code code as follows:

Log4j.rootlogger=debug, stdout, logfile

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=log/test.log
Log4j.appender.logfile.maxfilesize=128mb
Log4j.appender.logfile.maxbackupindex=3
Log4j.appender.logfile.layout=org.apache.log4j.patternlayout
LOG4J.APPENDER.LOGFILE.LAYOUT.CONVERSIONPATTERN=%D{YYYY-MM-DD HH:mm:ss}%-5p [%t]%c.%m (%l)-%m%n

What 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:

Copy Code code as follows:

private static Log Logger1 = Logfactory.getlog ("Mylogger1");

private static Log Logger2 = Logfactory.getlog ("Mylogger2");


The log4j.properties is configured as follows:

Copy Code code as follows:

Log4j.logger.mylogger1=debug,test1
Log4j.appender.test1=org.apache.log4j.fileappender
Log4j.appender.test1.file=log/test1.log
Log4j.appender.test1.layout=org.apache.log4j.patternlayout
LOG4J.APPENDER.TEST1.LAYOUT.CONVERSIONPATTERN=%D{YYYY-MM-DD HH:mm:ss}%-5p [%t]%c.%m (%l)-%m%n

Log4j.logger.mylogger2=debug,test2
Log4j.appender.test2=org.apache.log4j.fileappender
Log4j.appender.test2.file=log/test2.log
Log4j.appender.test2.layout=org.apache.log4j.patternlayout
LOG4J.APPENDER.TEST2.LAYOUT.CONVERSIONPATTERN=%D{YYYY-MM-DD HH:mm:ss}%-5p [%t]%c.%m (%l)-%m%n

Different logs use a different logger (such as the output to the Test1.log to use Logger1.info ("abc")).

Another problem is that these custom logs are exported to the same log as Log4j.rootlogger, and how can they only be exported to the log they specify? Don't worry, there's a switch here:

Log4j.additivity. Mylogger1 = False

It is used to set whether the output is also in the log configured by Log4j.rootlogger, and false will not be exported elsewhere.

But there is a small flaw in this approach, which is that the class name in the printed log can only be MyLogger or mylogger2.

2 Dynamic configuration Path
If the log path required by the program needs constant change, and it is impossible to change the configuration file every time, you can use the environment variable to solve it.

The log4j configuration is as follows:

Copy Code code as follows:

Log4j.rootlogger=debug,infolog,debuglog

#info Log
Log4j.appender.INFOLOG =org.apache.log4j.dailyrollingfileappender
Log4j.appender.infolog.file= ${log.dir}/${log.info.file}
Log4j.appender.infolog.datepattern=.yyyy-mm-dd
Log4j.appender.infolog.threshold=info
Log4j.appender.infolog.layout=org.apache.log4j.patternlayout
LOG4J.APPENDER.INFOLOG.LAYOUT.CONVERSIONPATTERN=%D{YYYY-MM-DD HH:mm:ss}%-5p [%t]%c.%m (%l)-%m%n

#debug Log
Log4j.appender.DEBUGLOG =org.apache.log4j.rollingfileappender
Log4j.appender.debuglog.file= ${log.dir}/${log.debug.file}
Log4j.appender.debuglog.threshold=debug
Log4j.appender.debuglog.maxfilesize=128mb
Log4j.appender.debuglog.maxbackupindex=3
Log4j.appender.debuglog.layout=org.apache.log4j.patternlayout
LOG4J.APPENDER.DEBUGLOG.LAYOUT.CONVERSIONPATTERN=%D{YYYY-MM-DD HH:mm:ss}%-5p [%t]%c.%m (%l)-%m%n

At this point, you need to use the system to define the environment variables for the output path and file name of the log before you use log to print the log:

Copy Code code as follows:

System.setproperty ("Log.dir", LogDir);

System.setproperty ("Log.info.file", infologfilename);

System.setproperty ("Log.debug.file", debuglogfilename);

Attachment: Format meaning of pattern 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: 2013-8-19-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 break, the Windows platform is "/r/n", and the UNIX platform is "/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.