log4j Introductory Tutorial (ii) parameter explanation

Source: Internet
Author: User
Tags print format

Continue with the example in the log4j introductory tutorial (i), where the contents of Log4j.properties are three important components of log4j- loggers, Appenders, Layouts, the work of the three components together , enabling developers to record information based on information categories and levels, and to be able to control how records are logged in the way they were stored during the run.  

There are three main components in log4j, which are Logger, Appender, and layout,log4j that allow developers to define multiple Logger, each with its own name and Logger to indicate affiliation by name. There is a Logger called root, which is always present and cannot be retrieved or referenced by name, and can be obtained through the Logger.getrootlogger () method, and the other Logger through the Logger.getlogger (String name) method.
Appender is used to indicate where all the log information is stored, LOG4J supports a variety of appender, such as console, files, GUI components, NT Event loggers, etc. A logger can have multiple appender, that is, you can output the log information to the screen, and also stored in a file.
The function of layout is to control the output of log information, that is, formatted output information.
The log information that will be output in log4j defines 5 levels, in turn, Debug, info, WARN, error, and fatal, and when output, only the level of information specified in the level above configuration can be true output, so it is convenient to configure the content to be output under different circumstances, Without the need to change the code, this is really convenient.

Log4j.rootlogger=info, stdout, log, errorloglog4j. logger=search,test## #Console # # #log4j. appender.stdout = Org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.Target = System.outlog4j.appender.stdout.layout = Org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern =%d{absolute} [%t] [%p]:%l-%m%n### Lo G # # # Log4j.appender.log = Org.apache.log4j.DailyRollingFileAppenderlog4j.appender.log.File = log/ Log.loglog4j.appender.log.Append = Truelog4j.appender.log.Threshold = infolog4j.appender.log.datepattern= '. ' Yyyy-mm-ddlog4j.appender.log.layout = Org.apache.log4j.PatternLayoutlog4j.appender.log.layout.ConversionPattern = %-d{yyyy-mm-dd HH:MM:SS} [%t]%m%n### Error # # #log4j. Appender.errorlog = Org.apache.log4j.DailyRollingFileAppenderlog4j.appender.errorlog.File = log/ Errorlog.loglog4j.appender.errorlog.Append = Truelog4j.appender.errorlog.Threshold = ERROR Log4j.appender.errorlog.datepattern= '. ' Yyyy-mm-ddlog4j.appender.errorlog.layout = Org.apache.log4j.PatterNLayoutlog4j.appender.errorlog.layout.ConversionPattern =%-d{yyyy-mm-dd HH:mm:ss} [%t]%m%n 

Configure the root Logger, whose syntax is:

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

Level is the priority of the log record

#log4j中有五级logger, their priority is to lower in descending order

#FATAL

#ERROR

#WARN

#INFO

#DEBUG

It is equivalent to a total control switch, only the level to this extreme or above the log is recorded

Appendername is where you specify where the log information is exported. You can specify multiple output destinations at the same time.

Configure the log information output destination Appender, whose syntax is

Log4j.appender.appenderName = Fully.qualified.name.of.appender.classlog4j.appender.appenderName.option1 = Value1log4j.appender.appenderName.option = Valuen

LOG4J offers the following types of layout:

Org.apache.log4j.HTMLLayout (Layout in HTML table Form),

Org.apache.log4j.PatternLayout (flexibility to specify layout mode),

Org.apache.log4j.SimpleLayout (contains the level of log information and the information string),

Org.apache.log4j.TTCCLayout (contains information about the time, thread, category, etc.) of the log

Printing parameters:

[C-sharp]View Plain Copy
    1. Log4j.appender.log.layout.ConversionPattern =%-d{yyyy-mm-dd HH:mm:ss} [%t]%m%n

log4j formats the log information in a print format similar to the printf function in C, 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 platform is "/r/n", UNIX platform is "/n"

%d date or time of the output log point in time.

The default format is ISO8601, or you can specify a format later, such as:%d{yyy MMM dd HH:mm:ss, SSS}, output similar to: October 18, 2002 22:10:28, 921

%l where the output log event occurs, including the class name, the thread that occurred, and the number of rows in the code.

Pay special attention to the following parameter settings:

[C-sharp]View Plain Copy
    1. Log4j.appender.log.Threshold = ERROR

It is indicated in the general control switch premise, feel accurate, only to reach this level of the log is recorded in this file

Reference article:

Http://www.blogjava.net/rickhunter/articles/28133.html

log4j Introductory Tutorial (ii) parameter explanation

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.