参考: Simple Logging Facade for Java (SLF4J)SLF4J user manualhttp://www.slf4j.org/manual.html SLF4J warning or error messages and their meanings |
(1)下载组件,添加jar包
(2)配置
(3)使用API
1、下载组件,添加jar包
组件名称 | jar包 | 下载地址 |
slf4j-1.7.21.zip | slf4j-api-1.7.21.jar | http://www.slf4j.org/download.html |
slf4j-log4j12-1.7.21.jar | ||
log4j-1.2.17.zip | log4j-1.2.17.jar | http://logging.apache.org/log4j/1.2/download.html |
2、配置
在项目的src目录下添加log4j.properties
log4j.rootLogger=debug, console, file log4j.appender.console=org.apache.log4j.ConsoleAppender log4j.appender.console.layout=org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern=%d %p %c.%M() -%m%n log4j.appender.file=org.apache.log4j.RollingFileAppender log4j.appender.file.File= ./logs/MyLog.log log4j.appender.file.MaxFileSize=5KB log4j.appender.file.MaxBackupIndex=100 log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d %p %c.%M() -%m%n
3、使用API
package com.rk.test; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class HelloWorld { @Test public void test() { Logger logger = LoggerFactory.getLogger(HelloWorld.class); logger.info("Hello World"); logger.info("你好,{},欢迎来到{}", "小明","南极"); } }
4、常见的错误
一个常见的错误提示是:
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-log4j12-1.7.21.jar so that your class path contains:
|
这个警告提示在classpath中没有找到slf4j binding。这里所说的slf4j binding是指slf4j-log4j12-1.7.21.jar。
SLF4J supports various logging frameworks. The SLF4J distribution ships with several jar files referred to as "SLF4J bindings", with each binding corresponding to a supported framework.
|
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
查看地址:http://www.slf4j.org/codes.html#StaticLoggerBinder
Failed to load class
|