Slf4j-02-slf4j 与 logback 整合
2018年8月27日大约 3 分钟
目的
整合 slf4j 与 logback。
快速开始
maven 依赖
添加依赖包logback使用需要和slf4j一起使用,所以总共需要添加依赖的包有slf4j-api
logback使用需要和slf4j一起使用,所以总共需要添加依赖的包有slf4j-api.jar,logback-core.jar,logback-classic.jar,logback-access.jar这个暂时用不到所以不添加依赖了,maven配置
UTF-8
1.1.7
1.7.21
org.slf4j
slf4j-api
${slf4j.version}
compile
ch.qos.logback
logback-core
${logback.version}
ch.qos.logback
logback-classic
${logback.version}
logback.xml 配置
%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
${LOG_HOME}/TestWeb.log.%d{yyyy-MM-dd}.log
30
%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
10MB
或者一个更加简单的 logback.xml
%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
测试代码
package com.github.houbb.zk.learn;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LoggerTest {
private static Logger logger = LoggerFactory.getLogger(LoggerTest.class);
public static void main(String[] args) {
logger.debug("debug:....");
logger.info("info...");
logger.error("error...");
}
}
日志如下:
17:07:02,874 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
17:07:02,875 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
17:07:02,876 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/D:/code/zk-learn/target/classes/logback.xml]
17:07:03,164 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set
17:07:03,202 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
17:07:03,219 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT]
17:07:03,378 |-WARN in ch.qos.logback.core.ConsoleAppender[STDOUT] - This appender no longer admits a layout as a sub-component, set an encoder instead.
17:07:03,378 |-WARN in ch.qos.logback.core.ConsoleAppender[STDOUT] - To ensure compatibility, wrapping your layout in LayoutWrappingEncoder.
17:07:03,378 |-WARN in ch.qos.logback.core.ConsoleAppender[STDOUT] - See also http://logback.qos.ch/codes.html#layoutInsteadOfEncoder for details
17:07:03,381 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [com.github.houbb.zk.learn] to TRACE
17:07:03,381 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to DEBUG
17:07:03,382 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[ROOT]
17:07:03,385 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
17:07:03,389 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@4ca8195f - Registering current configuration as safe fallback point
17:07:03.398 [main] DEBUG com.github.houbb.zk.learn.LoggerTest - debug:....
17:07:03.405 [main] INFO com.github.houbb.zk.learn.LoggerTest - info...
17:07:03.406 [main] ERROR com.github.houbb.zk.learn.LoggerTest - error...
参考资料
贡献者
binbin.hou