Java Log Learning Notes

Source: Internet
Author: User
Tags log4j

A Log family

Log4j was very powerful at first, and before the JDK had its own log system, Apache tried to make log4j a part of Java and somehow failed, and sun used its own weak log system. To be compatible with each log system, Apache has introduced commons-logging to encapsulate multiple log systems. Commons-logging itself is not a log system, it is just a log system butler to decide which log system to call. Because it uses ClassLoader to find and load the underlying log library, a framework like OSGi does not work properly because OSGi's different plugins use their own classloader. This mechanism of OSGI ensures that plug-ins are independent of each other, but it makes Apache common-logging unable to work. For this reason people created the slf4j, which statically binds the real log library at compile time, so it can be used in OSGi. So, commons-logging and slf4j are the same kind of things, there is competition between them. The founder of Log4j again a logback, performance beyond the log4j, but log4j 2.0 seems to overtake Logback. LOG4J 2.0 changes a lot with respect to the log4j, the most prominent thing is that the configuration file discards the properties, using XML or JSON instead. Although Commons-logging and slf4j are only equivalent to a large butler, but they feel like a separate log system, in addition to the use of third-party log system, the two big Butler also has its own simple log system, generally only for testing purposes. As a result, many jar packages have been written as bridges between these log systems.

Download from the Web log library, the directory has a lot of jar files, to have the choice of use, do not import all, heavy error, light occupies a space in vain. It is recommended to use the log4j 2.0+slf4j combination, log4j2.0 includes API and core two packages, in order to be compatible with past log4j, There is a compatibility package log4j-1.2-api-2.5.jar;slf4j core only includes a package API, in order to make it with the LOG4J 2.0, need to slf4j to log4j Bridge package, if it comes with its own simple log system collocation will be imported easy package.

log4j2.0 Typical code:

Import Org.apache.logging.log4j.Level;

Import Org.apache.logging.log4j.LogManager;

Import Org.apache.logging.log4j.Logger;

static Logger Logger = Logmanager.getlogger (Hello.class.getName ());

Typical code for LOG4J:

Import Org.apache.log4j.Logger;

private static Logger Logger = Logger.getlogger (Test.class);

Typical configuration files for log4j2.0:

<?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= "Trace" >

      <appender-ref ref= "Console"/>

    </root>

  </loggers>

</configuration>

Two log4j System Three Parts
The log4j consists of three important components: the logger (loggers), the output (appenders), and the log formatter (layout).

Level is the priority of logging, which is divided into off, FATAL, ERROR, WARN, INFO, DEBUG, all, or levels you define. LOG4J recommends using only four levels, with the priority from high to low being error, WARN, INFO, DEBUG. By defining the level here, you can control the switch to the appropriate level of log information in your application. For example, if the info level is defined here, the log information for all debug levels in the application will not be printed.

Appendername refers to where the B log information is exported. LOG4J offers the following types of Appender:

Org.apache.log4j.ConsoleAppender (console),

Org.apache.log4j.FileAppender (file),

Org.apache.log4j.DailyRollingFileAppender (generates a log file every day),

Org.apache.log4j.RollingFileAppender (creates a new file when the file size reaches the specified size),

Org.apache.log4j.WriterAppender (send log information in stream format to any specified location)

LOG4J offers the following types of layout:

Org.apache.log4j.HTMLLayout (Layout in HTML table Form),

Org.apache.log4j.PatternLayout (flexibility to specify layout mode),

Org.apache.log4j.SimpleLayout (contains the level of log information and the information string),

Org.apache.log4j.TTCCLayout (contains information about the time, thread, category, etc.) of the log

Three Output format

%M the message specified in the output code

%p output priority, i.e. Debug,info,warn,error,fatal

%r output the number of milliseconds that the log information is consumed from the application boot to output

%c output belongs to the class, which is usually the full name of the class

%t output The name of the thread that generated the log event

%n output a carriage return newline character, Windows platform is "RN", UNIX platform is "n"

%d the date or time of the output log time, the default format is ISO8601, can also be specified after the format, such as:%d{yyy MMM dd hh:mm:ss,sss}, output similar: October 18, 2002 22:10:28,921

%l where the output log event occurs, including the class name, the thread that occurred, and the number of rows in the code.

Four About the configuration of LOG4J 2.0

Under the log4j core package, extracting the jar package is a visible XSD and DTD file that defines some of the configuration file formats, and the official web sample does not write the DOCTYPE attribute, which prevents the automatic prompting feature from being used. On this question, see my blog: http://www.cnblogs.com/weidiao/p/5374824.html

Java Log Learning Notes

Related Article

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.