zoukankan      html  css  js  c++  java
  • use slf4j format pattern like dot net string format,it seems more elgeant

    SLF4J Manual

    SLF4J user manual

    The Simple Logging Facade for Java or (SLF4J) serves as a simple facade or abstraction for various logging frameworks, such as java.util.logging, log4j and logback. SLF4J allows the end-user to plug in the desired logging framework at deployment time. Note that SLF4J-enabling your library/application implies the addition of only a single mandatory dependency, namely slf4j-api-1.6.4.jar.

    SINCE 1.6.0 If no binding is found on the class path, then SLF4J will default to a no-operation implementation.

    Hello World

    As customary in programming tradition, here is an example illustrating the simplest way to output "Hello world" using SLF4J. It begins by getting a logger with the name "HelloWorld". This logger is in turn used to log the message "Hello World".

    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;

    public class HelloWorld {
     
    public static void main(String[] args) {
       
    Logger logger = LoggerFactory.getLogger(HelloWorld.class);
        logger
    .info("Hello World");
     
    }
    }

    To run this example, you first need to download the slf4j distribution, and then to unpack it. Once that is done, add the file slf4j-api-1.6.4.jar to your class path.

    Compiling and running HelloWorld will result in the following output being printed on the console.

    SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
    SLF4J: Defaulting to no-operation (NOP) logger implementation
    SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

    This warning is printed because no slf4j binding could be found on your class path.

    The warning will disappear as soon as you add a binding to your class path. Assuming you add slf4j-simple-1.6.4.jar so that your class path contains:

    • slf4j-api-1.6.4.jar
    • slf4j-simple-1.6.4.jar

    Compiling and running HelloWorld will now result in the following output on the console.

    0 [main] INFO HelloWorld - Hello World

    Typical usage pattern

    The sample code below illustrates the typical usage pattern for SLF4J. Note the use of {}-placeholders on line 15. See the question "What is the fastest way of logging?" in the FAQ for more details.

     

     1: import org.slf4j.Logger;
     
    2: import org.slf4j.LoggerFactory;
     
    3:
     
    4: public class Wombat {
     
    5:  
     
    6:   final Logger logger = LoggerFactory.getLogger(Wombat.class);
     
    7:   Integer t;
     
    8:   Integer oldT;
     
    9:
    10:   public void setTemperature(Integer temperature) {
    11:    
    12:     oldT = t;        
    13:     t = temperature;
    14:
    15:     logger.debug("Temperature set to {}. Old temperature was {}.", t, oldT);
    16:
    17:     if(temperature.intValue() > 50) {
    18:       logger.info("Temperature has risen above 50 degrees.");
    19:     }
    20:   }
    21: }

  • 相关阅读:
    SD卡测试
    测试人员可能会遇到的问题
    HDU 1024 Max Sum Plus Plus
    HDU 1176 免费馅饼
    HDU 1257 最少拦截系统
    HDU 1087 Super Jumping! Jumping! Jumping!
    poj 1328 Radar Installation
    poj 1753 Flip Game
    HDU 1003 Max Sum
    HDU 5592 ZYB's Premutation(BestCoder Round #65 C)
  • 原文地址:https://www.cnblogs.com/lexus/p/2379986.html
Copyright © 2011-2022 走看看