# download url
http://www.slf4j.org/download.html
http://logback.qos.ch/download.html
# 依赖 jar
slf4j-api-1.7.5.jar
logback-core-1.0.13.jar
logback-classic-1.0.13.jar
# 日志级别(大小写不敏感)
TRACE < DEBUG < INFO < WARN < ERROR
# Java 代码示例
package org.demo.logtest; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class LogbackTest { private static Logger logger = LoggerFactory.getLogger(LogbackTest.class); /** * @param args */ public static void main(String[] args) { logger.info("MethodIn: main"); // ... logger.info("MethodOut: main"); } }
# logback 配置文件 src/logback.xml 示例
# 将日志输出到控制台
<?xml version="1.0" encoding="UTF-8" ?> <configuration scan="true" scanPeriod="30 seconds"> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg [%file:%line] %n</pattern> </encoder> </appender> <logger name="org.demo.logtest" level="DEBUG" /> <root level="ERROR"> <appender-ref ref="STDOUT" /> </root> </configuration>
# 将日志输出到文件, 并按大小滚动
<?xml version="1.0" encoding="UTF-8" ?> <configuration scan="true" scanPeriod="30 seconds"> <timestamp key="startTime" datePattern="yyyyMMdd'T'HHmmss"/> <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>myApp-${startTime}.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy"> <fileNamePattern>myApp.%i.log.zip</fileNamePattern> <minIndex>1</minIndex> <maxIndex>3</maxIndex> </rollingPolicy> <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> <maxFileSize>2KB</maxFileSize> </triggeringPolicy> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg [%file:%line] %n</pattern> </encoder> </appender> <logger name="org.demo.logtest" level="DEBUG" /> <root level="ERROR"> <appender-ref ref="FILE" /> </root> </configuration># 将日志输出到文件, 并按时间滚动
<?xml version="1.0" encoding="UTF-8" ?> <configuration scan="true" scanPeriod="30 seconds"> <timestamp key="startTime" datePattern="yyyyMMdd'T'HHmmss"/> <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>myApp-${startTime}.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>myApp.%d{yyyy-MM-dd_HH-mm}.log.zip</fileNamePattern> <maxHistory>5</maxHistory> </rollingPolicy> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg [%file:%line] %n</pattern> </encoder> </appender> <logger name="org.demo.logtest" level="DEBUG" /> <root level="ERROR"> <appender-ref ref="FILE" /> </root> </configuration># 将日志输出到文件, 并按时间和大小滚动
<?xml version="1.0" encoding="UTF-8" ?> <configuration scan="true" scanPeriod="30 seconds"> <timestamp key="startTime" datePattern="yyyyMMdd'T'HHmmss"/> <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>myApp-${startTime}.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>myApp.%d{yyyy-MM-dd_HH-mm}.%i.log.zip</fileNamePattern> <maxHistory>5</maxHistory> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>2KB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg [%file:%line] %n</pattern> </encoder> </appender> <logger name="org.demo.logtest" level="DEBUG" /> <root level="ERROR"> <appender-ref ref="FILE" /> </root> </configuration>
# 多个 appender 以及日志的叠加性控制
# http://logback.qos.ch/manual/configuration.html#overrridingCumulativity
<logger name="chapters.configuration.Foo" additivity="false">
<appender-ref ref="FILE" />
</logger>
# 定义变量
在 logback.xml 中可以通过 <property> 元素定义一个变量,使用时通过 ${propertyName} 即可,例如下面配置文件中定义的 log.dir :
<?xml version="1.0" encoding="UTF-8" ?> <configuration scan="true" scanPeriod="30 seconds" debug="true"> <property name="log.dir" value="${catalina.home}" /> <timestamp key="startTime" datePattern="yyyyMMdd'T'HHmmss"/> <appender name="FILE" class="ch.qos.logback.core.FileAppender"> <file>${log.dir}/myApp-${startTime}.log</file> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> <root level="DEBUG"> <appender-ref ref="FILE" /> </root> </configuration># 注意:上面的 ${catalina.home} 引用的是一个JVM系统属性,这个在JVM启动的时候通过参数 -Dcatalina.home=D:/Temp 进行设置(tomcat启动时会自动将catalina.home设置为tomcat所在的目录);通过${propertyName}的方式不仅可以引用一个变量、一个系统属性,还可以直接引用一个操作系统的环境变量(例如windows里面的系统属性 -> 高级 -> 环境变量 -> 系统变量);
# 引用外部配置文件
# http://logback.qos.ch/manual/configuration.html#variableSubstitution3
<property file="${catalina.base}/webapps/myApp/WEB-INF/config/myAppConfig.properties" />
# 转换字符
http://logback.qos.ch/manual/layouts.html#conversionWord
# maxFileSize 的单位为KB, MB, GB, 默认是字节
例如: 5MB == 5000KB == 5000000
参见:http://logback.qos.ch/manual/appenders.html#SizeBasedTriggeringPolicy
# 文件名中的特殊字符
文件名不能包含下列任何字符:\/:*?"<>|
# pattern 中的特殊字符:()
圆括号在 logback 中用来分组, 如果需要将 () 当成普通字符对待, 则需要转义字符 '\' 处理, 例如 \([%thread]\)
参见:http://logback.qos.ch/manual/layouts.html#Parentheses
# 相关链接
日志组件logback的介绍及配置使用方法(一)
http://www.cnblogs.com/yuanermen/archive/2012/02/13/2348942.html
Logback学习笔记1
http://www.cnblogs.com/luowei010101/archive/2012/01/04/2312438.html