log4j configuration output to multiple log files

Source: Internet
Author: User
Tags log log system log log4j

In our project, there are some important logs to separate output to the specified file, rather than the total output to the system log file, then we log4j for us to provide such a function, below we have to step by step to see how to do. This is written in the configuration of the property, XML is similar, you want to know, you can see the official document.

The project package structure tested here is as follows:

Log4j mainly consists of three parts: loggers, appenders and layouts, (Appender can be understood as the destination of output)

We can configure multiple logger in our log4j.properties or log4j.xml.

Each logger can correspond to multiple appender, and appender and layout are one by one corresponding.

The simplest configuration below, we configure the system Rootlogger, as follows:

### rootlogger config
log4j.rootlogger=debug,a_console
log4j.appender.a_console= Org.apache.log4j.ConsoleAppender
log4j.appender.a_console.layout=org.apache.log4j.patternlayout
log4j.appender.a_console.layout.conversionpattern=%d [%t]%-5p%c-%m%n
Among them: 1, log4j.rootlogger This configuration three mode is: Log4j.rootlogger=level,appender1,appender2,... level for log grade, relationship such as: DEBUG < INFO < WARN < ERROR < FATAL, the smaller the level can output than their own large log, such as set to debug, you can output warn,error, and so on the log. And immediately following is the Appender list: Appender1,appender2,... 2, Log4j.appender.a_console=org.apache.log4j.consoleappender specifies the corresponding Appender class, which is output to the console. 3, Log4j.appender.a_console.layout=org.apache.log4j.patternlayout, designated layout class, 4, Log4j.appender.A_ console.layout.conversionpattern=%d [%t]%-5p%c-%m%n output format, specifically how to use can query patternlayout this API. We use a main method to get out and look at the log printing effect
package com.pt.test; import java.io.InputStream; import Org.apache.log4j.Logger; import

Org.apache.log4j.PropertyConfigurator;

Import Com.pt.Tool;
	/** * @author Mzy * */public class App {static Logger Logger = Logger.getlogger (App.class);
		/** * @param args */public static void main (string[] args) {//basicconfigurator.configure ();
		InputStream in = App.class.getClassLoader (). getResourceAsStream ("log4j.properties");
		Propertyconfigurator.configure (in);
		Domconfigurator ==>log4j.xml Logger.debug ("This is Main.1.");
		Logger.warn ("Testing warn ...");
		Tool t = new Tool ();
	T.dowork (); 
   } class Tool {static Logger Logger = Logger.getlogger (Tool.class);
   public void DoWork () {Logger.debug (' This is tool Run method ... '); }
}
2014-07-12 09:51:19,554 [main] DEBUG com.pt.test.App-this is  main.1.
2014-07-12 09:51:19,555 [main] WARN  com.pt.test.App-  testing WARN ...
2014-07-12 09:51:19,556 [main] DEBUG Com.pt.Tool-This is  Tool run method.
to configure multiple Appener   Each logger can correspond to multiple Appender, configured as follows:
### rootlogger config
log4j.rootlogger=debug,a_console,a_file
# A appender is  a_console
Log4j.appender.a_console=org.apache.log4j.consoleappender
log4j.appender.a_console.layout= Org.apache.log4j.PatternLayout
log4j.appender.a_console.layout.conversionpattern=%d [%t]%-5p%c-%m%n

# # Second Appender is a_file
log4j.appender.a_file=org.apache.log4j.fileappender
log4j.appender.a_file.file= D:/test/mylog.log
log4j.appender.a_file.layout=org.apache.log4j.patternlayout
log4j.appender.A_ file.layout.conversionpattern=%d [%t]%-5p%c-%m%n
### End Rootlogger Config
In this way, when we travel the app, the log will run out to the console and the MyLog.log log file, which is no longer posted. Configuring multiple Logger Scenarios  When we need to extract some important logs, or the system of more independent modules, such as specialized with the external System docking interface, then we can configure a number of logger, and the implementation of the log separation, to facilitate our day-to-day maintenance work. We will add the following on the original basis of two additional logger, respectively: "Com.pt.test" and "com.pt.test.intf", naming is defined by the package name, to see the detailed configuration bar:
### rootlogger Config Log4j.rootlogger=debug,a_console,a_file # a appender is A_console log4j.appender.a_console= Org.apache.log4j.ConsoleAppender Log4j.appender.a_console.layout=org.apache.log4j.patternlayout LOG4J.APPENDER.A _console.layout.conversionpattern=%d [%t]%-5p%c-%m%n # # Second appender is A_file Log4j.appender.a_file=org.apache.lo G4j. Fileappender Log4j.appender.a_file.file=d:/test/logfile1.log log4j.appender.a_file.layout= Org.apache.log4j.PatternLayout log4j.appender.a_file.layout.conversionpattern=%d [%t]%-5p%c-%m%n ### End Rootlogger
Config ### logger [com.pt.test] config log4j.logger.com.pt.test=debug,a_file2 #log4j. additivity.com.pt.test=false
Log4j.appender.a_file2=org.apache.log4j.fileappender Log4j.appender.a_file2.file=d:/test/logfile2.log Log4j.appender.a_file2.layout=org.apache.log4j.patternlayout log4j.appender.a_file2.layout.conversionpattern=%d [%t]%-5p%c-%m%n ### logger [com.pt.test] config ### logger [com.pt.test.intf] config log4j.logger.cOm.pt.test.intf=debug,a_file3 Log4j.appender.a_file3=org.apache.log4j.fileappender Log4j.appender.A_file3.file=D :/test/logfile3.log log4j.appender.a_file3.layout=org.apache.log4j.patternlayout Log4j.appender.A_

 file3.layout.conversionpattern=%d [%t]%-5p%c-%m%n ### End Servicelogger Config
We note that the newly added two logger are configured as: Log4j.logger. Com.pt.test=debug,a_file2 log output to: D:/test/logfile2.log Log4j.logger. Com.pt.test.intf=debug,a_file3 log output to: D:/test/logfile3.log Let's look at the log output of three files: Logfile1.log
2014-07-12 11:22:36,284 [main] DEBUG com.pt.test.App-this is  main.1.
2014-07-12 11:22:36,285 [main] WARN  com.pt.test.App-  testing WARN ...
2014-07-12 11:22:36,286 [main] DEBUG Com.pt.Tool-This is  Tool Run method.
2014-07-12 11:22:36,287 [main] DEBUG Com.pt.test.intf.ParseService-this  parseservice log ...
Logfile2.log
2014-07-12 11:22:36,284 [main] DEBUG com.pt.test.App-this is  main.1.
2014-07-12 11:22:36,285 [main] WARN  com.pt.test.App-  testing WARN ...
2014-07-12 11:22:36,287 [main] DEBUG Com.pt.test.intf.ParseService-this  parseservice log ...
Logfile3.log
2014-07-12 11:22:36,287 [main] DEBUG Com.pt.test.intf.ParseService-this  parseservice log ...
At this time we will find that Filelog1,filelog2,filelog3 have printed the contents of the log, attentive friends should find that filelog1 contains all the contents of FILELOG2, by default, The logger log will be output to all parent Appender, and if we do not want this output then we can set the corresponding Looger additivity property to False.  such as: the 17th line of comments open log4j.additivity.com.pt.test=false so that Rootlogger will no longer have logger "com.pt.test" and its child bag in the log. Two modes of scrolling generated log files: 1, using Rollingfileappender, which generates log files based on size, configures MaxFileSize to qualify each file size, and when the specified size is exceeded, the system automatically generates the next file. Appender as shown below:
Log4j.appender.a_roll=org.apache.log4j.rollingfileappender
Log4j.appender.a_roll.file=d:/test/rollfile.log
log4j.appender.a_roll.maxfilesize=1kb
Log4j.appender.a_roll.layout=org.apache.log4j.patternlayout
log4j.appender.a_roll.layout.conversionpattern=%d [%t]%-5p%c-%m%n
2, the use of Dailyrollingfileappender, this mode is a time to cut files, you can thin each of the generation of a file such as:
Log4j.appender.a_r_date=org.apache.log4j.dailyrollingfileappender
log4j.appender.a_r_date.file=d:/test/ RollFiledate.log
log4j.appender.a_r_date.datepattern= '. ' yyyy-mm-dd-hh-mm '. Log '
log4j.appender.a_r_date.layout=org.apache.log4j.patternlayout
log4j.appender.A_ r_date.layout.conversionpattern=%d [%t]%-5p%c-%m%n
Log4j.appender.a_r_date.datepattern= '. '  yyyy-mm-dd-hh-mm '. Log ' means that a log file is generated every minute, such as: Rollfiledate.log.2014-07-12-14-09.log Rollfiledate.log.2014-07-12-14-10.log If you want to configure one log file per day, you can: log4j.appender.a_r_date.datepattern= '. ' Yyyy-mm-dd '. Log ' using log4j in Web projectsI'll just put the written log4j.properties in the SRC directory. References: http://logging.apache.org/log4j/1.2/manual.html

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.