Log4j 2 of the benefits of not and everyone said, if you search 2, that you have a certain understanding of him, and want to use it, so the direct start here.
1. Go to the official Download log4j 2, import jar package, basically you only need to import the following two jar packages on it (xx is a messy version number):
Log4j-core-xx.jar
Log4j-api-xx.jar
2. Import into your project: This will not be said.
3. Start using:
We know that to use the log4j log in a class, you only need to declare the following member variable (in fact not necessarily a member variable, just for convenience)
private static Logger Logger = Logmanager.getlogger (MyApp.class.getName ());
Here GetLogger has a parameter that specifies the name of the logger, which is needed in the configuration file, and this will be later.
By declaring the Logger object, we can use it in the code.
4. Level of the log:
We're going to call the logger method now, but there are a lot of methods in this logger object, so we need to understand the log level of log4j, log4j set the default levels: Trace<debug<info<warn<error <fatal and so on. Here is to explain:
1 levels are included in the relationship, meaning that if you set the log level to be trace, the log will be output greater than or equal to this level.
2 Basically the default level is not much different, is a default setting. You can define the level by its API. You can also call these methods at will, but you have to do well in the configuration file, otherwise you will not be able to log the role, but also difficult to read, the equivalent of a specification, you have to fully define a set of can also, do not have much need.
3 The meaning of this different level is easy to understand, here is a brief introduction:
Trace: Is tracking, that is, the program pushes the following, you can write a trace output, so trace should be very much, but it doesn't matter, we can set the minimum log level to not let him output.
Debug: Debug, I usually just use this as the lowest level, and trace doesn't use it at all. Is it okay to use the debug feature of Eclipse or idea?
Info: Output The information you are interested in or important, which is the most used.
Warn: Some of the information is not an error message, but there are also some hints for the programmer, similar to the validation of the code in eclipse that there is no error or warn (not a mistake but also note, for example, the following depressed method).
Error: Wrong message. Use more.
Fatal: The level is higher. Major error, this level you can directly stop the program, it should not be a mistake. Not so nervous, in fact, is a matter of degree.
5. Log Call:
Here casually write a class, the call is so simple, log4j the core of the configuration file.
Import Org.apache.logging.log4j.Level;
Import Org.apache.logging.log4j.LogManager;
Import Org.apache.logging.log4j.Logger;
public class Hello {
static Logger Logger = Logmanager.getlogger (Hello.class.getName ());
public Boolean Hello () {
logger.entry (); Trace-level information, listed separately, is the one that you want to call at the beginning of a method or program logic, and the basic meaning of Logger.trace ("entry")
Logger.error ("Did it again!"); Error level information, the parameter is the information you output
logger.info ("I am info information"); Info level information
logger.debug ("I am Debug Information");
Logger.warn ("I am warn Information");
Logger.fatal ("I am fatal Information");
Logger.log (Level.debug, "I am DEBUG information"); This is to make the level type call: Who is idle to call this, not necessarily oh.
logger.exit (); The end method corresponding to entry (), and Logger.trace ("exit"); one meaning return
false;
}
If there is no custom profile, the above class is writing a main method, the console will enter the following:
[main] ERROR Cn.lsw.base.log4j2.hello-did It again!
[main] FATAL Cn.lsw.base.log4j2.Hello-I'm FATAL information
See, only >=error log output is coming (this is because log4j has a default configuration, its log level is error, output only console). If I have already defined the log, I change the log level to trace, and the output becomes the following:
19:11:36.941 TRACE Cn.lsw.base.log4j2.Hello hello-entry
19:11:36.951 ERROR Cn.lsw.base.log4j2.Hello Hello-di D It again!
19:11:36.951 Info Cn.lsw.base.log4j2.Hello Hello-I'm info information
19:11:36.951 DEBUG Cn.lsw.base.log4j2.Hello 15 Hello-I'm the debug information
19:11:36.951 WARN Cn.lsw.base.log4j2.Hello Hello-I'm WARN information
19:11:36.952 FATAL Cn.lsw.base.log4j2.Hello Hello-I'm fatal information
19:11:36.952 debug Cn.lsw.base.log4j2.Hello Hello-I'm debug information
19:11:36.952 TRACE Cn.lsw.base.log4j2.Hello hello-exit
All the logs are printed out, you can compare the above code to see.
6. Configuration file:
It's time to get down to business.
I thought log4j 2 should have a default configuration file, but it doesn't seem to be found, and the following configuration file is equivalent to the default configuration (fromhttp://blog.csdn.net/welcome000yy/article/details/7962668):
<?xml version= "1.0" encoding= "UTF-8"?> <configuration status=
"Off" >
<appenders>
<console name= "Console" target= "System_out" >
<patternlayout pattern= "%d{hh:mm:ss. SSS} [%t]%-5level%logger{36}-%msg%n '/>
</Console>
</appenders>
<loggers>
<root level= "Error" >
<appender-ref ref= "Console"/>
</root>
</loggers>
</configuration>
As long as we change the level attribute of Configuration>loggers>root to trace, we can output all the information we just wrote. Believe that the use of log4j is not unfamiliar to this profile, log4j traditional configuration has been. properties file, the form of key-value pairs, which configuration is very bad, but basically we can see from this configuration file log4j 1 shadow, nothing but Appender, Layout and the like, meaning is basically the same.
Here is not prepared to carefully talk about the configuration file, there is no need, you just know some basic configuration on it. I write a few configuration files here, and give some comments and explanations, basically can be used.
First example:
<?xml version= "1.0" encoding= "UTF-8"?> <configuration status=
"Off" >
<appenders>
<console name= "Console" target= "System_out" >
<patternlayout pattern= "%d{yyyy-mm-dd HH:mm:ss. SSS} [%t]%-5level%logger{36}-%msg%n '/>
</Console>
</appenders>
<loggers>
<!--We only let this logger output trace information, the other is the error level-->
<!--
additivity Open, because this logger is also satisfied with root, so will be printed two times.
but the level of root logger is error, why the trace information in bar is also printed two times
-->
<logger name= " Cn.lsw.base.log4j2.Hello "level=" Trace additivity= "false" >
<appender-ref ref= "Console"/>
< /logger>
<root level= "error" >
<