# 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>
# 转换字符
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