Log4j log Configuration

Source: Internet
Author: User

Log4j log Configuration

 

1. Configure the root logger:

Log4j. rootlogger = [level], appendername, appendername2

Level: the log level, which specifies the importance of the log information. It can be divided into four types: All <debug <info <warn, which are generally used for debugging, info, warn, and error, which correspond to four methods of the logger class respectively.

Debug (Object message );

Info (Object message );

Warn (Object message );

Error (Object message );

If the set level is info, logs with a priority greater than or equal to Info level (such as info, warn, and error) can be output,

 

If it is smaller than this level, such as: Debug will not be output

Appendername: Specifies the log output destination, such as printing to the console and outputting to a file ). Same log information

 

2. Configure the log output destination (Multiple Output destinations can be configured ):

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. log information format:

Org. Apache. log4j. htmllayout (HTML table format)

Org. Apache. log4j. simplelayout (logs in simple format only contain the log information level and specified information strings, such as debug-Hello)

Org. Apache. log4j. ttcclayout (the log format includes the log generation time, thread, category, and other information)

Org. Apache. log4j. patternlayout (flexibly customize the log format)

 

When you use org. Apache. log4j. patternlayout to customize the information format, you can use

Log4j. appender. a1.layout. conversionpattern = % d {yyyy-mm-dd hh: mm: SS} % P-% m % N to format the information.

The full name of the class to which the % C output belongs. It can be written to % c {num}. The output range of the num class name is as follows: "com. sun. AAA. classb ", % c {2} will set the log output range to AAA. classb

% D: the log output time is in a format that can be specified, such as % d {hh: mm: SS }.

% L location of log event output, including category name, thread, number of lines in the code

% N line break

% M output code specified information, such as Info ("message"), Output Message

% P indicates the priority of the output log, such as fatal and error.

% R the time taken to output the log information from startup to display (in milliseconds)

% T name of the thread that outputs the log event

 

4. Use commons-logging and log4j at the same time

1) first look for your own configuration file commons-logging.properties under classpath, if found, then use the defined log implementation class

2) If the commons-logging.properties file is not found, find whether the system environment variable org. Apache. commons. Logging. log has been defined, and use its defined log implementation class.

3) Otherwise, check whether the classpath contains a log4j package. If any package is found, log4j is automatically used as the log implementation class.

4) Otherwise, use the JDK's own log implementation class (the log implementation class will be available only after jdk1.4)

5) Otherwise, use the simple log implementation class simplelog provided by commons-logging.

 

5. Multiple log files (log4j. rootlogger = info, a1, a2)

# The extension that A2 outputs to the file rollingfileappender, which provides a log backup function.

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

# Log4j. appender. a2.file = log4j. Log

# Log file size: log4j. appender. a2.maxfilesize = 100kb

# Save a backup file log4j. appender. a2.maxbackupindex = 1

 

Log4j. appender. a2.layout = org. Apache. log4j. ttcclayout

# Log4j. appender. a2.layout. conversionpattern = %-d {yyyy-mm-dd hh: mm: SS} [% C]-[% P] % m % N

 

Appendix: (log4j. properties)

Example 1:

Log4j. rootlogger = debug, stdout

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

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

Log4j. appender. stdout. layout. conversionpattern = % c {1}-% m % N

 

Example 2:

# Specify the root logger and log output level. Logs greater than or equal to this level are output (debug <info <warn <error <fatal) to off to disable logs.

Log4j. rootlogger = debug, A1, A2

# Specify the log output purpose. Here it is set to output the log to the file my. log in the specified directory.

Log4j. appender. A1 = org. Apache. log4j. fileappender

Log4j. appender. a1.file =\\ logs \ My. log # under the current root directory

# Specify the log information format

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

Log4j. appender. a1.layout. conversionpattern = % R % d {yyyy-mm-dd hh: mm: SS} % C % P-% m % N

 

# Output A2 to the console

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

Log4j. appender. a2.layout = org. Apache. log4j. simplelayout

 

# You can also specify the Log Level of the output package separately.

# Log4j.logger.com. Study. hellolog4j = info

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.