参考文档
看一下常用配置:
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 <?xml version="1.0" encoding="UTF-8" ?> <configuration scan ="true" scanPeriod ="60 seconds" debug ="false" > <contextName > route-log</contextName > <property name ="logback.logdir" value ="/var/log/xxx" /> <property name ="logback.appname" value ="xxx-app" /> <appender name ="consoleLog" class ="ch.qos.logback.core.ConsoleAppender" > <layout class ="ch.qos.logback.classic.PatternLayout" > <pattern > <pattern > %d %-5level [%thread] %logger{36} %line - %msg%n</pattern > </pattern > </layout > <filter class ="ch.qos.logback.classic.filter.ThresholdFilter" > <level > INFO</level > </filter > </appender > <appender name ="fileInfoUpLog" class ="ch.qos.logback.core.rolling.RollingFileAppender" > <filter class ="ch.qos.logback.classic.filter.ThresholdFilter" > <level > INFO</level > </filter > <File > ${logback.logdir}/${logback.appname}.log</File > <rollingPolicy class ="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy" > <fileNamePattern > ${logback.logdir}/${logback.appname}-%d{yyyy-MM}.%i.log</fileNamePattern > <maxFileSize > 100MB</maxFileSize > <maxHistory > 30</maxHistory > <totalSizeCap > 200MB</totalSizeCap > </rollingPolicy > <encoder > <charset > UTF-8</charset > <pattern > %d %-5level [%thread] %logger{36} %line - %msg%n</pattern > </encoder > </appender > <root level ="INFO" > <appender-ref ref ="consoleLog" /> <appender-ref ref ="fileInfoUpLog" /> </root > </configuration >
假如要压缩,达到一定大小后压缩,直接改文件名即可:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 <appender name ="FILE" class ="ch.qos.logback.core.rolling.RollingFileAppender" > <File > ${LOGDIR}/filename.log</File > <rollingPolicy class ="ch.qos.logback.core.rolling.TimeBasedRollingPolicy" > <FileNamePattern > ${LOGDIR}/file.%d{yyyy-MM-dd}.%i.log.gz </FileNamePattern > <MaxHistory > 30</MaxHistory > <timeBasedFileNamingAndTriggeringPolicy class ="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP" > <maxFileSize > 10MB</maxFileSize > </timeBasedFileNamingAndTriggeringPolicy > </rollingPolicy > <encoder class ="ch.qos.logback.classic.encoder.PatternLayoutEncoder" > <Pattern > %date [%thread] %-5level %logger{36} - %msg%n </Pattern > </encoder > </appender >
上面的代码将在当天压缩文件, 如果日志文件大小超过10MB。