Log4j configuration output to multiple log files

Source: Internet
Author: User

In general, some important logs in our project want to be output to the specified file separately, instead of the total output to the System Log File. Therefore, log4j provides us with this function, next, let's take a step-by-step look at how it works. This is written in the property configuration mode. The XML method is similar. For more information, see the official documentation.

The project package structure tested here is as follows:

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

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

Each logger can correspond to multiple appender, while appender and layout correspond one-to-one.

The simplest configuration is as follows:

1. log4j. the Configuration Mode of rootlogger is log4j. rootlogger = level, appender1, appender2 ,... level indicates the log level. The relationship is as follows: Debug <info <warn <error <fatal. Smaller levels allow you to output logs larger than yourself. For example, if it is set to debug, You can output logs such as warn, error,. Followed by 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: Specifies the layout class. 4. log4j. appender. a_console.layout.conversionpattern = % d [% T] %-5 p % C-% m % N output formatting. For more information, see the patternlayout API. Let's use a main method to output the log to see the log printing result. Configuration of multiple appener Each logger can correspond to multiple appender, as shown in the following configuration: in this way, logs will be sent to the console and mylog. log files when we are traveling far away from the app, which will not be pasted out. Configure multiple logger Cases  When we need to extract some important logs, or a relatively independent module in the system, such as an interface dedicated to external systems, we can configure multiple logger, log segmentation facilitates our daily maintenance work. Next we will add two logger shards based on the original one: "com.pt. test and com.pt. test. INTF is defined by the package name. Let's take a look at the detailed configuration. We noticed that the two newly added logger configurations are log4j.logger.com.pt. test = debug, a_file2 log output to: D:/test/logfile2.loglog4j.logger.com.pt. test. INTF = debug, a_file3 logs are output to: D:/test/logfile3.log. Let's take a look at the log output content of the three files: logfile1.loglogfile2. loglogfile3.log at this time, we will find that filelog1, filelog2, and filelog3 all print the log Content. Careful friends should find that filelog1 contains all the content in filelog2. By default, the sub-logger logs are sent to all parent apps. Ender output. If we do not want this output, we can set the additivity attribute of the corresponding looger to false. For example, open log4j.additivity.com.pt with the 17th line comment. in this case, rootlogger will no longer have logger "com.pt. test "and its sub-packages. Two modes of rolling log file generation: 1. Use rollingfileappender to generate a log file based on the size and size. Configure maxfilesize to limit the size of each file, the system will automatically generate the next file, as shown in the following appender: 2. Use dailyrollingfileappender. In this mode, the file is cut by time and one file can be generated in detail, such as log4j. appender. a_r_date.datepattern = '. 'yyyy-mm-dd-hh-mm '. log' indicates that a log file is generated every minute, for example, rollfiledate. log.2014-07-12-14-09.log rollfiledate. log.2014-07-12-14-10.log if you want to configure a log file every day, you can: log4j. appender. a_r_date.datepattern = '. 'yyyy-mm-dd '. log' Use log4j in web projectsI just need to 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.