说明
logback作为log4j的替代,有很多优势。要将logback应用到项目中,步骤很简单。加入依赖的jar包和配置文件即可。
logback.xml中主要元素:
- <property>
定义属性,定义之后,后文可以通过该变量引用。
- <appender>
定义日志输出格式、位置、文件分割等。被<logger>或<root>引用。
- <logger>
定义日志名称及对应的级别。name可以是包名、类名或者单纯的字符串。如果是包名或者类名,那么该包或类中的日志级别,对应这里的设置;如果是单纯的字符串名,那么通过getLogger({name})获取的日志,采用这里的配置。
- <root>
定义默认使用的日志配置。如果不是<logger>中的配置,那么默认使用这里的配置。
使用
logback依赖的包:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
< dependency > < groupId >org.slf4j</ groupId > < artifactId >slf4j-api</ artifactId > < version >1.7.21</ version > </ dependency > < dependency > < groupId >ch.qos.logback</ groupId > < artifactId >logback-core</ artifactId > < version >1.1.7</ version > </ dependency > < dependency > < groupId >ch.qos.logback</ groupId > < artifactId >logback-classic</ artifactId > < version >1.1.7</ version > </ dependency > |
logback.xml配置文件放入classpath中即可,程序会自动读取该文件。
1
2
3
4
5
6
7
8
9
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
<? xml version = "1.0" encoding = "UTF-8" ?> < configuration scan = "true" scanPeriod = "3 seconds" > < property name = "z_app" value = "${z_app:-app}" /> < property name = "SQL_LEVEL" value = "DEBUG" /> < property name = "SERVICE_LEVEL" value = "DEBUG" /> < property name = "RPC_LEVEL" value = "DEBUG" /> < property name = "BOOT_LEVEL" value = "DEBUG" /> < property name = "BUF_LEVEL" value = "DEBUG" /> <!-- 09:14:20.256 [main] INFO com.h.db.business.main.Main - asfdasdf --> < property name = "PATTERN" value = "%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n" /> < property name = "SQL_LOG_FILE" value = "${LOG_HOME}/logs/${z_app}/${z_app}Sql.log" /> < property name = "SERVICE_LOG_FILE" value = "${LOG_HOME}/logs/${z_app}/${z_app}Service.log" /> < property name = "RPC_LOG_FILE" value = "${LOG_HOME}/logs/${z_app}/${z_app}RPC.log" /> < property name = "BOOT_LOG_FILE" value = "${LOG_HOME}/logs/${z_app}/${z_app}Boot.log" /> < property name = "BUF_LOG_FILE" value = "${LOG_HOME}/logs/${z_app}/${z_app}Buf.log" /> < property name = "TRACE_LOG_FILE" value = "${LOG_HOME}/logs/${z_app}/${z_app}Trace.log" /> < appender name = "CONSOLE" class = "ch.qos.logback.core.ConsoleAppender" > < encoder > < charset >utf-8</ charset > < pattern >${PATTERN}</ pattern > </ encoder > </ appender > < property name = "ROOT_LEVEL" value = "DEBUG" /> < property name = "APP_LOG_FILE" value = "${LOG_HOME}/logs/${z_app}/${z_app}.log" /> < appender name = "APP_LOG" class = "ch.qos.logback.core.rolling.RollingFileAppender" > < encoder > < charset >utf-8</ charset > < pattern >${PATTERN}</ pattern > </ encoder > < prudent >false</ prudent > < append >true</ append > < file >${APP_LOG_FILE}</ file > < rollingPolicy class = "ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy" > < fileNamePattern >${APP_LOG_FILE}.%d{yyyy-MM-dd}-%i.gz</ fileNamePattern > < maxFileSize >10MB</ maxFileSize > < maxHistory >15</ maxHistory > < totalSizeCap >200MB</ totalSizeCap > </ rollingPolicy > </ appender > <!-- 打印具体sql --> < logger name = "SqlLog" level = "DEBUG" > < appender-ref ref = "APP_LOG" /> < appender-ref ref = "CONSOLE" /> </ logger > <!-- 打印事务轨迹 --> < logger name = "druid.sql.Connection" level = "DEBUG" /> < logger name = "org.springframework.jdbc.datasource.CoreDataSourceUtils" level = "DEBUG" /> < logger name = "org.springframework.jdbc.datasource.DataSourceUtils" level = "DEBUG" /> < logger name = "org.apache.zookeeper.ClientCnxn" level = "info" /> < logger name = "org.mongodb.driver.connection" level = "info" /> < logger name = "org.mongodb.driver.cluster" level = "info" /> < logger name = "org.apache.zookeeper.Environment" level = "ERROR" /> < logger name = "org.apache.curator.framework.recipes.cache.TreeCache" level = "debug" /> < logger name = "tk.mybatis.mapper.mapperhelper.MapperTemplate" level = "DEBUG" /> < root level = "${ROOT_LEVEL}" > < appender-ref ref = "APP_LOG" /> < appender-ref ref = "CONSOLE" /> </ root > </ configuration > |
代码:
1
2
|
// 或者LoggerFactory.getLogger(${logger_name}),这里logger_name为logback.xml中logger标签定义的名称 private static final Logger logger2 = LoggerFactory.getLogger(XX. class ); |
常见问题
包冲突
如果出现:
1
|
org.slf4j.impl.Log4jLoggerFactory cannot be cast to ch.qos.logback.classic.LoggerContext |
或:
1
2
3
|
SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/E: /**/ WEB-INF/lib/activemq-all- 5.10 . 2 .jar!/org/slf4j/impl/StaticLoggerBinder. class ] SLF4J: Found binding in [jar:file:/E: /**/ WEB-INF/lib/slf4j-simple- 1.6 . 3 .jar!/org/slf4j/impl/StaticLoggerBinder. class ] |
表示有包冲突,将冲突的包删除即可。一般是由log4j,activemq-all等包导致,删除它们。
No such instance field: 'logger2'
一开始认为是编译缓存的原因,rebuild了好几次,都是同样的问题。
通过debugger的evaluate工具,执行LoggerFactory.getLogger("SqlLog").info("testtest"),又是正常的,可以正确在控制台和文件中输出日志内容。这说明,代码是没有问题的,且获取的logger实例也正确(logback的)。
通过evaluate工具,输入logger2始终有问题,但是输入logger却没有问题,logger.info会报错,提示是log4j的包。这已经说明.class不是最新的了。因为logger是修改之前定义了,后来改成了logger2。
只是,断点的时候,代码行的位置并没有错乱。和文件不是最新的似乎有点矛盾。
最后,发现还是编译缓存的原因。
将.class文件手动清除,重新编译启动,问题解决。
看来,idea的rebuild有时候并不靠谱,无法保证.class文件是最新的。
以上就是Java logback日志的使用的详细内容,更多关于Java logback日志的资料请关注服务器之家其它相关文章!
原文链接:https://segmentfault.com/a/1190000039671131