log4j configuration file Detailed resolution _java

Source: Internet
Author: User
Tags html form syslog log4j

Advantages of 1.LOG4J
Log4j is an open source project for Apache, by using log4j, we can control the delivery of log information, we can control the output format of each log, and by defining the level of each log information, we can control the log generation process more carefully. Most interesting of all, these can be configured flexibly with a single configuration file without the need to modify the applied code.

The benefits of log4j are:
(1) By modifying the configuration file, we can decide the destination of log information-console, file, GUI component, even interface server, NT Event recorder, UNIX syslog daemon, etc.

(2) By modifying the configuration file, you can define the level of each log information to control whether or not to output. In the system development phase can print detailed log information to track the operation of the system, and the system can be closed after the log output, so that can track the operation of the system at the same time, but also reduce the garbage code (SYSTEM.OUT.PRINTLN (...). , etc.).

(3) Using log4j, the whole system has a unified log mechanism, which is advantageous to the system planning.

2. Configuration file
Log4j is composed of three important components: priority of log information, output destination of log information, and output format of log information. The priority of the log information from high to low has fatal, ERROR, WARN, INFO, DEBUG, TRACE, all, respectively, to specify the importance of this log information; the output destination of the log information specifies whether the log will be printed to the console or file , and the output format controls the display of the log information.

2.1 Priority of log information
LOG4J recommends using only four levels, from high to low, respectively, for error, WARN, INFO, and DEBUG. By the level defined here, you can control the switch to log information at the appropriate level in your application. If the info level is defined here, all log information below the info level in the application will not be printed.

2.2 Use of the output source
Selective ability to use or disable log requests is only part of the log4j function. LOG4J allows log requests to be exported to multiple output sources. In Log4j's words, an output source is called a appender.

Appender includes console (console), files (file), GUI components (component of graphics), remote socket servers (socket service), JMS (Java Information Service), NT Event Logge RS (NT Event log), remote UNIX Syslog daemons (back-end log service for remotely Unix). It can also do asynchronous logging. A logger can be set with more than one appender. Add a appender to a given logger using the Addappender method. For a given logger, each log request that is in effect is forwarded to the logger of all logger appender and the logger's parents appender.

2.2.1 Consoleappender
If you use Consoleappender, log information is written to the console. The effect is equivalent to printing the information directly to the System.out.

2.2.2 Fileappender
With Fileappender, log information is written to the specified file. This should be a more frequent use of the situation. Accordingly, the file name of the log output should be specified in the configuration file. The following configuration specifies that the log file name is Log.txt.

Log4j.appender.appendername.file=log.txt Note Replace Appendername with Appender aliases in a specific configuration.

Note: The log file path problem specified

2.2.3 Dailyrollingappender
Use Fileappender to output log information to a file, but it is inconvenient to read if the file is too large. Then you can use Dailyrollingappender. Dailyrollingappender can output log information to a file that is distinguished by date. The profile will produce a log file every day (time can be set), and each log file records only the log information for the day:

Copy Code code as follows:

Log4j.appender.appendername=org.apache.log4j.dailyrollingfileappender
Log4j.appender.aappendername.file=log
Log4j.appender.appendername.datepattern= '. ' Yyyy-mm-dd
Log4j.appender.appendername.layout=org.apache.log4j.patternlayout
log4j.appender.appendername.layout.conversionpattern=%5r%-5p%c{2}-%m%n

2.2.4 Rollingfileappender
A new file is generated when the file size reaches the specified size.
Copy Code code as follows:

Og4j.appender.appendername=org.apache.log4j.rollingfileappender
Log4j.appender.appendername.file=.. /logs/rlog.log
# Control the maximum log file size
log4j.appender.appendername.maxfilesize=100kb
# Archive log files (one backup file here)
Log4j.appender.appendername.maxbackupindex=1

Log4j.appender.appendername.layout=org.apache.log4j.patternlayout
log4j.appender.appendername.layout.conversionpattern=%p%t%c-%m%n


This configuration file specifies the output source Appendername, which is a rotary log file. The largest file is 100KB, when a log file reaches the maximum size, log4j will automatically rename Rlog.log to Rlog.log.1, and then rebuild a new Rlog.log file, turn the rotation.

2.2.5 Writerappender
Sends log information to any specified location in streaming format.

Configuration of 2.3 layout
layout specifies the style for log information output.

2.3.1 Layout Style

Copy Code code as follows:

Org.apache.log4j.HTMLLayout (layout in HTML form),
Org.apache.log4j.PatternLayout (You can specify layout patterns flexibly),
Org.apache.log4j.SimpleLayout (The level and information string that contains the log information),
Org.apache.log4j.TTCCLayout (contains information about the time, thread, category, and so on that the log was generated)

2.3.2 Format
Copy Code code 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 it takes to boot to output the log information
The class to which the%c output belongs, usually the full name of the class in which it is located
%t output The name of the thread that generated the log event
%n output a carriage return line feed, Windows platform "RN", UNIX platform "n"
%d output log point-in-time date or time, the default format is ISO8601, you can also specify the format after, such as:%d{yyy MMM dd hh:mm:ss,sss}, output similar: October 18, 2002 22:10:28,921
%l the location where the output log event occurs, including the class name, the thread that occurred, and the number of lines in the code. Example: Testlog4.main (Test log4.java:10)

3. Set the log output level for different Appender
This can be accomplished by modifying the appender threshold in the configuration, such as the following example:

Configuration file:
Log4j.rootlogger = Debug,a,b,c

# Output to Console
LOG4J.APPENDER.A = Org.apache.log4j.ConsoleAppender
Log4j.appender.a.target = System.out
Log4j.appender.a.layout = Org.apache.log4j.PatternLayout
Log4j.appender.a.layout.conversionpattern =%p%t%c-%m%n

# Output to log file
Log4j.appender.b = Org.apache.log4j.DailyRollingFileAppender
Log4j.appender.b.file = Logs/log.log
Log4j.appender.b.append = True
Log4j.appender.b.threshold = DEBUG # output Ebug level above log
Log4j.appender.b.layout = Org.apache.log4j.PatternLayout
Log4j.appender.b.layout.conversionpattern =%p%t%c-%m%n

# Save exception information to a separate file
LOG4J.APPENDER.C = Org.apache.log4j.DailyRollingFileAppender
Log4j.appender.c.file = logs/error.log # Exception Log file name
Log4j.appender.c.append = True
Log4j.appender.c.threshold = ERROR #只输出ERROR级别以上的日志
Log4j.appender.c.layout = Org.apache.log4j.PatternLayout
Log4j.appender.c.layout.conversionpattern =%p%t%c-%m%n

Example:

Copy Code code as follows:

public class testlog4j
{
public static void Main (string[] args)
{
Propertyconfigurator.configure ("D:/code/conf/log4j.properties");
Logger Logger = Logger.getlogger (Testlog4j.class);
Logger.debug ("Debug");
Logger.error ("error");
}
}

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.