Java programmer cultivation-Logging (1/3)-Logback Configuration

Source: Internet
Author: User

Java programmer cultivation-Logging (1/3)-Logback Configuration

Preface:

As the first topic of the blog "Java programmer Cultivation", I plan to write the following three articles:

A brief introduction and configuration of Logback how to use SLF4J in Java code to write logs and the key points of writing logs as a programmer, how to analyze and mine logs in daily work.

PS: the default directory is incorrect. I checked it carefully. My h1, h2, h3, and h4 are correct.

1. Origin

Writing logs in the Code is a basic and basic skill apart from implementing functions using the code. It is a required skill. But so far, there are not many articles and books on how to log.

1.1 necessity of log writing

When I encountered a bug raised by QA, I had seen two ways to reply: a) please reproduce the steps and data for me; B) Give me the logs at that time. It usually takes a lot of time to locate the problem. If it is a module developed by someone else, more time is spent. If you reply to the latter, you can quickly find the problem and start the repair process.

In terms of concept: log is a basic component of a runable and maintainable software. With logs, we can understand the real-time status of the software system during running, historical Status and exception status. A software without good logs is everyone's nightmare.

If you don't want to bother yourself, you should write the logs well.

1.2 Why Logback?

There are two reasons:

I have used Logback in recent years. Before Log4j 2.0 came out a few days ago, logback's Logger was quite easy to use, but whether it's Logback, Log4j or other Log frameworks, some tips and suggestions on Log writing in subsequent articles are applicable.

2. the following describes how to configure Logback. It is suitable for starting configuration and getting started. It is suitable for general use. If you want to learn more, we recommend that you read the official Logback documentation and write it well. Brief Introduction to http://logback.qos.ch/manual/introduction.html2.1 Logback

In short, Log4j 1 is popular, and some problems cannot be solved. Therefore, Logback is released, which improves the performance and functions based on Log4j. However, Log4j 2 came out a few days ago. It is said that compared with Logbak, Log4j 2 has improved its performance and functionality.

2.2 about SLF4J and Logback

SLF4J(Slf4j.org), also known as Simple Logging Facade for Java, is a common logging interface. It tries to unify the world of Logging frameworks and is compatible with (Log4j 1, java. util. logging and Jakarta Commons Logging) these three most popular Logging frameworks. Logback is the default Implementation of SLF4J.

2.3 The following example shows how to import a dependency package: Logback 1.1.2 and slf4j1.7.6. Generally, you can follow the instructions below. If not, you can refer to the English document http://logback.qos.ch/setup.html. 2.3.1 General Program

Maven version

     
  
   ch.qos.logback
      logback-classic    
  
   1.1.2
  
 

Non-Maven version

Download and download http://logback.qos.ch/dist/logback-1.1.2.zip.Logback-core-1.1.2.jarAndLogback-classic-1.1.2.jarThese two Jar files areLogback-examples \ libUnderSlf4j-api-1.7.6.jarAdd these three jar files to your code package path.

2.3.2 non-independent programs

If you are using a Lib or API, you should not rely on the specific slf4j implementation. Therefore, your dependency on logback should be when running the test code. The specific implementation method is as follows:

Maven version

 
  
   org.slf4j
  slf4j-api
  
   1.7.6
  
 
 
  
   ch.qos.logback
  logback-classic
  
   1.1.2
  
  
   test
  
 

Of course, you want to put the version information in pom. xml. Or You can decide on your own.

Non-Maven version

During the release, do not package only Logback to jar in your release program. (If you think you want to bypass it and read it again)

2.4 compatible with the legacy Logging framework

Currently, in addition to Logback, the following four types of Logging frameworks are widely used in the industry:

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)

Because Log4j 2 was just launched, its compatibility with SLF4J is unknown. SLF4J provides compatibility support for the other three frameworks. The following describes how to make Logbak compatible with these frameworks, 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 supports Log4J 1 and Apache commons Logging by providing SLF4J implementation for the Log4j and Apache commons Logging interfaces. The usage is

Introduce the implementation package of the corresponding SLF4J interface to reference the Jar packages of Log4J and Apache commons Logging.

2.4.1.1 remove references if your system directly uses the Log4j or Apache commons Logging framework, you can simply remove the references. If the third-party package you reference references Log4j or Apache commons Logging, you can use Label removes references to them, as shown below:
      
     
      org.springframework.ldap
       spring-ldap-core  
       
      
         commons-logging  
       
        commons-logging
         
        
     
    

How to find which third-party packages reference Log4j or Apache commons Logging? There are two methods: Use Mvn dependency: treeCommand, as shown in, we can see that the reference to Apache commons Logging needs to be removed from org. springframework. ldap: spring-ldap-core. Release/qGjyOfPws28y/nKvqOstPK/qjxzdHJvbmc + release + 1tDKudPD09K8/LLLtaW + zb/release = "http://www.2cto.com/uploadfile/Collfiles/20140810/20140810092705184.png" alt = "\">
2.4.1.2 Maven import the corresponding SLF4J Implementation Package

    
    
     
      
Org. slf4j
     Log4j-over-slf4j
     
      
1.7.6
     
    
    
    
     
      
Org. slf4j
     Jcl-over-slf4j
     
      
1.7.6
     
    

2.4.1.3 import the corresponding SLF4J implementation package for non-Maven versions

Directly Delete the log4j-1. **. jar and commons-logging-1. **. jar files, download http://slf4j.org/dist/slf4j-1.7.6.zipLog4j-over-slf4j-1.7.6.jarOr (and)Jcl-over-slf4j-1.7.6.jarPut the file in classpath.

2.4.2 compatible with java. util. logging

The jul-to-slf4j module of SLF4J implements a java. util. logging handler, which converts the call to java. util. logging to the call to SLF4J implementation. Therefore, the following two steps are required:

Import jul-to-slf4j module enable jul-to-slf4j Module

2.4.2.1 import jul-to-slf4j module maven

    
     
      org.slf4j
     jul-to-slf4j
     
      1.7.6
     
    
2.4.2.2 import jul-to-slf4j module non-Maven version

Download http://slf4j.org/dist/slf4j-1.7.6.zipto download the compressed packageJul-to-slf4j-1.7.6.jarPut it in classpath.

2.4.2.3 enable jul-to-slf4j Module

InLogging. propertiesAdd the following line:

handlers = org.slf4j.bridge.SLF4JBridgeHandler
2.5 Logback configuration file overview 2.5.1 Logback configuration file name, location, and write policy

Logback reads the configuration file in classpath in the following order. If any of the files is read, Logback stops searching.

Logback. groovy: the configuration file logback.-test. xml logback. xml using groovy syntax. 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: In my experience, Debug configures the Logback file according to the following policies: For Maven ProjectsIn Src/test/resourcesDirectory Logback-test.xmlThe log output in this configuration file is to the output to the console, the code for this application is debug level, and the other is info level. In Src/main/resourcesDirectory Logback. xmlThe log output in this configuration file is output to the file. The file is compressed and packaged on a daily basis. The code for this application is info-level, and the other is warn-level. For non-Maven ProjectsI have never worked on any non-Maven project in the past 10 years, so I can only talk about what I know for your reference. For web projects, logback. xml is placed in WEB-INF/classesFor more information about how to configure Logback, see 2.5.2 examples of Logback configuration files in the classpath root directory when the application is started, I will not repeat it. You can search for it by yourself. Below are two I think write well, you can take a look: http://yuri-liuyu.iteye.com/blog/954038
Http://www.cnblogs.com/yongze103/archive/2012/05/05/2484753.html
Of course, the best written is the official documentation: http://logback.qos.ch/manual/introduction.html
The configuration file of the Company project is not easy to post out. below is what is used in my personal project. Some annotations are added for your reference. Logback. xml
    
    
         
     
     
      
Ldap-pwd.log
     
     
      
      
       
Archive/ldap-pwd.mongodw.yyyy-mm-ddw..zip
      
      
      
       
30
      
     
     
      
      
       
% D [% thread] %-5 level % 40 logger {40}-% msg % n
      
     
     
     
    
Logback-test.xml
    
    
     
      
       %d [%thread] %-5level %40logger{40} - %msg%n
      
     
     
     
    



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.