1、簡介html
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來訪問日誌的功能。java
2、slf4j和logback的使用web
1.若是日誌的參數超過3個,須要寫成spring
Object[] params = {newVal, below, above}; sql
logger.debug("Value {} was inserted between {} and {}.", params); express
2.由於內部已優化,做者認爲slf4j的logger不須要定義爲static。apache
3.可設置緩存後批量寫日誌文件(但服務器若是重啓,可能會丟失未寫到磁盤的記錄)api
4.MDC,用Filter,將當前用戶名等業務信息放入MDC中,在日誌format定義中便可使用該變量。緩存
5.JMS Appender用於告警, DB Appender用於業務日誌等可使用插件,如生成Log代碼的Eclipse插件Log4E。tomcat
6.tomcat和glassfish中,設定日誌路徑爲../logs/xxxx.log 都能將日誌放入應用服務器自己的logs目錄
3、例子
準備相關工具
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
二、實現記錄日誌
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://logback.qos.ch/manual/configuration.html】
Inserting log requests into the application code requires a fair amount of planning and effort. Observation shows that approximately four percent of code is dedicated to logging. Consequently, even a moderately sized application will contain thousands of logging statements embedded within its code. Given their number, we need tools to manage these log statements.
Logback can be configured either programmatically or with a configuration script expressed in XML or Groovy format. By the way, existing log4j users can convert their log4j.properties files tologback.xml using our PropertiesTranslator web-application.
Let us begin by discussing the initialization steps that logback follows to try to configure itself:
Logback tries to find a file called logback.groovy in the classpath.
If no such file is found, logback tries to find a file called logback-test.xml in the classpath.
If no such file is found, it checks for the file logback.xml in the classpath..
If no such file is found, and the executing JVM has the ServiceLoader (JDK 6 and above) the ServiceLoader will be used to resolve an implementation of com.qos.logback.classic.spi.Configurator
. The first implementation found will be used. See ServiceLoader documentation for more details.
If none of the above succeeds, logback configures itself automatically using the BasicConfigurator
which will cause logging output to be directed to the console.
The fourth and last step is meant to provide a default (but very basic) logging functionality in the absence of a configuration file.