logback是log4j作者设计的另一个开源日志组件。相对于log4j, 它拥有相当多的优势。比如更快的速度,在一些关键性能上提升10倍以上,而且初始化加载内存也更小了。此外,Logback-classic非常自然地实现了slf4j, 拥有充分的 文档等。因此使用logback来做日志的处理是我们一个非常不错的选择。
logback模块
- logback-core: 核心模块,是其他两个模块的基础模块
- logback-classic: 是log4j的一个改良版本,同时完整实现了slf4j API使你可以很方便的更换其他日志系统。
- logback-access: 访问模块,与servlet容器集成提供通过Http来访问日志的功能。
使用
添加maven依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| <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
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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
| <?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 second" debug="false"> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <Pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%5level] [%thread] %logger{0} %msg%n</Pattern> <charset>UTF-8</charset> </encoder> </appender> <appender name="DEBUG" class="ch.qos.logback.core.FileAppender"> <file>debug.log</file> <append>true</append> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>DEBUG</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <encoder> <Pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%5level] [%thread] %logger{0} %msg%n</Pattern> <charset>UTF-8</charset> </encoder> </appender> <appender name="INFO" class="ch.qos.logback.core.rolling.RollingFileAppender"> <File>info.log</File> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>INFO</level> </filter> <encoder> <Pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%5level] [%thread] %logger{0} %msg%n</Pattern> <charset>UTF-8</charset> </encoder> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>info.%d{yyyy-MM-dd}.log</fileNamePattern> <maxHistory>30</maxHistory> <TimeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>100MB</maxFileSize> </TimeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> </appender > <appender name="ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>error.log</file> <encoder> <Pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%5level] [%thread] %logger{0} %msg%n</Pattern> <charset>UTF-8</charset> </encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy"> <fileNamePattern>error.%d{yyyy-MM}(%i).log.zip</fileNamePattern> <minIndex>1</minIndex> <maxIndex>3</maxIndex> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>100MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> <maxHistory>30</maxHistory> </rollingPolicy> </appender> <appender name ="ASYNC" class= "ch.qos.logback.classic.AsyncAppender"> <discardingThreshold >0</discardingThreshold> <queueSize>512</queueSize> <appender-ref ref ="ERROR"/> </appender>
<logger name="ch.qos.logback" />
<logger name="com.weizhi.common.LogMain" level="INFO" additivity="false"> <appender-ref ref="STDOUT"/> <appender-ref ref="ASYNC"/> </logger>
<root level="DEBUG"> <appender-ref ref="STDOUT"/> <appender-ref ref="ASYNC"/> </root> </configuration>
|
其他参考
从零开始玩转logback
logback配置详解
logback配置解析