Lobback日志文件

Logback是由log4j创始人Ceki Gülcü设计的又一个开源日志组件。

 

logback.xml

 

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?> <!-- Reference Manual http://logback.qos.ch/manual/index.html -->   
  2.     <configuration>   
  3.         <!-- LEVEL:TRACE, DEBUG, INFO, WARN, ERROR, ALL 和 OFF -->  
  4.         <!-- 控制台输出 -->  
  5.         <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">      
  6.             <!-- encoder 可以指定字符集,对于中文输出有意义 -->       
  7.             <encoder charset="UTF-8">              
  8.                 <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>          
  9.             </encoder>      
  10.         </appender>  
  11.           
  12.         <!-- 每日最多生成10个文件,可以通过这些文件查看系统工作状态 -->  
  13.         <!--  
  14.         <appender name="RollingFile" class="ch.qos.logback.core.rolling.RollingFileAppender">  
  15.             <filter class="ch.qos.logback.classic.filter.ThresholdFilter">              
  16.                 <level>TRACE</level>          
  17.             </filter>       
  18.             <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">  
  19.                 <fileNamePattern>${log.root}/WEB-INF/logs/trace-%d{yyyy-MM-dd}.log</fileNamePattern>    
  20.   
  21.             
  22.                 <maxHistory>10</maxHistory>          
  23.             </rollingPolicy>  
  24.             <encoder>              
  25.                 <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>          
  26.             </encoder>      
  27.         </appender>   
  28.         -->  
  29.           
  30.         <!-- 下面这段注释的代码可以在日志文件超过 5MB 时进行归档,并且归档文件后缀只要是 .zip 或 .gz 就会自动  
  31.   
  32. 压缩日志归档 -->  
  33.         <appender name="InfoFileZip" class="ch.qos.logback.core.rolling.RollingFileAppender">  
  34.             <file>${log.root}/WEB-INF/logs/info.log</file>    
  35.             <filter class="ch.qos.logback.classic.filter.ThresholdFilter">              
  36.                 <level>INFO</level>          
  37.             </filter>  
  38.             <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">  
  39.                 <fileNamePattern>${log.root}/WEB-INF/logs/bak/info.%i.log.zip</fileNamePattern>         
  40.   
  41.      
  42.                 <minIndex>1</minIndex>  
  43.                 <maxIndex>10</maxIndex>  
  44.             </rollingPolicy>    
  45.               
  46.             <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">            
  47.                 <maxFileSize>10MB</maxFileSize>  
  48.             </triggeringPolicy>     
  49.   
  50.             <encoder>         
  51.                 <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>          
  52.             </encoder>      
  53.         </appender>  
  54.           
  55.         <!-- 时间滚动输出 level为 ERROR 日志 -->  
  56.         <appender name="ErrorFile" class="ch.qos.logback.core.rolling.RollingFileAppender">           
  57.             <filter class="ch.qos.logback.classic.filter.LevelFilter">             
  58.                 <level>ERROR</level>             
  59.                 <OnMismatch>DENY</OnMismatch>             
  60.                 <OnMatch>ACCEPT</OnMatch>         
  61.             </filter>  
  62.             <File>${log.root}/WEB-INF/logs/error.log</File>  
  63.             <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">              
  64.                 <FileNamePattern>${log.root}/WEB-INF/logs/error.log.%i.bak</FileNamePattern>            
  65.   
  66.     
  67.                 <MinIndex>1</MinIndex>   
  68.                 <MaxIndex>3</MaxIndex>  
  69.             </rollingPolicy>         
  70.             <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">              
  71.                 <MaxFileSize>100MB</MaxFileSize>          
  72.             </triggeringPolicy>          
  73.             <encoder>  
  74.                 <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg  
  75.   
  76. %n</Pattern>          
  77.             </encoder>  
  78.         </appender>  
  79.   
  80.   
  81.     <!-- 输出到控制台和文件,可定义更多的 Appender,默认配置 -->       
  82.     <root level="DEBUG">  
  83.         <appender-ref ref="stdout" />   
  84.     </root>    
  85.       
  86.     <!-- com.sysmaster包配置日志规则 -->  
  87.     <logger name="com.sysmaster" level="INFO" additivity="false">  
  88.         <appender-ref ref="stdout"/>  
  89.         <appender-ref ref="InfoFileZip"/>  
  90.         <appender-ref ref="ErrorFile"/>  
  91.     </logger>  
  92.       
  93.     <!--为com.sysmaster.po.ResourcePO类配置日志规则 -->  
  94.     <!--  
  95.     <logger name="com.sysmaster.po.ResourcePO" level="INFO" additivity="false">  
  96.         <appender-ref ref="STDOUT"/>  
  97.     </logger>  
  98.     -->  
  99.       
  100.     <!-- 下面配置一些第三方包的日志过滤级别,用于避免刷屏 -->    
  101.     <!--  
  102.     <logger name="org.hibernate" level="WARN"/>      
  103.     <logger name="org.springframework" level="WARN"/>      
  104.     <logger name="com.opensymphony" level="WARN"/>      
  105.     <logger name="org.apache" level="WARN"/>   
  106.     -->  
  107.       
  108.     <!-- 为 Hibernate 定制日志规则  -->   
  109.     <logger name="org.hibernate.type.descriptor.sql.BasicBinder" level="TRACE"/>      
  110.     <logger name="org.hibernate.type.descriptor.sql.BasicExtractor" level="DEBUG"/>      
  111.     <logger name="org.hibernate.SQL" level="DEBUG"/>  
  112.     <logger name="org.hibernate.engine.QueryParameters" level="DEBUG"/>      
  113.     <logger name="org.hibernate.engine.query.HQLQueryPlan" level="DEBUG"/>   
  114. </configuration>  

 

LogbackConfigListener.java

Java代码   收藏代码
  1. package com.sysmaster.util.logback;  
  2.   
  3. import javax.servlet.ServletContextEvent;  
  4. import javax.servlet.ServletContextListener;  
  5.   
  6. /** 
  7.  * 用于启用logback日志  
  8.  *  
  9.  */  
  10. public class LogbackConfigListener implements ServletContextListener {  
  11.   
  12.     public void contextInitialized(ServletContextEvent event) {  
  13.         LogbackWebConfigurer.initLogging(event.getServletContext());  
  14.     }  
  15.   
  16.     public void contextDestroyed(ServletContextEvent event) {  
  17.         LogbackWebConfigurer.shutdownLogging(event.getServletContext());  
  18.     }  
  19. }  

 

LogbackConfigurer.java

Java代码   收藏代码
  1. package com.sysmaster.util.logback;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileNotFoundException;  
  5. import java.net.URL;  
  6.   
  7. import org.slf4j.LoggerFactory;  
  8. import org.springframework.util.ResourceUtils;  
  9. import org.springframework.util.SystemPropertyUtils;  
  10.   
  11. import ch.qos.logback.classic.LoggerContext;  
  12. import ch.qos.logback.classic.joran.JoranConfigurator;  
  13. import ch.qos.logback.core.joran.spi.JoranException;  
  14.   
  15. /** 
  16.  *  
  17.  * 用于读取logback的配置文件,可以直接用spring->log4j的代码修改 
  18.  *  
  19.  */  
  20. public abstract class LogbackConfigurer   
  21. {  
  22.     public static final String CLASSPATH_URL_PREFIX = "classpath:";  
  23.   
  24.     public static final String XML_FILE_EXTENSION   = ".xml";  
  25.   
  26.     private static LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();  
  27.     private static JoranConfigurator configurator = new JoranConfigurator();  
  28.   
  29.     public static void initLogging(String location) throws FileNotFoundException {  
  30.         String resolvedLocation = SystemPropertyUtils.resolvePlaceholders(location);  
  31.         URL url = ResourceUtils.getURL(resolvedLocation);  
  32.         if (resolvedLocation.toLowerCase().endsWith(XML_FILE_EXTENSION)) {  
  33.             configurator.setContext(lc);  
  34.             lc.reset();  
  35.             try {  
  36.                 configurator.doConfigure(url);  
  37.             } catch (JoranException ex) {  
  38.                 throw new FileNotFoundException(url.getPath());  
  39.             }  
  40.             lc.start();  
  41.         }  
  42.     }  
  43.   
  44.     public static void shutdownLogging() {  
  45.         lc.stop();  
  46.     }  
  47.   
  48.     public static void setWorkingDirSystemProperty(String key) {  
  49.         System.setProperty(key, new File("").getAbsolutePath());  
  50.     }  
  51.   
  52. }  

 

LogbackWebConfigurer.java

Java代码   收藏代码
  1. package com.sysmaster.util.logback;  
  2.   
  3. import java.io.FileNotFoundException;  
  4.   
  5. import javax.servlet.ServletContext;  
  6.   
  7. import org.springframework.util.ResourceUtils;  
  8. import org.springframework.util.SystemPropertyUtils;  
  9. import org.springframework.web.util.WebUtils;  
  10.   
  11. /** 
  12.  *  
  13.  * 用于读取logback的WEB配置文件,可以直接用spring->log4j的代码修改 
  14.  *  
  15.  */  
  16. public abstract class LogbackWebConfigurer   
  17. {  
  18.     public static final String CONFIG_LOCATION_PARAM  = "logbackConfigLocation";  
  19.     public static final String REFRESH_INTERVAL_PARAM  = "logbackRefreshInterval";  
  20.     public static final String EXPOSE_WEB_APP_ROOT_PARAM = "logbackExposeWebAppRoot";  
  21.   
  22.     public static void initLogging(ServletContext servletContext) {  
  23.         if (exposeWebAppRoot(servletContext)) {  
  24.             WebUtils.setWebAppRootSystemProperty(servletContext);  
  25.         }  
  26.   
  27.         String location = servletContext.getInitParameter(CONFIG_LOCATION_PARAM);  
  28.         if (location != null) {  
  29.             try {  
  30.                 if (!ResourceUtils.isUrl(location)) {  
  31.                     location = SystemPropertyUtils.resolvePlaceholders(location);  
  32.                     location = WebUtils.getRealPath(servletContext, location);  
  33.                 }  
  34.                 servletContext.log("从 [" + location + "]初始化logback的配置");  
  35.                 LogbackConfigurer.initLogging(location);  
  36.   
  37.             } catch (FileNotFoundException ex) {  
  38.                 throw new IllegalArgumentException("无效的 'logbackConfigLocation' 配置参数,请检查web.xml中的logbackConfigLocation参数值前是否有classpat字样: " + ex.getMessage());  
  39.             }  
  40.         }  
  41.     }  
  42.   
  43.     public static void shutdownLogging(ServletContext servletContext) {  
  44.         servletContext.log("关闭logback");  
  45.         try {  
  46.             LogbackConfigurer.shutdownLogging();  
  47.         } finally {  
  48.             if (exposeWebAppRoot(servletContext)) {  
  49.                 WebUtils.removeWebAppRootSystemProperty(servletContext);  
  50.             }  
  51.         }  
  52.     }  
  53.   
  54.     private static boolean exposeWebAppRoot(ServletContext servletContext) {  
  55.         String exposeWebAppRootParam = servletContext.getInitParameter(EXPOSE_WEB_APP_ROOT_PARAM);  
  56.         return (exposeWebAppRootParam == null || Boolean.valueOf(exposeWebAppRootParam));  
  57.     }  
  58.   

转:http://star77266989.iteye.com/blog/2112113

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值