前言
回想以前都是用 System.out.println
作为调试的主要手段的, 但是当部署到服务器时, 我们不可能一直盯着控制台看, 并且这种方法也不能进行分级输出。 System.out.println
也就满足不了需求了。
Logback
是一个 Java 领域的日志框架。它被认为是 Log4J
的继承人,实现了 SLF4J
标准。反正就是个特好用的东西。
导入maven
Spring 配置
1 2 3 4 5 6 7
| <listener> <listener-class>ch.qos.logback.ext.spring.web.LogbackConfigListener</listener-class> </listener> <context-param> <param-name>logbackConfigLocation</param-name> <param-value>classpath:logback.xml</param-value> </context-param>
|
Spring Boot直接放在 resource/logback.xml
即可, 会自动配置加载。
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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
| <?xml version="1.0" encoding="UTF-8"?> <configuration debug="false"> <property name="log.context.name" value="MyApp" /> <property name="log.charset" value="UTF-8" /> <property name="log.history.max" value="30" /> <property name="log.path" value="LOG" />
<property name="log.pattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n" /> <property name="log.pattern.short" value="%date{yyyyMMdd HH:mm:ss.SSS}-%msg%n" />
<contextName>${log.context.name}</contextName>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder charset="${log.charset}"> <pattern>${log.pattern}</pattern> </encoder> </appender> <appender name="STDOUT_SHORT" class="ch.qos.logback.core.ConsoleAppender"> <encoder charset="${log.charset}"> <pattern>${log.pattern.short}</pattern> </encoder> </appender>
<appender name="FILE_ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>ERROR</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${log.path}/error/log.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>${log.history.max}</maxHistory> </rollingPolicy> <encoder charset="${log.charset}"> <pattern>${log.pattern}</pattern> </encoder> </appender>
<appender name="FILE_WARN" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>WARN</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${log.path}/warn/log.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>${log.history.max}</maxHistory> </rollingPolicy> <encoder charset="${log.charset}"> <pattern>${log.pattern}</pattern> </encoder> </appender>
<appender name="FILE_INFO" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>INFO</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${log.path}/info/log.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>${log.history.max}</maxHistory> </rollingPolicy> <encoder charset="${log.charset}"> <pattern>${log.pattern}</pattern> </encoder> </appender>
<appender name="FILE_DEBUG" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>DEBUG</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${log.path}/debug/log.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>${log.history.max}</maxHistory> </rollingPolicy> <encoder charset="${log.charset}"> <pattern>${log.pattern}</pattern> </encoder> </appender>
<appender name="FILE_TRACE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>TRACE</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${log.path}/trace/log.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>${log.history.max}</maxHistory> </rollingPolicy> <encoder charset="${log.charset}"> <pattern>${log.pattern}</pattern> </encoder> </appender>
<logger name="com.apache.ibatis" level="TRACE"/> <logger name="java.sql.Connection" level="DEBUG"/> <logger name="java.sql.Statement" level="DEBUG"/> <logger name="java.sql.PreparedStatement" level="DEBUG"/> <logger name="com.ahao.project" level="INFO"/>
<root level="INFO"> <appender-ref ref="STDOUT" /> <appender-ref ref="FILE_ERROR" /> <appender-ref ref="FILE_INFO" /> <appender-ref ref="FILE_WARN" /> <appender-ref ref="FILE_DEBUG" /> <appender-ref ref="FILE_TRACE" /> </root> </configuration>
|
参考资料