logback

 

转载自:http://yuri-liuyu.iteye.com/blog/954038。仅供学习,谢谢原文作者

 

首先介绍下logback的前世今生。

 

slf4j由log4j作者Ceki开发,逐步取代apahce commons logging。

logback由log4j作者Ceki开发,逐步取代log4j。

slf4j等于commons-logging,是各种日志实现的通用入口,会根据classpath中存在下面哪一个Jar来决定具体的日志实现库。

 

logback相比较log4j的优势

slf4j支持参数化的logger.error("帐号ID:{}不存在", userId);

告别了if(logger.isDebugEnable()) 时代。

另外logback的整体性能比log4j也较佳,hibernate等项目已经采用了slf4j。

 

要在工程里面使用logback需要以下jar文件:

 

slf4j-api-1.6.1.jar

官网网址:http://www.slf4j.org/

下载地址:http://www.slf4j.org/dist/slf4j-1.6.1.zip

 

logback-access-0.9.29.jar

logback-classic-0.9.29.jar

logback-core-0.9.29.jar

官网网址:http://logback.qos.ch/

下载地址:http://logback.qos.ch/dist/logback-0.9.29.zip

 

 

slf4j和logback的使用

1.如果日志的参数超过3个,需要写成

 

 

Java代码    收藏代码
  1. Object[] params = {newVal, below, above};  
  2. logger.debug("Value {} was inserted between {} and {}.", params);  

 

2.因为内部已优化,作者认为slf4j的logger不需要定义为static。

3.可设置缓存后批量写日志文件(但服务器如果重启,可能会丢失未写到磁盘的记录)

4.MDC,用Filter,将当前用户名等业务信息放入MDC中,在日志format定义中即可使用该变量。

5.JMS Appender用于告警, DB Appender用于业务日志等可以使用插件,如生成Log代码的Eclipse插件Log4E。

6.tomcat和glassfish中,设定日志路径为../logs/xxxx.log 都能将日志放入应用服务器本身的logs目录

 

最后把最近完善的一个logback.xml贴上,毕竟实际项目中的文件最能说明问题。

 

 

Xml代码    收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <configuration>  
  4.     <substitutionProperty name="log.base" value="d:\\logback\\logback" />  
  5.     <jmxConfigurator />  
  6.     <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">  
  7.         <filter class="ch.qos.logback.core.filter.EvaluatorFilter">  
  8.             <evaluator name="myEval">  
  9.                 <expression>message.contains("dao")</expression>  
  10.             </evaluator>  
  11.             <onMatch>ACCEPT</onMatch>  
  12.             <onMismatch>DENY</onMismatch>  
  13.         </filter>  
  14.         <layout class="ch.qos.logback.classic.PatternLayout">  
  15.             <pattern>%date [%thread] %-5level %logger{80} - %msg%n</pattern>  
  16.         </layout>  
  17.     </appender>  
  18.     <appender name="logfile-dao"  
  19.         class="ch.qos.logback.core.rolling.RollingFileAppender">  
  20.         <filter class="ch.qos.logback.core.filter.EvaluatorFilter">  
  21.             <evaluator name="myEval_dao">  
  22.                 <expression>message.contains("dao")</expression>  
  23.             </evaluator>  
  24.             <onMatch>ACCEPT</onMatch>  
  25.             <onMismatch>DENY</onMismatch>  
  26.         </filter>  
  27.         <Encoding>UTF-8</Encoding>  
  28.         <File>${log.base}_dao.log</File>  
  29.         <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">  
  30.             <FileNamePattern>${log.base}.%d{yyyy-MM-dd}_dao.log.zip  
  31.             </FileNamePattern>  
  32.         </rollingPolicy>  
  33.         <layout class="ch.qos.logback.classic.PatternLayout">  
  34.             <pattern>%date [%thread] %-5level %logger{80} - %msg%n</pattern>  
  35.         </layout>  
  36.     </appender>  
  37.     <appender name="logfile-service"  
  38.         class="ch.qos.logback.core.rolling.RollingFileAppender">  
  39.         <filter class="ch.qos.logback.core.filter.EvaluatorFilter">  
  40.             <evaluator name="myEval_service">  
  41.                 <expression>message.contains("service.impl")</expression>  
  42.             </evaluator>  
  43.             <onMatch>ACCEPT</onMatch>  
  44.             <onMismatch>DENY</onMismatch>  
  45.         </filter>  
  46.         <Encoding>UTF-8</Encoding>  
  47.         <File>${log.base}_service.log</File>  
  48.         <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">  
  49.             <FileNamePattern>${log.base}.%d{yyyy-MM-dd}_service.log.zip  
  50.             </FileNamePattern>  
  51.         </rollingPolicy>  
  52.         <layout class="ch.qos.logback.classic.PatternLayout">  
  53.             <pattern>%date [%thread] %-5level %logger{80} - %msg%n</pattern>  
  54.         </layout>  
  55.     </appender>  
  56.     <appender name="jms_dao" class="ch.qos.logback.classic.net.JMSQueueAppender">  
  57.         <InitialContextFactoryName>  
  58.             org.apache.activemq.jndi.ActiveMQInitialContextFactory  
  59.         </InitialContextFactoryName>  
  60.         <ProviderURL>tcp://192.168.1.120:61616</ProviderURL>  
  61.         <filter class="ch.qos.logback.core.filter.EvaluatorFilter">  
  62.             <evaluator name="myEval_service">  
  63.                 <expression>message.contains("dao")</expression>  
  64.             </evaluator>  
  65.             <onMatch>ACCEPT</onMatch>  
  66.             <onMismatch>DENY</onMismatch>  
  67.         </filter>  
  68.         <QueueConnectionFactoryBindingName>ConnectionFactory  
  69.         </QueueConnectionFactoryBindingName>  
  70.         <QueueBindingName>cms_dao_log</QueueBindingName>  
  71.     </appender>  
  72.     <appender name="jms_service" class="ch.qos.logback.classic.net.JMSQueueAppender">  
  73.         <InitialContextFactoryName>  
  74.             org.apache.activemq.jndi.ActiveMQInitialContextFactory  
  75.         </InitialContextFactoryName>  
  76.         <ProviderURL>tcp://192.168.1.120:61616</ProviderURL>  
  77.         <filter class="ch.qos.logback.core.filter.EvaluatorFilter">  
  78.             <evaluator name="myEval_service">  
  79.                 <expression>message.contains("service.impl")</expression>  
  80.             </evaluator>  
  81.             <onMatch>ACCEPT</onMatch>  
  82.             <onMismatch>DENY</onMismatch>  
  83.         </filter>  
  84.         <QueueConnectionFactoryBindingName>ConnectionFactory  
  85.         </QueueConnectionFactoryBindingName>  
  86.         <QueueBindingName>cms_service_log</QueueBindingName>  
  87.     </appender>  
  88.     <logger name="com.cms5.cmsservice.jms">  
  89.         <level value="DEBUG" />  
  90.     </logger>  
  91.     <logger name="java.sql.PreparedStatement">  
  92.         <level value="DEBUG" />  
  93.     </logger>  
  94.     <logger name="java.sql.Connection">  
  95.         <level value="DEBUG" />  
  96.     </logger>  
  97.     <logger name="java.sql.Statement">  
  98.         <level value="DEBUG" />  
  99.     </logger>  
  100.     <logger name="com.ibatis">  
  101.         <level value="DEBUG" />  
  102.     </logger>  
  103.     <logger name="com.ibatis.common.jdbc.SimpleDataSource">  
  104.         <level value="DEBUG" />  
  105.     </logger>  
  106.     <logger name="com.ibatis.common.jdbc.ScriptRunner">  
  107.         <level value="DEBUG" />  
  108.     </logger>  
  109.     <logger name="com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate">  
  110.         <level value="DEBUG" />  
  111.     </logger>  
  112.     <logger name="com.danga.MemCached">  
  113.         <level value="INFO" />  
  114.     </logger>  
  115.     <logger name="org.springframework.test">  
  116.         <level value="DEBUG" />  
  117.     </logger>  
  118.     <logger name="org.apache.struts2">  
  119.         <level value="DEBUG" />  
  120.     </logger>  
  121.     <root>  
  122.         <level value="INFO" />  
  123.         <!--<appender-ref ref="stdout" /> 
  124.         -->  
  125.         <appender-ref ref="logfile-dao" />  
  126.         <appender-ref ref="logfile-service" />  
  127.         <appender-ref ref="jms_dao" />  
  128.         <appender-ref ref="jms_service" />  
  129.     </root>  
  130. </configuration>  

 

你可能感兴趣的:(logback)