Log4j log Configuration

Source: Internet
Author: User
Tags print format ranges

 

The parameter configuration in the log4j configuration file is mainly divided into three parts: Priority configuration, log information output destination configuration, and log information output format configuration.

The log priority ranges from high to low, including error, warn, info, and debug, which are used to specify the importance of the log information;

The log output destination specifies whether the log will be printed to the console or a file. The output format controls the display of log information.

1. Define the configuration file:

Log4j configuration files are in XML format and Java feature files (key-value pairs.

1. Configure logger. Syntax format:

Log4j. rootlogger = [level], appendername, appendername ,...

Level is the log record priority, which can be off, fatal, error, warn, info, debug, all, or the level you define. We recommend that you use only four levels of log4j. The priority ranges from high to low: error, warn, info, and debug. By defining the level here, you can control the switch to the corresponding level of log information in the application. For example, if the info level is defined here, all debug-level logs in the application will not be printed.

Appendername refers to the location where the log information is output. You can specify multiple output destinations at the same time.

  

 

2. Configure the log output destination appender. Its syntax is:

Log4j. appender. appendername = fully. Qualified. Name. Of. appender. Class
Log4j. appender. appendername. option1 = value1
...
Log4j. appender. appendername. Option = valuen

Log4j provides the following types of appender:
Org. Apache. log4j. leleappender (console ),
Org. Apache. log4j. fileappender (file ),
Org. Apache. log4j. dailyrollingfileappender (a log file is generated every day ),
Org. Apache. log4j. rollingfileappender (a new file is generated when the file size reaches the specified size ),
Org. Apache. log4j. writerappender (send log information to any specified place in stream format)

 

 

3. Configure the log information format (layout). Its syntax is:

 

Log4j. appender. appendername. layout = fully. Qualified. Name. Of. layout. Class
Log4j. appender. appendername. layout. option1 = value1
...
Log4j. appender. appendername. layout. Option = valuen

 

Among them, the layout provided by log4j has several types of E:
Org. Apache. log4j. htmllayout (in the form of HTML tables ),
Org. Apache. log4j. patternlayout (you can flexibly specify the layout mode ),
Org. Apache. log4j. simplelayout (including the log information level and information string ),
Org. Apache. log4j. ttcclayout (including the log generation time, thread, category, and so on)

 

Log4j uses a print format similar to the printf function in C language to format log information. The print parameters are as follows: % m outputs the specified message in the code.

 

% P output priority, namely debug, info, warn, error, fatal
% R the number of milliseconds it takes to output the log information from application startup to output
% C output category, usually the full name of the class
% T name of the thread that outputs the log event
% N output a carriage return line break. For Windows, the value is/R/N, and for UNIX, the value is/n"
% D date or time of the log output time point. The default format is iso8601. You can also specify the format after it, for example, % d {YYY Mmm dd hh: mm: SS, SSS}, output is similar to: October 18, 2002 22:10:28, 921
% L location of log event output, including category name, thread, and number of lines in the code. Example: testlog4.main (testlog4.java: 10)

The following is an example:

####################################### Definition root, defined as debug level, and the output destination ##################################### # log4j. rootlogger = debug, stdout, system # the console prints log4j. appender. stdout = org. apache. log4j. leleappender log4j. appender. stdout. layout = org. apache. log4j. patternlayout log4j. appender. stdout. layout. conversionpattern = [% 5 p] % d {yyyy-mm-dd hh: mm: SS, SSS} (% F: % L) % m ()-% m % N
# Record system logs to files and logs not at the package log level
Log4j. appender. System = org. Apache. log4j. rollingfileappender
Log4j. appender. system. File =$ {Catalina. Base}/logs/heza_system.log
Log4j. appender. system. layout = org. Apache. log4j. patternlayout
Log4j. appender. system. maxfilesize = 30 mb
Log4j. appender. system. maxbackupindex = 50
Log4j. appender. system. layout. conversionpattern = [% 5 p] % d {yyyy-mm-dd hh: mm: SS, SSS} (% F: % L) % m ()-% m %

 

1. Configure logger:

Log4j. rootlogger = debug, stdout, System

The priority level is the debug level, and the output destination is stdout and sysytem;

2. Configure the output destination:

Log4j. appender. stdout = org. Apache. log4j. leleappender

Log4j. appender. System = org. Apache. log4j. rollingfileappender

Set stdout output destination to consoleappender (console), and system output destination to rollingfileappender (a new file is generated when the file size reaches the specified size)

3. Configure the output format:

Log4j. appender. stdout. layout = org. Apache. log4j. patternlayout

Log4j. appender. stdout. layout. conversionpattern = [% 5 p] % d {yyyy-mm-dd hh: mm: SS, SSS} (% F: % L) % m ()-% m % N

And

Log4j. appender. system. layout = org. Apache. log4j. patternlayout
Log4j. appender. system. maxfilesize = 30 mb
Log4j. appender. system. maxbackupindex = 50
Log4j. appender. system. layout. conversionpattern = [% 5 p] % d {yyyy-mm-dd hh: mm: SS, SSS} (% F: % L) % m ()-% m %

####################################### Definition com. ibeans. manage package log, defined as debug level, and the output destination ##################################### # log4j.logger.com. ibeans. manage = debug, managelog4j.additivity.com. ibeans. manage = false # (Management Platform) records COM. ibeans. log in the manage package to the file log4j. appender. manage = org. apache. log4j. rollingfileappenderlog4j. appender. manage. file =$ {Catalina. base}/logs/heza_manage.loglog4j.appender.manage.layout = org. apache. log4j. patternlayoutlog4j. appender. manage. maxfilesize = 30mblog4j. appender. manage. maxbackupindex = 50log4j. appender. manage. layout. conversionpattern = [% 5 p] % d {yyyy-mm-dd hh: mm: SS, SSS} (% F: % L)-% m % N

 

Log4j log Configuration

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.