前言
日志记录这块,简单的控制台打印也是日志,输出log文件也是日志,跨服务器输出日志,也是日志。这还没说复杂的日志监控,日志监控,需要对代码打印日志的标准有要求。这个就比较复杂了,
首先我们要知道,日志是有级别的,log4j,我记得好像有7个级别,slf4j,有五个级别。首先我们得知道log4j和slf4j是什么东西,为啥把这两个放在一起说。题外话说一下命名,这个4j应该不少人有疑问,因为这东西不符合Java的驼峰命名规则啊,为啥要起这个名呢,好吧,其实就是For Java的音译加缩写。据说是传统,比如什么dom4j等等。这个级别是用来控制日志输出内容的,你设置的级别越高,打印的日志就越少,你配置级别最低,则打印全部级别的日志。准确来说是包含关系,就像一个金字塔结构一样。
还有Log4j和Slf4j这两个并不负责输出日志,准确的说这两个是Java日志的门面,又或者是标准,门面是什么意思,就是服务员,你去吃饭,只需要和服务员说,我要一碗蛋炒饭。至于服务员去找哪个厨师帮你做,你不用管,这两个东西也是一样,你和slf4j说,我要打印日志,slf4j得去找实现给你打印日志,但是只有slf4j是没有办法给你打印日志的。这个时候你就需要去找一个厨师,就是实现。logback是直接实现了slf4j的一个日志实现。好了。应该说明白了。
实际上logback官网上是有十分详细的介绍,尬就尬在,是英文文档。
依赖
普通的Java项目
1 2 3
| implementation 'ch.qos.logback:logback-classic:1.2.10' implementation 'ch.qos.logback:logback-core:1.2.10' implementation 'org.slf4j:slf4j-api:1.7.32'
|
springboot中无需此依赖,默认的日志就是lagback。
配置文件介绍
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
| <?xml version="1.0" encoding="UTF-8" ?>
<configuration scan="true" scanPeriod="2 seconds" debug="false">
<contextName>demo</contextName>
<springProperty scope="context" name="app_name" source="logging.file.name"/>
<property name="log.dir" value="logs"/>
<appender name="fileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender"> <append>true</append> <prudent>false</prudent> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>logs/%d{yyyy-MM-dd}/info%i.log</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 %level [%thread] %logger{10}.%class{0}#%method[%file:%line] %n%msg%n</pattern> <charset>utf-8</charset> </encoder> </appender>
<appender name="errorAppender" class="ch.qos.logback.core.rolling.RollingFileAppender"> <prudent>true</prudent>
<File>logs/error.log</File> <append>true</append>
<prudent>false</prudent> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>logs/%d{yyyy-MM-dd}/error%i.log</fileNamePattern> <maxHistory>30</maxHistory> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>10MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> <encoder> <pattern>%date %level [%thread] %logger{10}.%class{0}#%method[%file:%line] %n%msg%n</pattern> <charset>utf-8</charset> </encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>ERROR</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> </appender>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender"> <layout class="ch.qos.logback.classic.PatternLayout"> <pattern>%date %level [%thread] %logger{10}.%class{0}#%method[%file:%line] %n%msg%n</pattern> </layout> </appender>
<springProfile name="dev"> <root level="DEBUG"> <appender-ref ref="STDOUT" /> </root> </springProfile>
<springProfile name="prod"> <root level="INFO"> <appender-ref ref="FILE_LOG" /> </root> </springProfile>
<root level="debug"> <appender-ref ref="console"/> <appender-ref ref="fileAppender"/> <appender-ref ref="errorAppender"/> </root> </configuration>
|
应该是介绍的比较详细了,因为这东西平时很少有人去专门去记一下,我也是弄好一套,然后CV一下就可以了,注意上面的配置只是用来演示的,并不能直接CV下来用。下面我配置几种常用的配置,可以直接CV下来用的。
全部日志一个文件
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
| <?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<appender name="soutAppender" class="ch.qos.logback.core.ConsoleAppender"> <layout class="ch.qos.logback.classic.PatternLayout"> <pattern>%date %level [%thread] %logger{10}.%class{0}#%method[%file:%line] %n%msg%n</pattern> </layout> </appender>
<appender name="fileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender"> <append>true</append> <prudent>false</prudent> <File>logs/log.log</File> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>logs/%d{yyyy-MM-dd}/log%i.log</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 %level [%thread] %logger{10}.%class{0}#%method[%file:%line] %n%msg%n</pattern> <charset>utf-8</charset> </encoder> </appender>
<root level="all"> <appender-ref ref="soutAppender"/> <appender-ref ref="fileAppender"/> </root> </configuration>
|
不同级别不同文件
控制台打印太基础我就不说了,加强主要针对log文件,这个配置我们按日志级别输出对应级别的日志。方便我们定位和查找。一般来说,我们只需要info和error级别的日志即可。
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
| <configuration>
<appender name="soutAppender" class="ch.qos.logback.core.ConsoleAppender"> <layout class="ch.qos.logback.classic.PatternLayout"> <pattern>%date %level [%thread] %logger{10}.%class{0}#%method[%file:%line] %n%msg%n</pattern> </layout> </appender>
<appender name="infoAppender" class="ch.qos.logback.core.rolling.RollingFileAppender"> <append>true</append> <prudent>false</prudent> <File>logs/info.log</File> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>logs/%d{yyyy-MM-dd}/info%i.log</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 %level [%thread] %logger{10}.%class{0}#%method[%file:%line] %n%msg%n</pattern> <charset>utf-8</charset> </encoder> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>WARN</level> <onMatch>DENY</onMatch> <onMismatch>ACCEPT</onMismatch> </filter> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>ERROR</level> <onMatch>DENY</onMatch> <onMismatch>ACCEPT</onMismatch> </filter> </appender>
<appender name="errorAppender" class="ch.qos.logback.core.rolling.RollingFileAppender"> <append>true</append> <prudent>false</prudent> <File>logs/error.log</File> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>logs/%d{yyyy-MM-dd}/error%i.log</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 %level [%thread] %logger{10}.%class{0}#%method[%file:%line] %n%msg%n</pattern> <charset>utf-8</charset> </encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>WARN</level> </filter> </appender>
<root level="all"> <appender-ref ref="soutAppender"/> <appender-ref ref="infoAppender"/> <appender-ref ref="errorAppender"/> </root> </configuration>
|
最终结果是会输出两个日志文件,info.log
和error.log
, info包括info级别以及以下级别的日志,error.log
包括warn
和error
两个级别的日志。
不同包不同日志文件
我们一般的系统基本都有几层架构,有controller层,以及service层,logback可以根据不同的包来输出日志,也可以精确到具体的某一个类名。主要使用logger
这个标签来完成。这个没有办法CV,因为不同的项目包名不一样,根据自己的项目需要修改一下。
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
| <configuration>
<appender name="soutAppender" class="ch.qos.logback.core.ConsoleAppender"> <layout class="ch.qos.logback.classic.PatternLayout"> <pattern>%date %level [%thread] %logger{10}.%class{0}#%method[%file:%line] %n%msg%n</pattern> </layout> </appender>
<appender name="infoAppender" class="ch.qos.logback.core.rolling.RollingFileAppender"> <append>true</append> <prudent>false</prudent> <File>logs/info.log</File> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>logs/%d{yyyy-MM-dd}/info%i.log</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 %level [%thread] %logger{10}.%class{0}#%method[%file:%line] %n%msg%n</pattern> <charset>utf-8</charset> </encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>WARN</level> <onMatch>DENY</onMatch> <onMismatch>ACCEPT</onMismatch> </filter> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>ERROR</level> <onMatch>DENY</onMatch> <onMismatch>ACCEPT</onMismatch> </filter> </appender>
<appender name="errorAppender" class="ch.qos.logback.core.rolling.RollingFileAppender"> <append>true</append> <prudent>false</prudent> <File>logs/error.log</File> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>logs/%d{yyyy-MM-dd}/error%i.log</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 %level [%thread] %logger{10}.%class{0}#%method[%file:%line] %n%msg%n</pattern> <charset>utf-8</charset> </encoder> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>WARN</level> </filter> </appender>
<appender name="wlfAppender" class="ch.qos.logback.core.rolling.RollingFileAppender"> <append>true</append> <prudent>false</prudent> <File>logs/wlf/wlf.log</File> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>logs/wlf/%d{yyyy-MM-dd}/wlf%i.log</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 %level [%thread] %logger{10}.%class{0}#%method[%file:%line] %n%msg%n</pattern> <charset>utf-8</charset> </encoder> </appender>
<logger name="com.wlf" level="debug"> <appender-ref ref="wlfAppender"/> </logger>
<root level="all"> <appender-ref ref="soutAppender"/> <appender-ref ref="infoAppender"/> <appender-ref ref="errorAppender"/> </root> </configuration>
|
这个不怎么常用,只需要知道就行了,一般只使用第二个配置就行了,直接CV就行。至于和springboot中使用,可以整,但是没啥必要。具体的配置还是多看看介绍,多多学习。或者直接CV。😁😁
尾
主要内容到这里就介绍完了,logback的配置十分的灵活,但想要灵活的使用,还是需要多多的了解。当然打印日志归打印日志,如果你写代码打日志时不按照标准输出,再牛逼的配置也救不了。打印日志也需要规范一点。开发使用debug,需要留存的日志信息用info,异常信息用error。