-java-log4j

Source: Internet
Author: User

log4j Introduction: LOG4J is a plug-in tool for managing information log, it is an open source project of Apache, by using log4j, we can control the destination of log information delivery is console, file, GUI component, even socket server, NT Event recorder, UNIX, Syslog daemon, email and so on; we can also control the output format of each log, and by defining the level of each log information, we are able to more carefully control the process of "learning Java to the kaige123.com". The most interesting thing is that these only need to be configured flexibly through a single configuration file, without the need to modify the code of the project, log4j because it is used in Java, so lo4j is the shorthand for the log for Java (Java logs).

log4j Download: log4j can be downloaded on the official website, then import the jar package into the project to be used. Official website: http://logging.apache.org/log4j/1.2/download.html

LOG4J can be configured directly in the MAVEN project through the pom file configuration download, configuration syntax:

<!--https://mvnrepository.com/artifact/log4j/log4j -- <dependency><groupId>Log4j</groupId><artifactid>Log4j</artifactid><version>1.2.17</version> </Dependency>

LO4J Design structure: lo4j The main four structure: Logger, Appender, Layout, log level, lo4j the highest parent class is Logger class, then is the Appender class, this class is used to set the output direction of the log information, Use Appender to specify the daily information output to the console, files, mailboxes, and so on. Appender Common Log information output direction:

Org.apache.log4j.ConsoleAppender (console)

Org.apache.log4j.FileAppender (file)

Org.apache.log4j.DailyRollingFileAppender (Generate 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)

And then layout,layout is the output format used for layout log information, the usual layout layouts: 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 and information string for log information) Org.apache.log4j.TTCCLayout (contains information such as the time, thread, category, etc.) of the log generation

At the end of the log level, the log level is simply a level that is based on the severity of the output log information in your code, with the following types of log levels:

DEBUG specifies fine-grained information events that are most useful for application debug info to specify messages that can highlight information about how applications run at a coarse-grained level

WARN specify a situation that is potentially harmful

Error error events may still allow the application to continue running FATAL to specify very serious error events, which could cause the application to abort

The above level of writing is done in a sort of, why is it done in a sort of? Because for example, if you specify the debug level, the following four levels of information, including debug, will be output, and the specified info level will output only the following three levels of information, including info, and so on. For example:

Debug>>>debug,info,warn,error,fatal

Info>>>info,warn,error,fatal

Warn>>>warn,error,fatal

Error>>>error,fatal

Fatal>>>fatal

Configuration lo4j: The log4j jar is downloaded to the project to start the log4j configuration, the first to create a log4j property file, the file for log4j equivalent to maven pom file, the log related things are configured in this file: Maven Project:

General Engineering:

Write a class that invokes some methods with the object of the logger class to output the log information:

Configuration Properties File:

Configuration Properties File meaning detailed:

The second line of output direction is required. Layout is due to the SetLayout method:

That line of garbled custom output format represents the following meanings:

 %m text content of output  %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{yyyy MMM dd hh:mm:ss,sss}, output similar:2002 years ten months  :921   %l where the output log event occurs, including the class name, the thread that occurred, and the number of rows in the code.   %x used to output the NDC (nested diagnostic context) associated and the thread that generated the Loggi Ng Event  %x used to output the MDC (mapped diagnostic context) associated with the thread that generated the logging E Vent  for specified key

Operation Result:

Select the level of the output log:

Operation Result:

Select the log information output for a package, and add a package name after Log4j.logger:

Operation Result:

You can also select the output of the log information for a particular class:

Operation Result:

You can also customize aliases in a class, and then configure the properties file to output the log information for this class: code example:

Configuration Properties File:

Operation Result:

The log scheme can be configured with multiple names, which require only a different name:

Operation Result:

When a MAVEN project or general engineering package is packaged into a jar, be careful to remove the properties file for the log4j log, as in the case of MAVEN engineering, where the normal project removes the file, the MAVEN project needs to insert a plugin in the lifecycle to help remove it, so that it does not need to be manually removed: Configuration syntax:

<build> <plugins><plugin><groupId>Org.apache.maven.plugins</groupId><artifactId>Maven-jar-plugin</artifactId><version>2.4</version> <configuration><excludes><exclude>**/*. properties</exclude></excludes> </configuration></plugin></plugins></build>

Run MAVEN's package command to see that the target folder and the jar package do not have this log4j property file:

Summarize:

-java-log4j

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.