log4j Tutorial 7, logging Level

Source: Internet
Author: User

The Org.apache.log4j.Level class provides the following levels, but can also be customized by subclasses of the level class.

Level
Description
All Levels include custom levels
DEBUG Specifying fine-grained information events is the most useful debugging for applications
ERROR Error events may still allow applications to continue running
FATAL Specifies a very serious error event that could cause the application to abort
INFO Message that specifies information that can highlight the operation of an application at a coarse-grained level
OFF This is the highest level, in order to turn off logging
TRACE Specify less granular information events than debug
WARN Specify a situation that is potentially harmful
How does the log level work?

Level p levels use Q when logging requests are logged, if P>=Q is enabled. This rule is the core of log4j. It assumes that the level is orderly. They relate to the standard level as follows: All < DEBUG < INFO < WARN < ERROR < FATAL < OFF.

The following example clearly indicates how all debug and info messages can be filtered. This program uses a record and executes the Setlevel (level.x) method to set the required logging level:

This example will print all the messages except debugging and information:

Importorg.apache.log4j.*; Public classLogclass {Private StaticOrg.apache.log4j.Logger log =Logger. GetLogger (Logclass.class);  Public Static voidMain (string[] args) {log.setlevel (Level.warn); Log.trace ("Trace message!"); Log.debug ("Debug message!"); Log.info ("Info message!"); Log.warn ("Warn message!"); Log.error ("Error message!"); Log.fatal ("Fatal message!"); }}

When you compile and run the Logclass program, the following results are produced:

Warn message! Error Message! Fatal Message!
To set the level using the configuration file:

LOG4J provides these to allow programmers to freely change the source code, changing the debug level of the configuration level is based on file settings.

The following is an example of a configuration file using the Log.setlevel (Level.warn) method as shown in the example above.

=/usr/home/= WARN, file# Define the FILE appenderlog4j.appender.FILE=  Org.apache.log4j.FileAppenderlog4j.appender.FILE.File=${log}/ for FILE Appenderlog4j.appender.FILE.layout=  Org.apache.og4j.PatternLayoutlog4j.appender.FILE.layout.conversionPattern=%m%n

Now, use the following program:

Importorg.apache.log4j.*; Public classLogclass {Private StaticOrg.apache.log4j.Logger log =Logger. GetLogger (Logclass.class);  Public Static voidMain (string[] args) {Log.trace ("Trace message!"); Log.debug ("Debug message!"); Log.info ("Info message!"); Log.warn ("Warn message!"); Log.error ("Error message!"); Log.fatal ("Fatal message!"); }}

Now, compile and run the above program to get the following results in the/usr/home/log4j/log.out file:

Warn message! Error Message! Fatal Message!

log4j Tutorial 7, logging Level

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.