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: }

  • 相关阅读:
    如何解决chrome和chromedriver版本不匹配
    RobotFramework 实战1——数据检查自动化
    RobotFramework 中的循环语句:FOR IN RANGE
    robotframework 获取昨日(get time关键词的用法)
    大数据用户画像方法与实践(干货 转帖)
    Scrapy实战篇(九)之爬取链家网天津租房数据
    Scrapy实战篇(八)之爬取教育部高校名单抓取和分析
    Scrapy实战篇(七)之爬取爱基金网站基金业绩数据
    Selenium常用方法
    Selenium之动作链(ActionChains)
  • 原文地址:https://www.cnblogs.com/lexus/p/2379986.html
Copyright © 2011-2022 走看看