logback 介绍及配置使用方法

新项目日志管理统一用logback,由于之前一直在用log4j,所以其实对logback这个由log4j长出来的家伙还不算陌生。

首先介绍下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。

slf4j和logback的使用

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

Object[] params = {newVal, below, above};   

logger.debug("Value {} was inserted between {} and {}.", params);  
Object[] params = {newVal, below, above};
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代码 logback 介绍及配置使用方法 logback 介绍及配置使用方法logback 介绍及配置使用方法
  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>  
<?xml version="1.0" encoding="UTF-8"?>

<configuration>
	<substitutionProperty name="log.base" value="d:\\logback\\logback" />
	<jmxConfigurator />
	<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
		<filter class="ch.qos.logback.core.filter.EvaluatorFilter">
			<evaluator name="myEval">
				<expression>message.contains("dao")</expression>
			</evaluator>
			<onMatch>ACCEPT</onMatch>
			<onMismatch>DENY</onMismatch>
		</filter>
		<layout class="ch.qos.logback.classic.PatternLayout">
			<pattern>%date [%thread] %-5level %logger{80} - %msg%n</pattern>
		</layout>
	</appender>
	<appender name="logfile-dao"
		class="ch.qos.logback.core.rolling.RollingFileAppender">
		<filter class="ch.qos.logback.core.filter.EvaluatorFilter">
			<evaluator name="myEval_dao">
				<expression>message.contains("dao")</expression>
			</evaluator>
			<onMatch>ACCEPT</onMatch>
			<onMismatch>DENY</onMismatch>
		</filter>
		<Encoding>UTF-8</Encoding>
		<File>${log.base}_dao.log</File>
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<FileNamePattern>${log.base}.%d{yyyy-MM-dd}_dao.log.zip
			</FileNamePattern>
		</rollingPolicy>
		<layout class="ch.qos.logback.classic.PatternLayout">
			<pattern>%date [%thread] %-5level %logger{80} - %msg%n</pattern>
		</layout>
	</appender>
	<appender name="logfile-service"
		class="ch.qos.logback.core.rolling.RollingFileAppender">
		<filter class="ch.qos.logback.core.filter.EvaluatorFilter">
			<evaluator name="myEval_service">
				<expression>message.contains("service.impl")</expression>
			</evaluator>
			<onMatch>ACCEPT</onMatch>
			<onMismatch>DENY</onMismatch>
		</filter>
		<Encoding>UTF-8</Encoding>
		<File>${log.base}_service.log</File>
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<FileNamePattern>${log.base}.%d{yyyy-MM-dd}_service.log.zip
			</FileNamePattern>
		</rollingPolicy>
		<layout class="ch.qos.logback.classic.PatternLayout">
			<pattern>%date [%thread] %-5level %logger{80} - %msg%n</pattern>
		</layout>
	</appender>
	<appender name="jms_dao" class="ch.qos.logback.classic.net.JMSQueueAppender">
		<InitialContextFactoryName>
			org.apache.activemq.jndi.ActiveMQInitialContextFactory
		</InitialContextFactoryName>
		<ProviderURL>tcp://192.168.1.120:61616</ProviderURL>
		<filter class="ch.qos.logback.core.filter.EvaluatorFilter">
			<evaluator name="myEval_service">
				<expression>message.contains("dao")</expression>
			</evaluator>
			<onMatch>ACCEPT</onMatch>
			<onMismatch>DENY</onMismatch>
		</filter>
		<QueueConnectionFactoryBindingName>ConnectionFactory
		</QueueConnectionFactoryBindingName>
		<QueueBindingName>cms_dao_log</QueueBindingName>
	</appender>
	<appender name="jms_service" class="ch.qos.logback.classic.net.JMSQueueAppender">
		<InitialContextFactoryName>
			org.apache.activemq.jndi.ActiveMQInitialContextFactory
		</InitialContextFactoryName>
		<ProviderURL>tcp://192.168.1.120:61616</ProviderURL>
		<filter class="ch.qos.logback.core.filter.EvaluatorFilter">
			<evaluator name="myEval_service">
				<expression>message.contains("service.impl")</expression>
			</evaluator>
			<onMatch>ACCEPT</onMatch>
			<onMismatch>DENY</onMismatch>
		</filter>
		<QueueConnectionFactoryBindingName>ConnectionFactory
		</QueueConnectionFactoryBindingName>
		<QueueBindingName>cms_service_log</QueueBindingName>
	</appender>
	<logger name="com.cms5.cmsservice.jms">
		<level value="DEBUG" />
	</logger>
	<logger name="java.sql.PreparedStatement">
		<level value="DEBUG" />
	</logger>
	<logger name="java.sql.Connection">
		<level value="DEBUG" />
	</logger>
	<logger name="java.sql.Statement">
		<level value="DEBUG" />
	</logger>
	<logger name="com.ibatis">
		<level value="DEBUG" />
	</logger>
	<logger name="com.ibatis.common.jdbc.SimpleDataSource">
		<level value="DEBUG" />
	</logger>
	<logger name="com.ibatis.common.jdbc.ScriptRunner">
		<level value="DEBUG" />
	</logger>
	<logger name="com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate">
		<level value="DEBUG" />
	</logger>
	<logger name="com.danga.MemCached">
		<level value="INFO" />
	</logger>
	<logger name="org.springframework.test">
		<level value="DEBUG" />
	</logger>
	<logger name="org.apache.struts2">
		<level value="DEBUG" />
	</logger>
	<root>
		<level value="INFO" />
		<!--<appender-ref ref="stdout" />
		-->
		<appender-ref ref="logfile-dao" />
		<appender-ref ref="logfile-service" />
		<appender-ref ref="jms_dao" />
		<appender-ref ref="jms_service" />
	</root>
</configuration>

 

就先不做说明了,里面的配置还是比较明确的,关于jmsappender那部分,之前也做过简单说明。

相关推荐