以下是我们在日常开发中使用的logback.xml文件配置,整理出来分享给大家。
输出日志到控制台
以下配置将使所有的项目日志输出到控制台:
<?xml version="1.0" encoding="UTF-8"?> <configuration> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <layout class="ch.qos.logback.classic.PatternLayout"> <Pattern> %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n </Pattern> </layout> </appender> <logger name="com.mkyong.web" level="debug" additivity="false"> <appender-ref ref="STDOUT" /> </logger> <root level="error"> <appender-ref ref="STDOUT" /> </root> </configuration>
输出日志到文件
以下配置会将日志输出到c:/logs/debug.log文件里,此外,日志文件每天或大于10M时会被存档:
<?xml version="1.0" encoding="UTF-8"?> <configuration> <property name="DEV_HOME" value="c:/logs" /> <appender name="FILE-AUDIT" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${DEV_HOME}/debug.log</file> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <Pattern> %d{yyyy-MM-dd HH:mm:ss} - %msg%n </Pattern> </encoder> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!-- rollover daily --> <fileNamePattern>${DEV_HOME}/archived/debug.%d{yyyy-MM-dd}.%i.log </fileNamePattern> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>10MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> </appender> <logger name="com.mkyong.web" level="debug" additivity="false"> <appender-ref ref="FILE-AUDIT" /> </logger> <root level="error"> <appender-ref ref="FILE-AUDIT" /> </root> </configuration>
输出日志到控制台和文件
<?xml version="1.0" encoding="UTF-8"?> <configuration> <property name="DEV_HOME" value="c:/logs" /> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <layout class="ch.qos.logback.classic.PatternLayout"> <Pattern> %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n </Pattern> </layout> </appender> <appender name="FILE-AUDIT" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${DEV_HOME}/debug.log</file> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <Pattern> %d{yyyy-MM-dd HH:mm:ss} - %msg%n </Pattern> </encoder> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!-- rollover daily --> <fileNamePattern>${DEV_HOME}/archived/debug.%d{yyyy-MM-dd}.%i.log </fileNamePattern> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>10MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> </appender> <appender name="FILE-ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${DEV_HOME}/error.log</file> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <Pattern> %d{yyyy-MM-dd HH:mm:ss} - %msg%n </Pattern> </encoder> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!-- rollover daily --> <fileNamePattern>${DEV_HOME}/archived/error.%d{yyyy-MM-dd}.%i.log </fileNamePattern> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>10MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> </appender> <!-- Send logs to both console and file audit --> <logger name="com.mkyong.web" level="debug" additivity="false"> <appender-ref ref="FILE-AUDIT" /> <appender-ref ref="STDOUT" /> </logger> <root level="error"> <appender-ref ref="FILE-ERROR" /> </root> </configuration>
参考资料:http://logback.qos.ch/manual/appenders.html
SiftingAppender示例
以下配置会将日志输出到分散的文件里,文件名称在运行时通过MDC方式生成:
<?xml version="1.0" encoding="UTF-8"?> <configuration> <property name="DEV_HOME" value="c:/logs" /> <appender name="FILE-THREAD" class="ch.qos.logback.classic.sift.SiftingAppender"> <discriminator> <key>logFileName</key> <defaultValue>head0</defaultValue> </discriminator> <sift> <appender name="FILE-${logFileName}" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${DEV_HOME}/${logFileName}.log</file> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <Pattern> %d{yyyy-MM-dd HH:mm:ss} [%thread] %level %logger{35} - %msg%n </Pattern> </encoder> <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy"> <FileNamePattern>${DEV_HOME}/${logFileName}.%i.log.zip </FileNamePattern> <MinIndex>1</MinIndex> <MaxIndex>10</MaxIndex> </rollingPolicy> <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> <MaxFileSize>10MB</MaxFileSize> </triggeringPolicy> </appender> </sift> </appender> <logger name="com.mkyong.web" level="debug" additivity="false"> <appender-ref ref="FILE-THREAD" /> </logger> <root level="error"> <appender-ref ref="FILE-THREAD" /> </root> </configuration>
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.slf4j.MDC; //... public void run() { MDC.put('logFileName',"head1"); logger.debug("hello"); MDC.remove('logFileName'); }
参考资料:@L_403_1@
编程设置文件名称
可以通过System.setProperty方法设置log.name属性,来编程设置文件名称,代码如下:
<?xml version="1.0" encoding="UTF-8"?> <configuration> <property name="USER_HOME" value="/home/mkyong/ant/logs" /> <appender name="FILE-ENGINE-ERROR" class="ch.qos.logback.core.FileAppender"> <file>${USER_HOME}/${log.name}.error</file> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> <appender name="FILE-ENGINE" class="ch.qos.logback.core.FileAppender"> <file>${USER_HOME}/${log.name}.log</file> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{35} - %msg%n</pattern> </encoder> </appender> <logger name="com.mkyong.core" level="debug" additivity="false"> <appender-ref ref="FILE-ENGINE" /> </logger> <root level="error"> <appender-ref ref="FILE-ENGINE-ERROR" /> </root> </configuration>
TestRunApp.java:
package com.mkyong.core; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class TestRunApp { //No static,else log.name.IS_UNDEFINED.log private final Logger logger = LoggerFactory.getLogger(TestRunApp.class); public static void main(String[] args) { //Set this before the logger start. System.setProperty("log.name","abcdefg"); TestRunApp obj = new TestRunApp(); obj.start(); } private void start() { logger.debug("------ Starting Ant------"); //... } }
结果如下:
Debug log file path /home/mkyong/ant/logs/abcdefg.log Error log file path /home/mkyong/ant/logs/abcdefg.error
结语
Mkyong.com是一个不错的网站,我关注它几年了,作者是一个开发者,该网站主要面向Java开发者,所有示例简单易懂,并且很容易在开发环境测试。