一、简介
Logback是由log4j创始人设计的又一个开源日志组件。logback当前分成三个模块:logback-core,logback- classic和logback-access。logback-core是其它两个模块的基础模块。logback-classic是log4j的一个 改良版本。此外logback-classic完整实现SLF4J API使你可以很方便地更换成其它日志系统如log4j或JDK14 Logging。logback-access访问模块与Servlet容器集成提供通过Http来访问日志的功能。
二、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目录
三、例子
准备相关工具
1. 从网上下载slf4j组件和logback相关的jar包,很多网站都有,logback可以直接到这个网站下载http://logback.qos.ch/,http://www.slf4j.org/可以直接到这里下载http://www.slf4j.org/。总之,不管在哪里下载,你都需要找到以下几个jar包(可能根据需要,有个别不需要,这个没有详细研究,读者可以自己研究下):
1) logback-acces-0.9.18.jar
2) logback-classic-0.9.18.jar
3) logback-core-0.9.18.jar
4) slf4j-api-1.5.6.jar
2、实现记录日志
1)建立一个JAVA工程,比如是LogTest,工程整体目录结构如下,工程根目录除了src目录外,新建一个lib目录,将上边4个jar包放入并导入工程。再新建一个config目录,用于存放日志配 置文件logback.xml,这个文件如何配置,后边再介绍。
2)配置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>
原文地址:http://www.cnblogs.com/caroline4lc/p/3836486.html