Learn from Teacher Wang (ix): log4j Basic Use Method

Source: Internet
Author: User
Tags throwable

Log4j Basic Use Method
Speaker: Wang Shaohua QQ Group No.: 483773664
Learning Goals
    • Mastering the use of log4j in Java projects

    • Understand the meaning of each configuration of log4j.properties


LOG4J consists of three important components: the priority of the log information, the output destination of the log information, and the output format of the log information.

I. Output level (i) Output level
1 log4j.rootLogger=debug, stdout,logfile

Where debug refers to the output level of the logger (Logger).

(b), the meaning of each level
    • Fatal: Indicates a critical error event that will cause the application to exit

    • Error: Indicates that the system continues to operate without affecting the failure event

    • Warm: A scenario that indicates a potential error

    • Info: Indicates the message at a coarse-grained level, emphasizing the application's running process

    • Debug: Indicates fine-grained information events that are very helpful for debugging applications.

The priority levels for each application are as follows

Fatal>error>warn>info>debug

These priorities mean that the logger (Logger) will only output information that is above or equal to that level. For example, the level of debug will output fatal, error, warn, info, debug level log information, and level error will only output fatal, error level log information.

Second, the log output destination Appender (a) The destination of the log output
1 log4j.rootLogger=debug, stdout,logfile

Where stdout, logfile refers to the name of the destination where the log output is

(ii) Type of destination

LOG4J allows logs to be output to multiple destinations, and an output destination is called a appender. The most commonly used are two kinds:

    • Consoleappender: Output Log to console, configure output to System.err or System.out through the target property. The default target is System.out

    • Fileappender: Output Log event to a file. The path and name of the file property profile

In addition to these two types, the log4j also offers the following three output destinations

    • Org.apache.log4j.DailyRollingFileAppender (generates 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)

Iii. Log layout type layout (a) Log layout type

The Appender must use a layout type layouts associated with it to specify its output style

1 log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout
(ii) Layout style

The following 3 types of layout are most commonly used in log4j

    • Htmllayout: Formatted log output as HTML table

    • Simplelayout: Format the log output in a very simple way, it outputs level levels, followed by a dash "-----", and finally a log message

    • Patternlayout: Formats the log output according to the specified conversion mode, thus supporting a rich variety of output formats. You need to configure layout. The Conversionpattern property, if this property is not configured, uses the default conversion mode.

In addition to the above 3 kinds of commonly used, there is one of the following

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

Iv. Conversion Mode Conversionpattern (I.), example
1 log4j.appender.logfile.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss}%l %F %p %m%n
(b) Parameter description

For patternlayout, you need to configure layout. Conversionpattern properties, common configuration parameters and meanings are as follows:

    • %d: the date and time used to set the output log, the default format is ISO8601. You can also specify the format later, such as%d{yyyy-mm-dd HH:mm:ss}, and the output format is similar to 2016-04-25 09:10:08

    • %m: Used to output the message specified in the code

    • %n: Used to output carriage return line breaks

    • %l: Used to output the occurrence of log events, the class name, the thread that occurred, and the number of rows in the code. such as: Chapter06_08.Test.main (test.java:26)

    • %p: Used to output priority, i.e. debug, info, warn, error, fatal, etc.

    • %F: Used to output file names

    • %M: Used to output method names

1 2016-04-2513:43:34chapter06_08.Test.main(Test.java:26) Test.java ERROR / by zero
V. Using LOG4J (a) in the code to get the recorder

The first step in using log4j is to get the logger, which is responsible for controlling the log information. Its syntax is as follows:

1 publicstaticLogger getLogger( String name)

Name generally takes the names of this class

1 privatestaticLogger logger = Logger.getLogger(Test.class.getName());
(ii) Insert log information

The Logger object provides a series of methods to output different levels of log information

    • public void Debug (Object msg)

    • public void Debug (Object msg,throwable t)

    • public void info (Object msg)

    • public void info (Object msg,throwable t)

    • public void warn (Object msg)

    • public void warn (Object msg,throwable t)

    • public void error (Object msg)

    • public void error (Object msg,throwable t)

    • public void Fatal (Object msg)

    • public void fatal (Object msg,throwable t)

Six, more

About Log4j More detailed introduction can go to the official website to view the document, also can go to my blog.

My blog: http://teacherwang.blog.51cto.com/10946447/1767469

log4j Document: http://logging.apache.org/log4j/1.2/manual.html















From for notes (Wiz)

Learn from Teacher Wang (ix): log4j Basic Use Method

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.