validating - Logging

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wangbingfengf98/article/details/88957101

SLF4J() provides multiple levels of reporting. Below example shows them all, in increasing order of "seriousness":

// validating/SLF4JLevels.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.

import org.slf4j.*;

public class SLF4JLevels {
  private static Logger log = LoggerFactory.getLogger(SLF4JLevels.class);

  public static void main(String[] args) {
    log.trace("Hello");
    log.debug("Logging");
    log.info("Using");
    log.warn("the SLF4J");
    log.error("Facade");
    float f = 1.1f + 2.2f;
    System.out.println("f:" + f);
  }
}
/* My Output TRACE level:
2019-04-01T20:50:40.249
[main] TRACE SLF4JLevels - Hello
2019-04-01T20:50:40.251
[main] DEBUG SLF4JLevels - Logging
2019-04-01T20:50:40.251
[main] INFO  SLF4JLevels - Using
2019-04-01T20:50:40.252
[main] WARN  SLF4JLevels - the SLF4J
2019-04-01T20:50:40.252
[main] ERROR SLF4JLevels - Facade
f:3.3000002
*/
/* My Output info level:
2019-04-01T20:52:42.318
[main] INFO  SLF4JLevels - Using
2019-04-01T20:52:42.320
[main] WARN  SLF4JLevels - the SLF4J
2019-04-01T20:52:42.320
[main] ERROR SLF4JLevels - Facade
*/

programmatic reload of logback configurations 

LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
context.reset();
JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(context);
configurator.doConfigure(this.getClass().getResourceAsStream("/logback.xml"));

references:

1. On Java 8 - Bruce Eckel

2. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/validating/SLF4JLevels.java

3. https://javachannel.org/posts/programmatic-reload-of-logback-configurations/

猜你喜欢

转载自blog.csdn.net/wangbingfengf98/article/details/88957101
今日推荐