Logback是由log4j创始人Ceki Gülcü设计的又一个开源日志组件。
logback.xml
- <?xml version="1.0" encoding="UTF-8"?> <!-- Reference Manual http://logback.qos.ch/manual/index.html -->
- <configuration>
- <!-- LEVEL:TRACE, DEBUG, INFO, WARN, ERROR, ALL 和 OFF -->
- <!-- 控制台输出 -->
- <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
- <!-- encoder 可以指定字符集,对于中文输出有意义 -->
- <encoder charset="UTF-8">
- <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
- </encoder>
- </appender>
- <!-- 每日最多生成10个文件,可以通过这些文件查看系统工作状态 -->
- <!--
- <appender name="RollingFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
- <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
- <level>TRACE</level>
- </filter>
- <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
- <fileNamePattern>${log.root}/WEB-INF/logs/trace-%d{yyyy-MM-dd}.log</fileNamePattern>
- <maxHistory>10</maxHistory>
- </rollingPolicy>
- <encoder>
- <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
- </encoder>
- </appender>
- -->
- <!-- 下面这段注释的代码可以在日志文件超过 5MB 时进行归档,并且归档文件后缀只要是 .zip 或 .gz 就会自动
- 压缩日志归档 -->
- <appender name="InfoFileZip" class="ch.qos.logback.core.rolling.RollingFileAppender">
- <file>${log.root}/WEB-INF/logs/info.log</file>
- <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
- <level>INFO</level>
- </filter>
- <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
- <fileNamePattern>${log.root}/WEB-INF/logs/bak/info.%i.log.zip</fileNamePattern>
- <minIndex>1</minIndex>
- <maxIndex>10</maxIndex>
- </rollingPolicy>
- <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
- <maxFileSize>10MB</maxFileSize>
- </triggeringPolicy>
- <encoder>
- <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
- </encoder>
- </appender>
- <!-- 时间滚动输出 level为 ERROR 日志 -->
- <appender name="ErrorFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
- <filter class="ch.qos.logback.classic.filter.LevelFilter">
- <level>ERROR</level>
- <OnMismatch>DENY</OnMismatch>
- <OnMatch>ACCEPT</OnMatch>
- </filter>
- <File>${log.root}/WEB-INF/logs/error.log</File>
- <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
- <FileNamePattern>${log.root}/WEB-INF/logs/error.log.%i.bak</FileNamePattern>
- <MinIndex>1</MinIndex>
- <MaxIndex>3</MaxIndex>
- </rollingPolicy>
- <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
- <MaxFileSize>100MB</MaxFileSize>
- </triggeringPolicy>
- <encoder>
- <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg
- %n</Pattern>
- </encoder>
- </appender>
- <!-- 输出到控制台和文件,可定义更多的 Appender,默认配置 -->
- <root level="DEBUG">
- <appender-ref ref="stdout" />
- </root>
- <!-- com.sysmaster包配置日志规则 -->
- <logger name="com.sysmaster" level="INFO" additivity="false">
- <appender-ref ref="stdout"/>
- <appender-ref ref="InfoFileZip"/>
- <appender-ref ref="ErrorFile"/>
- </logger>
- <!--为com.sysmaster.po.ResourcePO类配置日志规则 -->
- <!--
- <logger name="com.sysmaster.po.ResourcePO" level="INFO" additivity="false">
- <appender-ref ref="STDOUT"/>
- </logger>
- -->
- <!-- 下面配置一些第三方包的日志过滤级别,用于避免刷屏 -->
- <!--
- <logger name="org.hibernate" level="WARN"/>
- <logger name="org.springframework" level="WARN"/>
- <logger name="com.opensymphony" level="WARN"/>
- <logger name="org.apache" level="WARN"/>
- -->
- <!-- 为 Hibernate 定制日志规则 -->
- <logger name="org.hibernate.type.descriptor.sql.BasicBinder" level="TRACE"/>
- <logger name="org.hibernate.type.descriptor.sql.BasicExtractor" level="DEBUG"/>
- <logger name="org.hibernate.SQL" level="DEBUG"/>
- <logger name="org.hibernate.engine.QueryParameters" level="DEBUG"/>
- <logger name="org.hibernate.engine.query.HQLQueryPlan" level="DEBUG"/>
- </configuration>
LogbackConfigListener.java
- package com.sysmaster.util.logback;
- import javax.servlet.ServletContextEvent;
- import javax.servlet.ServletContextListener;
- /**
- * 用于启用logback日志
- *
- */
- public class LogbackConfigListener implements ServletContextListener {
- public void contextInitialized(ServletContextEvent event) {
- LogbackWebConfigurer.initLogging(event.getServletContext());
- }
- public void contextDestroyed(ServletContextEvent event) {
- LogbackWebConfigurer.shutdownLogging(event.getServletContext());
- }
- }
LogbackConfigurer.java
- package com.sysmaster.util.logback;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.net.URL;
- import org.slf4j.LoggerFactory;
- import org.springframework.util.ResourceUtils;
- import org.springframework.util.SystemPropertyUtils;
- import ch.qos.logback.classic.LoggerContext;
- import ch.qos.logback.classic.joran.JoranConfigurator;
- import ch.qos.logback.core.joran.spi.JoranException;
- /**
- *
- * 用于读取logback的配置文件,可以直接用spring->log4j的代码修改
- *
- */
- public abstract class LogbackConfigurer
- {
- public static final String CLASSPATH_URL_PREFIX = "classpath:";
- public static final String XML_FILE_EXTENSION = ".xml";
- private static LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
- private static JoranConfigurator configurator = new JoranConfigurator();
- public static void initLogging(String location) throws FileNotFoundException {
- String resolvedLocation = SystemPropertyUtils.resolvePlaceholders(location);
- URL url = ResourceUtils.getURL(resolvedLocation);
- if (resolvedLocation.toLowerCase().endsWith(XML_FILE_EXTENSION)) {
- configurator.setContext(lc);
- lc.reset();
- try {
- configurator.doConfigure(url);
- } catch (JoranException ex) {
- throw new FileNotFoundException(url.getPath());
- }
- lc.start();
- }
- }
- public static void shutdownLogging() {
- lc.stop();
- }
- public static void setWorkingDirSystemProperty(String key) {
- System.setProperty(key, new File("").getAbsolutePath());
- }
- }
LogbackWebConfigurer.java
- package com.sysmaster.util.logback;
- import java.io.FileNotFoundException;
- import javax.servlet.ServletContext;
- import org.springframework.util.ResourceUtils;
- import org.springframework.util.SystemPropertyUtils;
- import org.springframework.web.util.WebUtils;
- /**
- *
- * 用于读取logback的WEB配置文件,可以直接用spring->log4j的代码修改
- *
- */
- public abstract class LogbackWebConfigurer
- {
- public static final String CONFIG_LOCATION_PARAM = "logbackConfigLocation";
- public static final String REFRESH_INTERVAL_PARAM = "logbackRefreshInterval";
- public static final String EXPOSE_WEB_APP_ROOT_PARAM = "logbackExposeWebAppRoot";
- public static void initLogging(ServletContext servletContext) {
- if (exposeWebAppRoot(servletContext)) {
- WebUtils.setWebAppRootSystemProperty(servletContext);
- }
- String location = servletContext.getInitParameter(CONFIG_LOCATION_PARAM);
- if (location != null) {
- try {
- if (!ResourceUtils.isUrl(location)) {
- location = SystemPropertyUtils.resolvePlaceholders(location);
- location = WebUtils.getRealPath(servletContext, location);
- }
- servletContext.log("从 [" + location + "]初始化logback的配置");
- LogbackConfigurer.initLogging(location);
- } catch (FileNotFoundException ex) {
- throw new IllegalArgumentException("无效的 'logbackConfigLocation' 配置参数,请检查web.xml中的logbackConfigLocation参数值前是否有classpat字样: " + ex.getMessage());
- }
- }
- }
- public static void shutdownLogging(ServletContext servletContext) {
- servletContext.log("关闭logback");
- try {
- LogbackConfigurer.shutdownLogging();
- } finally {
- if (exposeWebAppRoot(servletContext)) {
- WebUtils.removeWebAppRootSystemProperty(servletContext);
- }
- }
- }
- private static boolean exposeWebAppRoot(ServletContext servletContext) {
- String exposeWebAppRootParam = servletContext.getInitParameter(EXPOSE_WEB_APP_ROOT_PARAM);
- return (exposeWebAppRootParam == null || Boolean.valueOf(exposeWebAppRootParam));
- }
- }
转:http://star77266989.iteye.com/blog/2112113