logback是由log4j创始人设计的又一个开源日志组件。当前分成三个模块:
logback需要logback-core、logback-classic、slf4j-api、logback-access这4个依赖。其中logback-classic已经包含了logback-core和slf4j-api依赖,由于Maven依赖的传递性,所以我们只需导入logback-classic和logback-access依赖即可。
<dependencies>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.3.0-alpha4</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-access</artifactId>
<version>1.3.0-alpha4</version>
</dependency>
</dependencies>
public class BasicConfigurator extends ContextAwareBase implements Configurator {
public BasicConfigurator() {
}
public void configure(LoggerContext lc) {
addInfo("Setting up default configuration.");
ConsoleAppender<ILoggingEvent> ca = new ConsoleAppender<ILoggingEvent>();
ca.setContext(lc);
ca.setName("console");
LayoutWrappingEncoder<ILoggingEvent> encoder = new LayoutWrappingEncoder<ILoggingEvent>();
encoder.setContext(lc);
// same as
// PatternLayout layout = new PatternLayout();
// layout.setPattern("%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n");
TTLLLayout layout = new TTLLLayout();
layout.setContext(lc);
layout.start();
encoder.setLayout(layout);
ca.setEncoder(encoder);
ca.start();
Logger rootLogger = lc.getLogger(Logger.ROOT_LOGGER_NAME);
rootLogger.addAppender(ca);
}
}
<configuration scan="true" scanPeriod="30 seconds">
<!-- 自定义属性,通过${}访问 -->
<property name="filePath" value="/logs/" />
<!-- 输出到控制台 -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} %msg%n</pattern>
</encoder>
</appender>
<!-- 输出到文件 -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 文件路径 -->
<file>${filePath}app.log</file>
<!-- 日志输出格式化 -->
<encoder>
<pattern>%date [%level] [%thread] %logger{80} [%file : %line] %msg%n</pattern>
</encoder>
<!-- 滚动策略 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 每日滚动 -->
<fileNamePattern>${filePath}app.log%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- 将30天的日志总大小限制在3GB内 -->
<maxHistory>30</maxHistory>
<totalSizeCap>3GB</totalSizeCap>
</rollingPolicy>
</appender>
<!-- 基于尺寸和时间的滚动策略 -->
<appender name="ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${filePath}other.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${filePath}other.log%d{yyyy-MM-dd}%i.log</fileNamePattern>
<!-- 每个日志文件最多100MB,保存60天的历史记录,总大小不超过20GB -->
<maxFileSize>1KB</maxFileSize>
<maxHistory>60</maxHistory>
<totalSizeCap>20GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%date [%level] [%thread] %logger{80} [%file : %line] %msg%n</pattern>
</encoder>
</appender>
<!-- name属性指定包名或者完全限定类名 -->
<logger name="service.OtherService" level="DEBUG">
<appender-ref ref="ROLLING" />
</logger>
<!-- 根logger -->
<root level="DEBUG">
<!-- 配置输出源 -->
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
</configuration>
测试:
public class HelloService {
private final static Logger logger = LoggerFactory.getLogger(HelloService.class);
public static void main(String[] args) {
//根据logback.xml中配置的日志级别,TRACE级别的日志将不会输出,只会输出DEBUG及以上级别的日志。
//TRACE < DEBUG < INFO < WARN < ERROR
logger.trace("---------------trace---------------");
logger.debug("---------------debug---------------");
logger.info("---------------info---------------");
logger.warn("---------------warn---------------");
logger.error("---------------error---------------");
}
}
logger.debug("我是" + name + ",我今年" + age + "岁,很高兴认识你!");//普通方式
logger.debug("我是{},我今年{}岁,很高兴认识你!", name, age);//占位符方式(推荐)
当debug日志被禁用时,普通方式中,参数依然会被构造拼接,而在占位符方式中,参数不会进行构造拼接。
将元素的scan属性设置为true,logback会定时的扫描配置文件,如果配置文件发生了更改,将自动重新加载配置文件。默认每分钟扫描一次,可以设置scanPeriod属性来指定扫描间隔。
<configuration scan="true" scanPeriod="30 seconds" >
...
</configuration>