Log4j is really familiar with almost all Java projects using it. But I didn't get it. Finally one day I can not stand, set heart to read a document, only two hours, I finally got it. Under normal circumstances log4j always with Apache commons-logging together, I will introduce it together. A lot of things are not more troublesome, but simpler.
<!--[if!supportlists]--> one, <!--[Endif]-->log4j's simple idea
Log4j is really simple, simple to the point of outrageous. Not to log it. So I'll give you a log, and then you can write something with log, and first come to a complete class example:
Package test;
Import Org.apache.commons.logging.Log;
Import Org.apache.commons.logging.LogFactory;
public class Test {
Static log log = Logfactory.getlog (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");
}
/**
* @param args
*/
public static void Main (string[] args) {
Test test = new test ();
Test.log ();
}
}
Don't be afraid, after reading this article you will feel very simple.
log4j The log information into five levels by default
Debug < Info < Warn < error < fatal
Although I can add the level, but I think there is no need, five is enough. When you write the information, you classify the information as one of the five levels, and then call the corresponding function.
What is the use of grading five grades? Where did the log information go?
"Logfactory.getlog (Test.class)" is what meaning pinch. Then look down.
The key point of log4j is that it inherits ideas. That is, a log can inherit another log's attributes (where output goes, log rank, log format, and so on). How to inherit.
Log4j is based on the name of the log to determine the inheritance relationship, such as:
The name "Com.zhlmmc.lib" Log is "Com.zhlmmc.lib.log" of the parent, understand it. LOG4J also has a rootlogger, which is equivalent to Java object.
Looking back at "Logfactory.getlog" here, "Test.class" actually goes in the full path (package name + class name) of test this class, "Test." Test.class Test ". So if there is a "test" log, then test this log inherits it, otherwise it inherits Rootlogger.
Where is the specific log attribute defined?
<!--[if!supportlists]--> II, <!--[endif]--> Common configuration files
Although it is possible to configure log4j with XML or Java at run time, the properties file is useful.
Log4j.rootlogger=info, stdout
Log4j.appender.stdout=org.apache.log4j.consoleappender
Log4j.appender.stdout.layout=org.apache.log4j.patternlayout
# to output the caller ' s file name and line number.
log4j.appender.stdout.layout.conversionpattern=%5p [%t] (%f:%l)-%m%n
Analyze:
First line, configure Log4j.rootlogger you know. It should be the root, must be configured, otherwise the other log inherit what ah. The other log can be configured or not configured. The first parameter after the equal sign represents the log level, one of five levels, followed by a parameter that lets log know where the output is, and if you want the log output to two places, add two output parameters, such as:
Log4j.rootlogger=info, stdout, file
The info here indicates that log level is info, and all logs with less than info are not logged. For example, using this configuration file, the class that I just started lifting
Log.debug ("Debug Info.");
This sentence is not working because the debug level 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 are not changed code, it is very convenient.
But what about stdout and file?
Then look down, is the configuration of stdout, the name is random, you can call it a:
Log4j.appender.a=org.apache.log4j.consoleappender
Then the above Rootlogger parameter stdout also must change to a, the other use place of course also must change. The key here is not the name, but the Appender type, such as the "Consoleappender" here, see the bar, output to console. The next two lines are set log format, usually you copy it. Since it's the simplest introductory focus on understanding how log4j works, I'm not going to introduce the file type of Appender, a bunch of searches.
In the actual project development, it is likely to encounter the quoted package log4j to log, such as Hibernate. So here you can easily control how the packet logs. For example, add a line to the above configuration file:
Log4j.logger.org.hibernate=fatal
Then all the classes below the Org.hibernate package will show very little information because the fatal is the highest level.
<!--[If!supportlists]--> III, <!--[endif]--> deployment
Don't be afraid, this is not a deployment of Tomcat. Put the log4j bag and the commons-logging bag (add together only two) under Classpath. Then save the configuration file as Log4j.properties, also under CLASSPATH (if you use Eclipse, put it in the SRC directory). And then you can run.