logback个人使用配置
提供一个目前个人供词使用的,无需日志汇总的日志配置文件:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <configuration scan="true" scanPeriod="10 seconds" debug="true" packagingData="false"> 3 4 <contextName>xxx</contextName> 5 <!-- 状态监听器 --> 6 <!--<statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener" />--> 7 8 <!-- 日志级别变更穿透 --> 9 <contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator" /> 10 11 <!-- drop duplicate message --> 12 <turboFilter class="ch.qos.logback.classic.turbo.DuplicateMessageFilter"/> 13 14 <!--<property scope="context" name="logEnv" value="${log.env}" />--> 15 <property scope="context" resource="log.properties" /> 16 <!--<property scope="context" name="log.file.path" value="/usr/local/logs" />--> 17 <!--<property scope="context" name="smtp.enable" value="true" />--> 18 <!--<property scope="context" name="smtp.host" value="smtp.126.com" />--> 19 <!--<property scope="context" name="smtp.port" value="25" />--> 20 <!--<property scope="context" name="smtp.username" value="xxx@126.com" />--> 21 <!--<property scope="context" name="smtp.password" value="xxx" />--> 22 <!--<property scope="context" name="smtp.from" value="xxx@126.com" />--> 23 <!--<property scope="context" name="smtp.to" value="xxx@126.com" />--> 24 <!--<property scope="context" name="max.history" value="30" />--> 25 <!--<property scope="context" name="file.html.size" value="512MB" />--> 26 <!--<property scope="context" name="file.html.enable" value="true" />--> 27 28 <!-- 多应用区分 --> 29 <!--<timestamp key="byDay" datePattern="yyyyMMdd" timeReference="contextBirth" />--> 30 31 <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> 32 <encoder> 33 <pattern>%green(%d{yyyy-MM-dd HH:mm:ss.SSS}) %magenta([%thread]) %highlight(%-5level) %cyan(%logger{36}) - %msg%n</pattern> 34 </encoder> 35 </appender> 36 37 <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> 38 <file>${log.file.path}/query.log</file> 39 <append>true</append> 40 <!-- set immediateFlush to false for much higher logging throughput --> 41 <immediateFlush>true</immediateFlush> 42 43 <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> 44 <!-- daily rollover --> 45 <fileNamePattern>${log.file.path}/query.%d{yyyy-MM-dd}.log.gz</fileNamePattern> 46 47 <!-- keep 30 days' worth of history capped at 3GB total size --> 48 <maxHistory>${max.history:-30}</maxHistory> 49 <!--<totalSizeCap>3GB</totalSizeCap>--> 50 51 </rollingPolicy> 52 53 <!-- encoders are assigned the type 54 ch.qos.logback.classic.encoder.PatternLayoutEncoder by default --> 55 <encoder> 56 <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> 57 </encoder> 58 </appender> 59 60 <appender name="HTML_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> 61 62 <file>${log.file.path}/query.jsp</file> 63 <filter class="ch.qos.logback.core.filter.EvaluatorFilter"> 64 <evaluator class="ch.qos.logback.classic.boolex.GEventEvaluator"> 65 <expression> 66 ${file.html.enable} =~ /true/ 67 </expression> 68 </evaluator> 69 <OnMismatch>DENY</OnMismatch> 70 <OnMatch>ACCEPT</OnMatch> 71 </filter> 72 <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> 73 <!-- rollover daily --> 74 <fileNamePattern>${log.file.path}/query.%d{yyyy-MM-dd}.%i.html</fileNamePattern> 75 <maxFileSize>${file.html.size:-512MB}</maxFileSize> 76 <maxHistory>1</maxHistory> 77 <totalSizeCap>${file.html.size:-512MB}</totalSizeCap> 78 </rollingPolicy> 79 <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder"> 80 <layout class="ch.qos.logback.classic.html.HTMLLayout"> 81 <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg</pattern> 82 </layout> 83 </encoder> 84 </appender> 85 86 <!-- marker email send --> 87 <appender name="EMAIL" class="ch.qos.logback.classic.net.SMTPAppender"> 88 <!-- event描述信息 --> 89 <discriminator class="ch.qos.logback.classic.sift.MDCBasedDiscriminator"> 90 <key>host</key> 91 <defaultValue>none</defaultValue> 92 </discriminator> 93 <!-- email --> 94 <smtpHost>${smtp.host}</smtpHost> 95 <smtpPort>${smtp.port}</smtpPort> 96 <username>${smtp.username}</username> 97 <password>${smtp.password}</password> 98 <to>${smtp.to}</to> 99 <from>${smtp.from}</from> 100 <subject>【ERROR】: %logger{0} from %X{host}</subject> 101 <layout class="ch.qos.logback.classic.PatternLayout"> 102 <pattern>%date %-5level %logger{35} - %message%n</pattern> 103 </layout> 104 <filter class="ch.qos.logback.core.filter.EvaluatorFilter"> 105 <evaluator class="ch.qos.logback.classic.boolex.GEventEvaluator"> 106 <expression> 107 ${smtp.enable} =~ /true/ 108 </expression> 109 </evaluator> 110 <OnMismatch>DENY</OnMismatch> 111 <OnMatch>ACCEPT</OnMatch> 112 </filter> 113 <evaluator class="ch.qos.logback.classic.boolex.OnMarkerEvaluator"> 114 <marker>NOTIFY</marker> 115 <!-- you specify add as many markers as you want --> 116 <!--<marker>TRANSACTION_FAILURE</marker>--> 117 </evaluator> 118 <cyclicBufferTracker class="ch.qos.logback.core.spi.CyclicBufferTracker"> 119 <!-- send just one log entry per email --> 120 <bufferSize>1</bufferSize> 121 </cyclicBufferTracker> 122 </appender> 123 124 <appender name="ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender"> 125 <file>${log.file.path}/query-error.log</file> 126 <filter class="ch.qos.logback.classic.filter.LevelFilter"> 127 <level>ERROR</level> 128 <onMatch>ACCEPT</onMatch> 129 <onMismatch>DENY</onMismatch> 130 </filter> 131 <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> 132 <!-- daily rollover --> 133 <fileNamePattern>${log.file.path}/query-error.%d{yyyy-MM-dd}.log.gz</fileNamePattern> 134 <maxHistory>${max.history:-30}</maxHistory> 135 </rollingPolicy> 136 <encoder> 137 <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> 138 </encoder> 139 </appender> 140 141 <logger name="com.qding" level="${log.level:-info}"> 142 <appender-ref ref="ERROR" /> 143 </logger> 144 145 <root level="${log.level:-info}"> 146 <appender-ref ref="STDOUT" /> 147 <appender-ref ref="FILE" /> 148 <appender-ref ref="HTML_FILE" /> 149 <appender-ref ref="EMAIL" /> 150 </root> 151 <!--<if condition='p("logEnv").contains("dev")'>--> 152 <!--<then>--> 153 <!--<root level="info">--> 154 <!--<appender-ref ref="STDOUT" />--> 155 <!--</root>--> 156 <!--</then>--> 157 <!--<else>--> 158 <!--<root level="debug">--> 159 <!--<appender-ref ref="STDOUT" />--> 160 <!--</root>--> 161 <!--</else>--> 162 <!--</if>--> 163 164 165 </configuration>
jar包的maven依赖:
<dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-access</artifactId> <version>1.2.3</version> </dependency> <!-- 日志切换 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>1.7.5</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>log4j-over-slf4j</artifactId> <version>1.7.12</version> </dependency> <dependency> <groupId>org.logback-extensions</groupId> <artifactId>logback-ext-spring</artifactId> <version>0.1.2</version> </dependency> <dependency> <groupId>org.codehaus.janino</groupId> <artifactId>janino</artifactId> <version>3.0.7</version> </dependency>