http://yuri-liuyu.iteye.com/blog/954038
新的公司日志管理统一用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);
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 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那部分,之前也做过简单说明。