一个简单的日志示例
public class HelloWorld {
public static void main(String[] args) {
Logger logger= LoggerFactory.getLogger(HelloWorld.class);
logger.info("Hello world");
}
}
结果:[main] INFO com.test.HelloWorld - Hello world
SLF4J的典型使用模式
public class HelloWorld {
final Logger logger = LoggerFactory.getLogger(HelloWorld.class);
int count;
int oldCount;
public void setCount(int count) {
this.oldCount = count;
this.count = count;
logger.debug("oldCount {},newCount {}", this.oldCount, this.count);
if (this.count > 50) {
logger.info("the count > 50");
}
}
public static void main(String[] args) {
HelloWorld helloWorld = new HelloWorld();
for (int i = 0; i < 100; i++) {
helloWorld.setCount(i);
}
}
}
[main] INFO com.test.HelloWorld - the count > 50
[main] INFO com.test.HelloWorld - the count > 50
[main] INFO com.test.HelloWorld - the count > 50
[main] INFO com.test.HelloWorld - the count > 50
…