Use log4j instances

Source: Internet
Author: User

1. Logger class

Use the static logger method logger. getrootlogger of the logger class to obtain the rootlogger. All other loggers are instantiated and obtained through the static method logger. getlogger. In this method, logger. getlogger takes the name of the logger as the parameter. Some other basic methods of the logger class are listed below:

 

Package org. Apache. log4j;

Public class logger {

 

// Creation and retrieval methods:

Public static logger getrootlogger ();

Public static logger getlogger (string name );

 

// Printing methods:

Public void debug (Object message );

Public void Info (Object message );

Public void warn (Object message );

Public void error (Object message );

Public void fatal (Object message );

 

// Generic printing method:

Public void log (Level L, object message );

}

 

 

 

2. getlogger Method

Call the getlogger method with the same parameter name. The returned reference always points to the same logger object. For example, here:

Logger x = logger. getlogger ("wombat ");

Logger y = logger. getlogger ("wombat ");

X and Y point to exactly the same logger object.

 

3. log4j usage process

1) initialize log4j Based on the configuration file

Log4j can be initialized using three kinds of configurator:

Basicconfigurator, domconfigurator, and propertyconfigurator.

Propertyconfigurator is used here. Propertyconfigurator applies to all systems. The following statement.

Propertyconfigurator. Configure ("log4j. properties ");

The log4j environment has been initialized using log4j. properties as the configuration file. For general Java projects, you can use the preceding statement to initialize log4j. log4j will automatically find the configuration file and initialize it under classpath. If log4j cannot automatically initialize the configuration file, you need to use the above method for initialization.

Note: It is recommended that the configuration file be initialized only once when the system is started. If the configuration file is executed multiple times, one is a waste of resources, and the other is when dailyrollingfileappender is used for earlier log4j versions, problems may occur.

 

2) Import org. Apache. log4j. Logger; and related packages.

 

3) obtain the log instance where log4j is needed.

Static logger log = logger. getlogger ("myclass. Class ");

 

4) use the debug, info, fatal... method of the logger object.

Log. debug ("it is the debug info ");

 

4. Example of using log4j

Test. Java

 

Import org. Apache. log4j. Logger;

 

Public class test {

Static logger log = logger. getlogger (test. Class );

Public void log (){

Log. debug ("debug info .");

Log.info ("info Info ");

Log. Warn ("Warn Info ");

Log. Error ("error Info ");

Log. Fatal ("Fatal Info ");

}

Public static void main (string [] ARGs ){

Test test = new test ();

Test. Log ();

}

}

 

 

 

Log4j. properties (for specific Configuration Attribute definitions, see log4j configuration instructions.)

 

Log4j. rootlogger = info, stdout

Log4j. appender. stdout = org. Apache. log4j. leleappender

Log4j. appender. stdout. layout = org. Apache. log4j. patternlayout

# Pattern to output the caller's file name and line number.

Log4j. appender. stdout. layout. conversionpattern = % 5 p [% T] (% F: % L)-% m % N

 

 

Result

Info [main] (test. Java: 16)-Info info

Warn [main] (test. Java: 17)-Warn info

Error [main] (test. Java: 18)-error info

Fatal [main] (test. Java: 19)-fatal info

 

Analysis:

Test. Java

"Logger. getlogger (test. class); "" test. "The fact is uploaded to the complete path of the test class (package name + class name)," test. test ". In this way, if the log "test" exists, the log "test" inherits it; otherwise, rootlogger is inherited.

 

Log4j. Properties

Configure log4j. rootlogger in the first line. It should be the root and must be configured; otherwise, other logs cannot be inherited. Other logs can be configured or not configured. The first parameter after the equal sign indicates the log level. You can enter one of the five levels (log4j divides the log information into five levels by default: Debug <info <warn <error <fatal ), the following parameters let the log know where the output is. If you want the log to be output to two places, add two output parameters, for example:

Log4j. rootlogger = info, stdout

Info indicates that the Log Level of the log is info, and all logs whose level is earlier than info are not recorded. For example, if this configuration file is used

Log. debug ("debug info .");

This statement does not work because the debug level is smaller than info. In this way, it is easy to control what information should be displayed during debugging and what information should be removed during release. No need to changeCode.

Configure stdout. The name is random. You can call it:

Log4j. appender. A = org. Apache. log4j. leleappender

The above rootlogger parameter stdout should also be changed to a, and other places of use should also be changed. The key here is not the name, but the appender type. For example, the "leleappender" here refers to the output to the console. The log format is set for the next two lines.

Pay: log4j. Property

1) Place log4j. properties or log4j. xml under the SRC root (classpath path), or combine spring configuration in Web. xml
> The following is an example of log4j. properties:
Log4j. rootcategory = debug, stdout, R

# Console config
Log4j. appender. stdout = org. Apache. log4j. leleappender
Log4j. appender. stdout. layout = org. Apache. log4j. patternlayout
Log4j. appender. stdout. layout. conversionpattern = <% d> <%-5 p> <% C>-% m (% F: % m: % L) % N

# File config
Log4j. appender. r = org. Apache. log4j. rollingfileappender
Log4j. appender. R. File =$ {catalina_home}/logs/test. Log
Log4j. appender. R. maxfilesize = 100kb
Log4j. appender. R. maxbackupindex = 0
Log4j. appender. R. layout = org. Apache. log4j. patternlayout
Log4j. appender. R. layout. conversionpattern = <% d> <%-5 p> <% C>-% m (% F: % m: % L) % N

# Package config
Log4j.logger.org. Apple = debug
Log4j.logger.org. Apache. commons = Error
Log4j.logger.org. springframework = Error
Log4j.logger.org. Apache. Struts = Error

 

In addition, the following code from the http://www.javaeye.com/topic/8307 may be useful for reference

Code
Java code
Log4j. rootlogger = Debug, stdout, file

### Direct log messages to stdout ###
Log4j. appender. stdout = Org. Apache. log4j. leleappender
Log4j. appender. stdout. Target = System. Out
Log4j. appender. stdout. Layout = Org. Apache. log4j. patternlayout
Log4j. appender. stdout. layout. conversionpattern = % M % N

### Direct messages to file hibernate. Log ###
Log4j. appender. File = Org. Apache. log4j. rollingfileappender
Log4j. appender. file. File = Hibernate. Log
Log4j. appender. file. maxfilesize = 100kb
Log4j. appender. file. maxbackupindex = 1
Log4j. appender. file. Layout = Org. Apache. log4j. patternlayout
Log4j. appender. file. layout. conversionpattern = % D {absolute} % 5 p % C { 1 }: % L -   % M % N

### Set Log levels -   For More verbose logging change ' Info ' To ' Debug ' ###

Log4j.logger.org. hibernate = Info

### Log just the SQL
# Log4j.logger.org. hibernate. SQL = Debug

### Log JDBC bind parameters ###
Log4j.logger.org. hibernate. Type = Info

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.