Slf4j tutorial (freely switch among various logs)

Source: Internet
Author: User

Address: http://www.blogjava.net/dreamstone/archive/2007/07/09/128993.html

 

I. Introduction:
Simple logging facade for Java slf4j provides a simple and unified Log service for various loging APIs.
To enable end users to configure their desired loging APIs implementation during deployment. You can use the logging API
Select the loging APIs that implements slf4j directly, such as nlog4j and simplelogger. It can also be implemented through the API provided by slf4j
To develop the corresponding adapters such as log4jloggeradapter and jdk14loggeradapter. The slf4j release contains several
Jar packages, such as slf4j-nop.jar, slf4j-simple.jar, slf4j-log4j12.jar, slf4j-log4j13.jar,
The slf4j-jdk14.jar and slf4j-jcl.jar can use these jar files to separate the compilation period from the specific implementation. Or yes.
Flexible Switching
Ii. Official Site
Official Website: http://www.slf4j.org/manual.html
Iii. Why slf4j?
Various logs may be used during the development process. Each log has a different style and layout. If you want to switch flexibly, slf4j is better.
Select.
4. How to Use slf4j
The following program is a classic method to use slf4j.

 

import org.slf4j.Logger;import org.slf4j.LoggerFactory;public class Wombat {    final Logger logger = LoggerFactory.getLogger(Wombat.class);    Integer t;    Integer oldT;    public void setTemperature(Integer temperature) {        oldT = t;        t = temperature;        logger.error("Temperature set to {}. Old temperature was {}.", t, oldT);        if (temperature.intValue() > 50) {            logger.info("Temperature has risen above 50 degrees.");        }    }    public static void main(String[] args) {        Wombat wombat = new Wombat();        wombat.setTemperature(1);        wombat.setTemperature(55);    }}

The following describes how to run the above program.
1, compile the above program, need to add slf4j-api-1.4.1.jar file in classpath
2, at runtime, you need to add slf4j-simple-1.4.1.jar to classpath
Run the command to obtain the result:
----------------------------
0 [main] Error Wombat-temperature set to 1. Old temperature was null.
0 [main] Error Wombat-temperature set to 55. Old temperature was 1.
0 [main] info Wombat-temperature has risen above 50 degrees.
This is a simple log style,

3, switch: If you want to switch to the jdk14 log style, just need to put slf4j-simple-1.4.1.jar
Remove from classpath and add slj4j-jdk14-1.4.1.jar to classpath
The running result is as follows:
---------------------------------------------------
2007-7-9 10:40:15 wombat settemperature
Severe: temperature set to 1. Old temperature was null.
2007-7-9 10:40:16 wombat settemperature
Severe: temperature set to 55. Old temperature was 1.
2007-7-9 10:40:16 wombat settemperature
Information: temperature has risen abve 50 degrees.
It has become the log style of JDK 14.
4. Switch to log4j again
Also remove slj4j-jdk14-1.4.1.jar, add slf4j-log4j12-1.4.1.jar, and add log4j-1.2.x.jar
Add log4j. properties. The displayed result is as follows:
---------------------------------------
10:42:27, 328 error wombat: temperature set to 1. Old temperature was null.
10:42:27, 328 error wombat: temperature set to 55. Old temperature was 1.
10:42:27, 328 info wombat: temperature has risen abve 50 degrees.
In different styles, you only need to switch the class library during the deployment period, and it has nothing to do with development.

 

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.