Source: Internet
Author: User



Log4j has three main components: recorder (loggers), Storage (appenders), Layout (Layouts), which can be simply understood as the log category, where the log is to be exported and how the logs are exported.

1. Configure Root logger:
Log4j.rootlogger = [level], Appendername, appenderName2
Levels: The level of the log that specifies the importance of this log information. is divided into all < DEBUG < INFO < WARN generally used for

DEBUG, INFO, WARN, error Four, four ways to correspond to logger class respectively
Debug (Object message);
Info (Object message);
Warn (Object message);
Error (Object message);
If the setting level is info, the log information with priority greater than or equal to the info level (such as: info, WARN, ERROR) will be output.

Less than this level, such as: Debug will not be output
Appendername: Specifies the log information output destination, such as (print to console, output to file, etc.). Same log information

Multiple output destinations can be configured.

2. Configure log Output destination:
Org.apache.log4j.ConsoleAppender (console)
Org.apache.log4j.FileAppender (file)
Org.apache.log4j.DailyRollingFileAppender (Generate a log file every day)
Org.apache.log4j.RollingFileAppender (creates a new file when the file size reaches the specified size)
Org.apache.log4j.WriterAppender (send log information in stream format to any specified location)
3. Format of log information:
Org.apache.log4j.HTMLLayout (HTML table Form)
Org.apache.log4j.SimpleLayout (a simple-format log that includes only the level of log information and the specified string of information, such as: Debug-hello)
Org.apache.log4j.TTCCLayout (the format of the log includes information such as the time the log was generated, threads, categories, etc.)
Org.apache.log4j.PatternLayout (Flexible custom log format)

When you use Org.apache.log4j.PatternLayout to customize the information format, you can use the
Log4j.appender.a1.layout.conversionpattern=%d{yyyy-mm-dd HH:mm:ss}%p-%m%n to format information
%c output belongs to the full name of the class, can be written as%c{num}, the Num class name output range such as: "Com.sun.aaa.classB",%c{2} will make the output range of the log output is: AAA.CLASSB
%d output log time in a format that can be specified in format such as%d{hh:mm:ss}
%l output Log event location, including the class name, the thread that occurred, the number of lines in the code
%n line break
%M Output code specifies information, such as info ("message"), output message
%p the priority of the output log, i.e. FATAL, ERROR, etc.
%r elapsed time (in milliseconds) from boot to display of this log information
%t output The name of the thread that generated the log event

4. Simultaneous use of commons-logging and log4j

1) First look for your own configuration file under Classpath commons-logging.properties, if found, use the

Log Implementation Class
2) If the Commons-logging.properties file is not found, find out if the system environment variable is defined

Org.apache.commons.logging.Log, find the Log implementation class that uses its definition
3) Otherwise, see if there are log4j packages in classpath, and if found, automatically use log4j as the log implementation class
4) Otherwise, use the JDK's own log implementation class (JDK1.4 only after the log implementation class)
5) Otherwise, use commons-logging to provide a simple log implementation class Simplelog

5. Multiple log files (Log4j.rootlogger=info, A1, A2)

#A2 output to a file rollingfileappender extension, you can provide a log backup function.

#日志文件的名称 Log4j.appender.a2.file=log4j.log
#日志文件的大小 log4j.appender.a2.maxfilesize=100kb
#保存一个备份文件 log4j.appender.a2.maxbackupindex=1


#log4j. Appender.a2.layout.conversionpattern=%-d{yyyy-mm-dd HH:mm:ss} [%c]-[%p]%m%n

#log4j. appender.a2.layout.conversionpattern=%5p [%t] (%f:%l)-%d-%m%n

By-Laws: (Log4j.properties)
Example 1:
Log4j.rootlogger=debug, stdout

Example 2:
#指定根Logger, and log output levels, logs greater than or equal to this level will be output (DEBUG < INFO < WARN < ERROR < FATAL) set to off to close the log
Log4j.rootlogger=debug, A1,A2, A3
#指定log输出目的, this is set as the output log in the file My.Log of the specified directory
Log4j.appender.a1.file=\\logs\\my.log #当前根目录下
Log4j.appender.a1.layout.conversionpattern=%r%d{yyyy-mm-dd HH:mm:ss}%c%p-%m%n



Log4j.appender.A3 =org.apache.log4j.dailyrollingfileappender//per day output
Log4j.appender.a3.file=logs/my.log//File location
Log4j.appender.a3.datepattern= '. ' YYYY-MM-DD//File format
Log4j.appender.a3.layout.conversionpattern=%d{yyyy-mm-dd Hh:mm:ss,sss}%5p%c{1}:%l-%m%n

#log4j. Logger.com.study.hellolog4j=info


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.