在前一篇日志:
Java 流行框架(Spring/Struts2/Hibernate/iBatis)都在使用什么日志组件,介绍了当前流行框架 Spring、 Struts2、 Hibernate、iBatis(Mybatis) 分别使用的是什么通用日志组件:
Spring 3.0.4:Commons-Logging
Struts 2.2.1:间接的优先支持 Commons-Logging
Hibernate 3.6.0:直接的使用 Slf4J
iBatis/MyBatis 3.0.2:间接的优先使用 Slf4J
Struts 2.2.1:间接的优先支持 Commons-Logging
Hibernate 3.6.0:直接的使用 Slf4J
iBatis/MyBatis 3.0.2:间接的优先使用 Slf4J
再考虑 Slf4J 以及 Logback 的优越性,性能得到的改善,多 JVM 写同一日志文件都无妨,方便运用于 OSGI 环境,自动压缩日志,参数化。所以个人认为在 SSH 或 SSI 项目中推荐用
Slf4J + Logback 对于 Hibernate 和 iBatis/MyBatis 直接走这条路
jcl-over-slf4j + Logback 对于 Spring 和 Struts2,需用用 jcl-over-slf4j 把 Commons-Logging 桥接到 Slf4J,然后 Logback。
jcl-over-slf4j + Logback 对于 Spring 和 Struts2,需用用 jcl-over-slf4j 把 Commons-Logging 桥接到 Slf4J,然后 Logback。
下面介绍在这样的项目中如何使用 Slf4J、继而用 Logback 输出日志。
首先需要引入相关的 jar,如果你是用 Maven 来管理你的依赖,那太简单了,只需要在你的 pom.xml 文件中加入:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
|
<
dependency
>
<
groupId
>org.slf4j
groupId
>
<
artifactId
>slf4j-api
artifactId
>
<
version
>1.6.1
version
>
dependency
>
<
dependency
>
<
groupId
>ch.qos.logback
groupId
>
<
artifactId
>logback-core
artifactId
>
<
version
>0.9.24
version
>
dependency
>
<
dependency
>
<
groupId
>ch.qos.logback
groupId
>
<
artifactId
>logback-classic
artifactId
>
<
version
>0.9.24
version
>
dependency
>
<
dependency
>
<
groupId
>org.slf4j
groupId
>
<
artifactId
>jcl-over-slf4j
artifactId
>
<
version
>1.6.1
version
>
dependency
>
|
相应的四个 jar 包,slf4j-api-1.6.1、logback-core-0.9.24.jar、logback-classic-0.9.24.jar、jcl-over-slf4j-1.6.1.jar 就会加到你的 classpath 中了。否则必须手工到
http://www.slf4j.org/ 和
http://logback.qos.ch/ 去下载相应的包,是不是觉得
http://www.slf4j.org/ 和
http://logback.qos.ch/ 是那么的像,没错它们其实映射在同一个 IP 上的,可见
slf4j + logback 原生的绝配。
jcl-over-slf4j-1.6.1.jar 是 Commons-Logging 到 Slf4J 的桥接器,应用 Commons-Logging 的 SPI 模式,只要把它放到 classpath 中就起作用了。机关就是这个包的 META-INF/service/org.apace.commons.logging.LogFactory 文件,内容是:org.apache.commons.logging.impl.SLF4JLogFactory。
接下来配置 logback.xml 文件,logback 也需要像 log4j.properties 那样的配置文件。把 logback.xml 文件放在 classpath 下,内容可参考:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
configuration
>
<
appender
name
=
"stdout"
class
=
"ch.qos.logback.core.ConsoleAppender"
>
<
filter
class
=
"ch.qos.logback.core.filter.EvaluatorFilter"
>
<
evaluator
>
<
expression
>logger.contains("springframework")
expression
>
evaluator
>
<
OnMismatch
>NEUTRAL
OnMismatch
>
<
OnMatch
>DENY
OnMatch
>
filter
>
<
encoder
charset
=
"UTF-8"
>
<
pattern
>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
pattern
>
encoder
>
appender
>
<
appender
name
=
"RollingFile"
class
=
"ch.qos.logback.core.rolling.RollingFileAppender"
>
<
file
>unmi_ssh.log
file
>
<
filter
class
=
"ch.qos.logback.classic.filter.ThresholdFilter"
>
<
level
>INFO
level
>
filter
>
<
rollingPolicy
class
=
"ch.qos.logback.core.rolling.TimeBasedRollingPolicy"
>
<
fileNamePattern
>unmi_ssh.%d{yyyy-MM-dd}..log.zip
fileNamePattern
>
<
minIndex
>1
minIndex
>
<
maxIndex
>3
maxIndex
>
rollingPolicy
>
<
triggeringPolicy
class
=
"ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"
>
<
maxFileSize
>5MB
maxFileSize
>
triggeringPolicy
>
<
encoder
>
<
pattern
>%-4relative [%thread] %-5level %logger{35} - %msg%n
pattern
>
encoder
>
appender
>
<
root
level
=
"DEBUG"
>
<
appender-ref
ref
=
"stdout"
/>
<
appender-ref
ref
=
"RollingFile"
/>
root
>
configuration
>
|
熟悉 log4j 的看到上面的配置应该能够很好的理解,奇怪的是目前还没有 logback.xml 文件的 schema 文件出现,不然在 Eclipse 等 xml 编辑器上可提示出一些标签和属性来。详细的配置可参考:
http://logback.qos.ch/manual/index.html。对于输出日志的过滤可以控制的更精细,这点比 log4j 要强,可以用表达式来判断。
需要用上面的 ch.qos.logback.core.filter.EvaluatorFilter 来过滤日志输出,要引用 janino-2.5.16.jar。在 pom.xml 中加上 org.codehaus.janino.janino-2.5.16 的依赖。
应用 Slf4J 和 Logback 了,来写个测试代码:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
|
import
org.slf4j.Logger;
import
org.slf4j.LoggerFactory;
/**
* Make use of the slf4j
* @author Unmi
*/
public
class
TestLog {
private
static
final
Logger logger = LoggerFactory.getLogger(TestLog.
class
);
public
static
void
main(String[] args) {
logger.info(
"Hello {}"
,
"TestLogback"
);
}
}
|
输出是:00:48:16.984 [main] INFO TestLog - Hello TestLogback
你现在同时可以发现,从 Java 框架输出的日志信息全部按照 logback.xml 中的布局输出,说明它们都经由 Slf4J -> Logback 输出了日志。
你可以继续挖掘,要看它能输出到什么地方就看下它提供了多少种 Appdender,并且 Logback 提供了 Log4J 所没有的 Access 的功能,即可以让你的日志历史信息更直观的展示出来,而不是总是在文本编辑器中来查看。