Turn from the purple Wind disorderly write: http://www.blogjava.net/justfly/archive/2014/08/10/416768.html, suggest you go to the same place to study
Written in the previous words:
As the first theme of the "Java Programmer's Way to Practice" blog post, logging, I plan to write according to the following three articles:
- Simple introduction and configuration of Logback
- How to use SLF4J in Java code to write logs and write the main points of the log
- As a programmer, how to analyze and dig log in daily work.
1. Origins
Writing a log in the code is a skill that must be mastered, in addition to the most basic and basic skills of using code to implement functionality. But so far, there have been few articles and books on how to write logs.
1.1 Necessity of writing logs
When I met a bug in QA, I saw two ways of replying: a) Please give me the steps to reproduce and reproduce the data; b) give me the log of the time. In response to the former, it usually takes a lot of time to find out where the problem is, and if it is a module developed by others, it takes more time. In response to the latter, it is generally possible to quickly find the point of the problem and then begin the process of repairing it.
Conceptually: Logs are a fundamental part of a running, maintainable software, and through the logs, we can understand the real-time state of the software system in operation, historical state and abnormal state, etc. A software that doesn't have good logs is a nightmare for everyone.
If you don't want to make trouble for yourself, you should write the diary well.
1.2 Why Choose Logback?
There are two reasons:
- I've been using Logback in recent years.
- Before the LOG4J 2.0 came out a few days ago, Logback's logger used to be more cool
But whether it's logback, log4j, or anything else, there are some tips and suggestions for writing a log in a successor article.
2. How to configure Logback the following is a brief introduction to the Logback configuration, suitable for starting configuration and getting started, suitable for general use, if you want to know more information, it is recommended to look at Logback official documents, written very good. http://logback.qos.ch/manual/introduction.html2.1 Logback Brief Introduction
In short, log4j 1 is popular, found that there are some problems can not be solved, and then out of the logback, on the basis of log4j improve performance, improve the function and so on. But a few days ago came out again Log4j 2, is said to be relative to the logbak to improve the performance of the function.
2.2 About SLF4J and Logback
slf4j (slf4j.org) also known as simple Logging facade for Java, is a generic Logging interface, it tries to unified Logging framework of the World, compatible (log4j 1, java.util.logging and Jakarta Commons Logging) These three most popular Logging frameworks. Logback is the default implementation of SLF4J.
2.3 Dependency Package Import below are examples of Logback 1.1.2 and slf4j1.7.6 versions. In general, you can follow what I said below, if not, you can go through the English document: Http://logback.qos.ch/setup.html. 2.3.1 General Procedures
Maven Edition
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.2</version>
</dependency>
Non-MAVEN version
After downloading the http://logback.qos.ch/dist/logback-1.1.2.zip, there are Logback-core-1.1.2.jar in its root directory and Logback-classic-1.1.2.jar These two jars, there is Slf4j-api-1.7.6.jar this jar under the logback-examples\lib , Add these three jars to your code package path.
2.3.2 Non-standalone run program
If you are doing a Lib or API, then you should not rely on specific SLF4J implementations. So your reliance on logback should be when you run the test code, as shown in the following article:
Maven Edition
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.6</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.2</version>
<scope>test</scope>
</dependency> of course you want to put the version information in the Pom.xml <dependencies> or <dependencyManagement> inside you decide.
Non-MAVEN version
Do not use only logback to pack the jar into your release program when you publish it. (If you feel that you are reading two more times around the mouth)
2.4 Compatible with legacy logging framework
At present, in addition to Logback, the industry is widely used in four other kinds of logging frameworks:
- log4j 1 (HTTP://LOGGING.APACHE.ORG/LOG4J/1.2/)
- log4j 2 (http://logging.apache.org/log4j/2.x/)
- Java.util.logging (http://docs.oracle.com/javase/8/docs/api/java/util/logging/package-summary.html)
- Apache Commons Logging (http://commons.apache.org/proper/commons-logging/)
Log4j 2 because it is just out, the current slf4j compatibility is unknown, for the other three kinds of framework, SLF4J provides compatibility support. Here's how to make Logbak compatible with these frameworks, and you can also read the official note: http://www.slf4j.org/legacy.html
2.4.1 compatible with log4j 1 and Apache Commons Logging
SLF4J support for log4j 1 and Apache Commons logging is provided by LOG4J implementations that implement Commons and Apache logging slf4j interfaces. How to use it
- Removal of references to log4j and Apache Commons logging jar packages
- Introduce the implementation package of the corresponding interface of SLF4J.
2.4.1.1 Remove References If your system is directly using the log4j or Apache Commons logging framework, you can simply remove the reference to them. If you are referring to a third-party package that references log4j or Apache Commons Logging, you can use the <exclusions> tag to remove references to them, as shown below:<dependency>
<groupId>org.springframework.ldap</groupId>
<artifactId>spring-ldap-core</artifactId>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
How do I find out which third-party packages refer to log4j or Apache Commons logging? There are two ways:
- Using the mvn dependency:tree command, as shown, you can see that you need to exclude the Apache Commons from the Org.springframework.ldap:spring-ldap-core A reference to the logging.
- The second way is to use Eclipse's m2e maven plugin. As shown, after opening the pom.xml file, select the Dependency Hierarchy label, and then enter logging or log4j in the filter, on the left The Dependency Hierarchy can be automatically filtered using the right-click menu.
2.4.1.2 maven Import the corresponding SLF4J implementation package
<!--log4j SLF4J implementation--
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>1.7.6</version>
</dependency>
<!--Apache Commons Logging slf4j implemented--
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.6</version>
</dependency>2.4.1.3 Non-MAVEN version import corresponding SLF4J implementation package
Delete the Log4j-1.**.jar and Commons-logging-1.**.jar files directly, download the Http://slf4j.org/dist/slf4j-1.7.6.zip, and compress the Log4j-over-slf4j-1.7.6.jar or (and)Jcl-over-slf4j-1.7.6.jar files are placed in Classpath.
2.4.2 Compatible java.util.logging
SLF4J's jul-to-slf4j module implements a java.util.logging handler, which converts calls to handler into calls to Java.util.logging implementations. Therefore, the following two steps are required:
- Import JUL-TO-SLF4J Module
- Enable the JUL-TO-SLF4J module
2.4.2.1 Import jul-to-slf4j module maven Edition <dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
<version>1.7.6</version>
</dependency>
2.4.2.2 Import jul-to-slf4j Module non-MAVEN version
Download the Http://slf4j.org/dist/slf4j-1.7.6.zip and put the Jul-to-slf4j-1.7.6.jar in the compressed package into the classpath.
2.4.2.3 Enabling the JUL-TO-SLF4J module
Add the following line to the logging.properties :
handlers = Org.slf4j.bridge.SLF4JBridgeHandler
2.5 Logback Profile 2.5.1 logback configuration file name, location, and authoring policy
The Logback will read the configuration file in the Classpath in the following order, and if read to any one, stop looking.
- Logback.groovy This is a configuration file that uses groovy syntax
- Logback-test.xml
- Logback.xml
If none of the above three files can be found in Classpath, the default configuration is used. The default configuration is as follows:
- Output format%d{hh:mm:ss. SSS} [%thread]%-5level%logger{36}-%msg%n
- Output direction: System.out
- Output level: Debug
In my experience, I basically configured the Logback file according to the following policy:
for MAVEN Projects
- Put Logback-test.xmlin the src/test/resources directory, the output of log in this configuration file is to output to console, the code for this application is the debug level, For the other is the info level.
- In the src/main/resources directory, place logback.xml, the output of log in the configuration file is output to a file, the file is rolled and packed daily, the code for this app is the info level, For the other is the warn level.
for non-MAVEN projectsIn the past 10 years, I did not remember to do non-MAVEN projects, so not provide experience, can only say I know, for your reference.
- For WEB projects, Logback.xml is placed in the web-inf/classes directory, and the configuration method is recommended by referring to the above mentioned
- For other projects, place the Classpath root directory at the time the app starts running
2.5.2 Logback Configuration File example describes how the Logback configuration file is written a lot, I will not repeat, you can search for themselves. Here are two articles I think are well written, you can look at:
- http://yuri-liuyu.iteye.com/blog/954038
- Http://www.cnblogs.com/yongze103/archive/2012/05/05/2484753.html
- Of course, the best writing is the Official document: http://logback.qos.ch/manual/introduction.html
The company's project configuration file is not easy to paste out, the following is my own personal project used, add some comments for your reference.
Logback.xml
<?xml version= "1.0" encoding= "UTF-8"?>
<configuration scan= "true" scanperiod= "seconds" >
<!--Appendar Detailed: Http://logback.qos.ch/manual/appenders.html#RollingFileAppender--
<appender name= "FILE" class= "Ch.qos.logback.core.rolling.RollingFileAppender" >
<!--current log file name--
<file>ldap-pwd.log</file>
<rollingpolicy class= "Ch.qos.logback.core.rolling.TimeBasedRollingPolicy" >
<!--non-day log file compression backup to Archive/ldap-pwd.2014-08-10.zip
<fileNamePattern>archive/ldap-pwd.%d{yyyy-MM-dd}.zip</fileNamePattern>
<!--more than 30 days of backup files will be deleted--
<maxHistory>30</maxHistory>
</rollingPolicy>
<layout class= "Ch.qos.logback.classic.PatternLayout" >
<!--format Description: http://logback.qos.ch/manual/layouts.html#ClassicPatternLayout--
<pattern>%d [%thread]%-5level%40logger{40}-%msg%n</pattern>
</layout>
</appender>
<logger name= "cn.justfly.training.logging" level= "info"/>
<root level= "Warn" >
<appender-ref ref= "FILE"/>
</root>
</configuration>
Logback-test.xml
<?xml version= "1.0" encoding= "UTF-8"?>
<configuration>
<appender name= "Console" class= "Ch.qos.logback.core.ConsoleAppender" >
<encoder>
<pattern>%d [%thread]%-5level%40logger{40}-%msg%n</pattern>
</encoder>
</appender>
<logger name= "cn.justfly.training.logging" level= "Debug"/>
<root level= "Info" >
<appender-ref ref= "Console"/>
</root>
</configuration>
Java Programmer's Path to Logging (1/3)-logback configuration (GO)