Three main components of the log4j configuration file

Source: Internet
Author: User

The three main components of the log4j configuration file are:

Logger,appender and layout, respectively, log type, log output destination, log output format.
Log4j.rootlogger = [Level],appendername, Appendername, ...

(Level is the error, Appendername is the output destination, this example is set to MyLog, you can define more than one) level priority is fatal, ERROR, WARN, INFO, DEBUG 5 levels. By defining the level, You can control the log output in the program. For example, the error level is defined here, and only the faral, error level log is output.
log4j.appender.mylog= Output Destination (here the appendername is defined above, can be arbitrarily named)
The log4j provides output destinations in the following ways:
Org.apache.log4j.ConsoleAppender (console)
Org.apache.log4j.FileAppender (file)
Org.apache.log4j.DailyRollingFileAppender (Generate a log file every day)
Org.apache.log4j.RollingFileAppender (a new file is generated when the file reaches the specified size)
Org.apache.log4j.WriterAppender (send log information to any location in stream format)

Log4j.appender.filelog.file=your File dir
Log4j.appender.filelog.maxfilesize=your FileSize
Log4j.appender.mylog.maxbackupindex=num setting the number of saved backup files

Log4j.appender.appenderName.layout = Layout type (set layout type)
LOG4J offers the following 4 types of layout:
Org.apache.log4j.HTMLLayout (Layout in HTML table Form)
Org.apache.log4j.SimpleLayout (contains level and information strings for log information)
Org.apache.log4j.TTCCLayout (contains information about the time, thread, category, etc.) of the log
Org.apache.log4j.PatternLayout (flexibility to specify layout mode)
If you use the Patternlayout layout to specify the exact format of the print information Conversionpattern, print the parameters as follows:
%M the message specified in the output code
%p output priority, i.e. Debug,info,warn,error,fatal
%r output the number of milliseconds that the log information is consumed from the application boot to output
%c output belongs to the class, which is usually the full name of the class
%t output The name of the thread that generated the log event
%n output a carriage return line break, Windows is "RN", Unix is "n"
%d output log time, for example:%d{yyyy MMM ddhh:mm:ss,sss}, Output: May 17, 2007 19:30:00,000
%l where the output log event occurs, including the class name, the thread that occurred, and the number of rows in the code
[QC] is the beginning of the log information, can be any character, generally referred to as the project

The Log4j class is not used directly in the program, but the log class provided by Commons-logging is used. Commons-logging is a unified interface for all Java log implementations, and its functionality is said to be "usually weak."
Log (Basic logger) and logfactory (responsible for creating log instances). When Commons-logging.jar is added to the classpath, it will reasonably guess the log tool you like and then self-set, and the user does not need to do any settings at all. The default logfactory is to follow the steps below to discover and decide which log tool will be used (in order, the search process will be aborted when the first tool is found):
1. Look for the value in the current factory named Org.apache.commons.logging.Log configuration property
2. Find the value of the attribute in the system named Org.apache.commons.logging.Log
3. If there is log4j in the classpath of the application, use the relevant wrapper (wrapper) class (Log4jlogger)
4. If the application is running on a jdk1.4 system, use the relevant wrapper class (Jdk14logger)
5. Use simple Log wrapper class (Simplelog)

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Three main components of the log4j configuration file

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.