1.Logger classRootlogger is obtained by Logger.getrootlogger the static method of the Logger class. All other loggers are instantiated and obtained through the static method Logger.getlogger. This method Logger.getlogger the name of the desired logger as a parameter. Some other basic methods of the Logger class are listed below:
Packageorg.apache.log4j; PublicclassLogger {//Creation and Retrieval methods: PublicStaticLogger Getrootlogger (); PublicStaticLogger GetLogger (String name); Printing methods: PublicvoidDebug (Object message); PublicvoidInfo (Object message); PublicvoidWarn (Object message); PublicvoidError (Object message); PublicvoidFatal (Object message); Generic Printing method: PublicvoidLog (level L, Object message);} |
2. GetLogger MethodThe GetLogger method is called with the same parameter name, and the returned reference always points to the exact same logger object. For example, here:
Logger x = Logger. GetLogger ("Wombat"); Logger y = Logger. GetLogger ("Wombat"); |
X and y point to the exact same logger object.
3.log4j Use Process1) According to the configuration file initialization log4j log4j can use 3 kinds of configurator to initialize: Basicconfigurator,domconfigurator,propertyconfigurator. Propertyconfigurator is used here. Use Propertyconfigurator for all systems. As the following statement.
Propertyconfigurator.configure ("Log4j.properties"); |
The log4j environment is initialized with Log4j.properties as the configuration file. For generic Java project, you can initialize log4j,log4j without using the above statement, and automatically locate the configuration file and initialize it under Classpath. If log4j cannot initialize the configuration file automatically, then it needs to be initialized with the method above. Note: Initialize the configuration file, preferably only at the time of system startup, if executed several times, one is a waste of resources, and the other is the old version of the log4j, when using Dailyrollingfileappender, there may be problems. 2) Import Org.apache.log4j.Logger, and related packages. 3) Get the log instance where you need to use log4j.
Private Static Logger log = Logger.getlogger ("Myclass.class"); |
4) Use the Logger object's debug,info,fatal ... Method.
Log.debug ("It is the Debug info"); |
4.log4j Use ExampleTest.java
ImportOrg.apache.log4j.Logger; Public classTest {StaticLoggerLog= Logger.GetLogger(Test.class); PublicvoidLog () {Log. Debug ("Debug Info.");Log. info ("info info");Log. Warn ("Warn info");Log. Error ("error info");Log. Fatal ("fatal info"); } PublicStaticvoidMain (string[] args) {test test =NewTest (); Test.log (); }} |
Log4j.properties (Specific configuration attribute definitions See "log4j configuration Instructions")
Log4j.rootlogger=info, Stdoutlog4j.appender.stdout=org.apache.log4j.consoleappenderlog4j.appender.stdout.layout =org.apache.log4j.patternlayout# Pattern to output the caller ' s file name and line number.log4j.appender.stdout.layout.conversionpattern=%5p [%t] (%f:%l)-%m%n |
result info [main] (test.java:16)-Info Infowarn [main] (test.java:17)-Warn infoerror [main] (test.java:18)-Error Infofata L [main] (test.java:19)-Fatal info analysis: Test.java "Logger.getlogger (Test.class);" The "Test.class" here actually passes in the full path of the test class (package name + class name), "Test." Test ". So if there is a "test" log then test this log inherits it, otherwise it inherits Rootlogger. Log4j.properties first line, configure Log4j.rootlogger. It should be root, must be configured, or else log cannot inherit. The other logs can be configured or not configured. The first parameter after the equals sign represents the log level, which can be filled in one of five levels (log4j default divides the log information into five levels of debug < Info < Warn < error < fatal), followed by the arguments to let the log know where the output is, If you want to have the log output to two places, add two output parameters, such as:
Log4j.rootlogger=info, stdout |
The info here indicates that the log level is info, and all logs with a level less than info are not logged. For example, using this configuration file, I just started to lift the class:
log. Debug ("Debug Info."); |
This sentence does not work because the level of debug is less than info. This makes it easy to control what information is displayed when debugging, and what information is removed when it is released. These don't have to change the code. Configure stdout, the name is random, you can call it a:
Log4j.appender.a=org.apache.log4j.consoleappender |
Then the above Rootlogger parameter stdout also to change to a, the other used places of course also need to change. The key here is not the name, but the Appender type, such as "Consoleappender" here, referring to the output to the console. The next two lines are formatted with the log.
5.Apache Log Introduction Set[1]
Apache log4j Configuration Instructions[2]
Apache log4j Usage examples[3]
Apache commons-logging Usage examples[4]
How to build a self-appender extended log4j Framework
6. References[1] log4j Official Handbook [2] log4j minimalist http://afront.bokee.com/6157060.html
log4j Simple to use