logback是log4j作者推出的新日志系统,原生支持slf4j通用日志api,允许平滑切换日志系统,并且对简化应用部署中日志处理的工作做了有益的封装。
官方地址为:http://logback.qos.ch/
Logback日志需要依赖一下jar包:
slf4j-api-1.6.0.jar
logback-core-0.9.21.jar
logback-classic-0.9.21.jar
logback-access-0.9.21.jar
主配置文件为logback.xml,放在src目录下或是WEB-INF/classes下,logback会自动加载
logback.xml的基本结构如下:
view plaincopy to clipboardprint?
01.<?xml version="1.0" encoding="UTF-8"?>
02.<configuration>
03.<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
04. <encoder>
05. <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
06. </encoder>
07. </appender>
08. <root level="DEBUG"><appender-ref ref="STDOUT" /></root>
09.</configuration>
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="DEBUG"><appender-ref ref="STDOUT" /></root>
</configuration>
logback.xml的基本配置信息都包含在configuration标签中,需要含有至少一个appender标签用于指定日志输出方式和输出格式,root标签为系统默认日志进程,通过level指定日志级别,通过appender-ref关联前面指定顶的日志输出方式。
例子中的appender使用的是ch.qos.logback.core.ConsoleAppender类,用于对控制台进行日志输出
其中encoder标签指定日志输出格式为“时间 线程 级别 类路径 信息”
logback的文件日志输出方式还提供多种日志分包策略
1.文件日志
view plaincopy to clipboardprint?
01.<appender name="ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
02. <file>E:/logs/mylog.txt</file>
03. <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
04. <!-- rollover daily -->
05. <fileNamePattern>E:/logs/mylog-%d{yyyy-MM-dd_HH-mm}.%i.log</fileNamePattern>
06. <maxHistory>5</maxHistory>
07. <timeBasedFileNamingAndTriggeringPolicy
08. class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
09. <!-- or whenever the file size reaches 100MB -->
10. <maxFileSize>100MB</maxFileSize>
11. </timeBasedFileNamingAndTriggeringPolicy>
12. </rollingPolicy>
13. <encoder>
14. <pattern>%date %level [%thread] %logger{36} [%file : %line] %msg%n</pattern>
15. </encoder>
16. </appender
<appender name="ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>E:/logs/mylog.txt</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>E:/logs/mylog-%d{yyyy-MM-dd_HH-mm}.%i.log</fileNamePattern>
<maxHistory>5</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- or whenever the file size reaches 100MB -->
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<pattern>%date %level [%thread] %logger{36} [%file : %line] %msg%n</pattern>
</encoder>
</appender
文件日志输出采用的ch.qos.logback.core.rolling.RollingFileAppender类,它的基本属性包括<file>指定输入文件路径,encoder指定日志格式。其中,rollingPolicy标签指定的是日志分包策略,ch.qos.logback.core.rolling.TimeBasedRollingPolicy类实现的是基于时间的分包策略,分包间隔是根据fileNamePattern中指定的事件最小单位,比如例子中的%d{yyyy-MM-dd_HH-mm}的最小事件单位为分,它的触发方式就是1分钟,策略在每次想日志中添加新内容时触发,如果满足条件,就将mylog.txt复制到E:/logs/目录并更名为mylog-2010-06-22_13-13.1.log,并删除原mylog.txt。maxHistory的值为指定E:/logs目录下存在的类似mylog-2010-06-22_13-13.1.log文件的最大个数,当超过时会删除最早的文件。此外,策略还可以互相嵌套,比如本例中在时间策略中又嵌套了文件大小策略,ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP实现对单文件大小的判断,当超过maxFileSize中指定大大小时,文件名中的变量%i会加一,即在不满足时间触发且满足大小触发时,会生成mylog-2010-06-22_13-13.1.log和mylog-2010-06-22_13-13.2.log两个文件。
2.数据库日志
view plaincopy to clipboardprint?
01.<appender name="DB" class="ch.qos.logback.classic.db.DBAppender">
02. <connectionSource class="ch.qos.logback.core.db.DriverManagerConnectionSource">
03. <dataSource
04. class="com.mchange.v2.c3p0.ComboPooledDataSource">
05. <driverClass>com.mysql.jdbc.Driver</driverClass>
06. <url>jdbc:mysql://127.0.0.1:3306/databaseName</url>
07. <user>root</user>
08. <password>root</password>
09. </dataSource>
10. </connectionSource>
11. </appender>
<appender name="DB" class="ch.qos.logback.classic.db.DBAppender">
<connectionSource class="ch.qos.logback.core.db.DriverManagerConnectionSource">
<dataSource
class="com.mchange.v2.c3p0.ComboPooledDataSource">
<driverClass>com.mysql.jdbc.Driver</driverClass>
<url>jdbc:mysql://127.0.0.1:3306/databaseName</url>
<user>root</user>
<password>root</password>
</dataSource>
</connectionSource>
</appender>
数据库输出使用ch.qos.logback.classic.db.DBAppender类,数据源支持c3p0数据连接池,例子中使用的MySql,其他配置方式请参考官方文档。
使用数据库输出需要在数据库中建立3个表,建表脚本如下
view plaincopy to clipboardprint?
01.# Logback: the reliable, generic, fast and flexible logging framework.
02.# Copyright (C) 1999-2010, QOS.ch. All rights reserved.
03.#
04.# See http://logback.qos.ch/license.html for the applicable licensing
05.# conditions.
06.# This SQL script creates the required tables by ch.qos.logback.classic.db.DBAppender.
07.#
08.# It is intended for MySQL databases. It has been tested on MySQL 5.1.37
09.# on Linux
10.
11.BEGIN;
12.DROP TABLE IF EXISTS logging_event_property;
13.DROP TABLE IF EXISTS logging_event_exception;
14.DROP TABLE IF EXISTS logging_event;
15.COMMIT;
16.
17.BEGIN;
18.CREATE TABLE logging_event
19. (
20. timestmp BIGINT NOT NULL,
21. formatted_message TEXT NOT NULL,
22. logger_name VARCHAR(254) NOT NULL,
23. level_string VARCHAR(254) NOT NULL,
24. thread_name VARCHAR(254),
25. reference_flag SMALLINT,
26. arg0 VARCHAR(254),
27. arg1 VARCHAR(254),
28. arg2 VARCHAR(254),
29. arg3 VARCHAR(254),
30. caller_filename VARCHAR(254) NOT NULL,
31. caller_class VARCHAR(254) NOT NULL,
32. caller_method VARCHAR(254) NOT NULL,
33. caller_line CHAR(4) NOT NULL,
34. event_id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY
35. );
36.COMMIT;
37.BEGIN;
38.CREATE TABLE logging_event_property
39. (
40. event_id BIGINT NOT NULL,
41. mapped_key VARCHAR(254) NOT NULL,
42. mapped_value TEXT,
43. PRIMARY KEY(event_id, mapped_key),
44. FOREIGN KEY (event_id) REFERENCES logging_event(event_id)
45. );
46.COMMIT;
47.BEGIN;
48.CREATE TABLE logging_event_exception
49. (
50. event_id BIGINT NOT NULL,
51. i SMALLINT NOT NULL,
52. trace_line VARCHAR(254) NOT NULL,
53. PRIMARY KEY(event_id, i),
54. FOREIGN KEY (event_id) REFERENCES logging_event(event_id)
55. );
56.COMMIT;
# Logback: the reliable, generic, fast and flexible logging framework.
# Copyright (C) 1999-2010, QOS.ch. All rights reserved.
#
# See http://logback.qos.ch/license.html for the applicable licensing
# conditions.
# This SQL script creates the required tables by ch.qos.logback.classic.db.DBAppender.
#
# It is intended for MySQL databases. It has been tested on MySQL 5.1.37
# on Linux
BEGIN;
DROP TABLE IF EXISTS logging_event_property;
DROP TABLE IF EXISTS logging_event_exception;
DROP TABLE IF EXISTS logging_event;
COMMIT;
BEGIN;
CREATE TABLE logging_event
(
timestmp BIGINT NOT NULL,
formatted_message TEXT NOT NULL,
logger_name VARCHAR(254) NOT NULL,
level_string VARCHAR(254) NOT NULL,
thread_name VARCHAR(254),
reference_flag SMALLINT,
arg0 VARCHAR(254),
arg1 VARCHAR(254),
arg2 VARCHAR(254),
arg3 VARCHAR(254),
caller_filename VARCHAR(254) NOT NULL,
caller_class VARCHAR(254) NOT NULL,
caller_method VARCHAR(254) NOT NULL,
caller_line CHAR(4) NOT NULL,
event_id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY
);
COMMIT;
BEGIN;
CREATE TABLE logging_event_property
(
event_id BIGINT NOT NULL,
mapped_key VARCHAR(254) NOT NULL,
mapped_value TEXT,
PRIMARY KEY(event_id, mapped_key),
FOREIGN KEY (event_id) REFERENCES logging_event(event_id)
);
COMMIT;
BEGIN;
CREATE TABLE logging_event_exception
(
event_id BIGINT NOT NULL,
i SMALLINT NOT NULL,
trace_line VARCHAR(254) NOT NULL,
PRIMARY KEY(event_id, i),
FOREIGN KEY (event_id) REFERENCES logging_event(event_id)
);
COMMIT;
3.其他
此外logback还提供基于mail,基于jmx等多种日志输出方式,你也可以通过继承 ch.qos.logback.core.AppenderBase 自己写appender实现
除了使用默认的日志主线程<root>外,还可以通过<logger>标签定制其他日志线程如:
view plaincopy to clipboardprint?
01.<logger name="com.test" level="DEBUG">
02. <appender-ref ref="STDOUT" />
03. </logger>
<logger name="com.test" level="DEBUG">
<appender-ref ref="STDOUT" />
</logger>
其中name指定线程针对的包路径,level是日志级别,<appender-ref>定义使用那种appender。
例如要实现打印jdbc提交的sql,可以加入如下logger:
<logger name="java.sql.Connection" level="DEBUG"><appender-ref ref="STDOUT" /></logger>
贴出我完整的logback.xml
view plaincopy to clipboardprint?
01.<?xml version="1.0" encoding="UTF-8"?>
02.<configuration>
03. <!-- log output to file -->
04. <appender name="ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
05. <file>E:/logs/mylog.txt</file>
06. <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
07. <!-- rollover daily -->
08. <fileNamePattern>E:/logs/mylog-%d{yyyy-MM-dd_HH-mm}.%i.log</fileNamePattern>
09. <maxHistory>5</maxHistory>
10. <timeBasedFileNamingAndTriggeringPolicy
11. class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
12. <!-- or whenever the file size reaches 100MB -->
13. <maxFileSize>100MB</maxFileSize>
14. </timeBasedFileNamingAndTriggeringPolicy>
15. </rollingPolicy>
16. <encoder>
17. <pattern>%date %level [%thread] %logger{36} [%file : %line] %msg%n</pattern>
18. </encoder>
19. </appender>
20.<!-- log output to console -->
21.<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
22. <encoder>
23. <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
24. </encoder>
25. </appender>
26.<!-- log output to db-->
27.<appender name="DB" class="ch.qos.logback.classic.db.DBAppender">
28. <connectionSource class="ch.qos.logback.core.db.DriverManagerConnectionSource">
29. <dataSource
30. class="com.mchange.v2.c3p0.ComboPooledDataSource">
31. <driverClass>com.mysql.jdbc.Driver</driverClass>
32. <url>jdbc:mysql://127.0.0.1:3306/tunneldb</url>
33. <user>root</user>
34. <password>123123</password>
35. </dataSource>
36. </connectionSource>
37. </appender>
38.
39. <logger name="com.ttt.ttt" level="DEBUG">
40. <appender-ref ref="STDOUT" />
41. </logger>
42.
43. <logger name="java.sql.Connection" level="DEBUG"><appender-ref ref="STDOUT" /></logger>
44. <!-- 打印sql
45. <logger name="java.sql.Statement" level="DEBUG"><appender-ref ref="STDOUT" /></logger>
46. <logger name="java.sql.PreparedStatement" level="DEBUG"><appender-ref ref="STDOUT" /></logger>
47.
48. <logger name="java.sql.ResultSet" level="DEBUG"><appender-ref ref="STDOUT" /></logger>
49. -->
50. <root level="DEBUG"><appender-ref ref="ROLLING" /></root>
51.</configuration>
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- log output to file -->
<appender name="ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>E:/logs/mylog.txt</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>E:/logs/mylog-%d{yyyy-MM-dd_HH-mm}.%i.log</fileNamePattern>
<maxHistory>5</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- or whenever the file size reaches 100MB -->
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<pattern>%date %level [%thread] %logger{36} [%file : %line] %msg%n</pattern>
</encoder>
</appender>
<!-- log output to console -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<!-- log output to db-->
<appender name="DB" class="ch.qos.logback.classic.db.DBAppender">
<connectionSource class="ch.qos.logback.core.db.DriverManagerConnectionSource">
<dataSource
class="com.mchange.v2.c3p0.ComboPooledDataSource">
<driverClass>com.mysql.jdbc.Driver</driverClass>
<url>jdbc:mysql://127.0.0.1:3306/tunneldb</url>
<user>root</user>
<password>123123</password>
</dataSource>
</connectionSource>
</appender>
<logger name="com.ttt.ttt" level="DEBUG">
<appender-ref ref="STDOUT" />
</logger>
<logger name="java.sql.Connection" level="DEBUG"><appender-ref ref="STDOUT" /></logger>
<!-- 打印sql
<logger name="java.sql.Statement" level="DEBUG"><appender-ref ref="STDOUT" /></logger>
<logger name="java.sql.PreparedStatement" level="DEBUG"><appender-ref ref="STDOUT" /></logger>
<logger name="java.sql.ResultSet" level="DEBUG"><appender-ref ref="STDOUT" /></logger>
-->
<root level="DEBUG"><appender-ref ref="ROLLING" /></root>
</configuration>
总结:logback提供了丰富而高效的日志输出方式,并通过滚动策略,将实施时复杂的备份策略整合,极大的简化的部署成本,有兴趣的朋友可已考虑放到自己的项目里.官方提供了几百页的文档,内容相当详细,更多的内容请参照官方文档。
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/jiaincs/archive/2010/06/22/5686287.aspx