springboot-logback

Posted exact

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot-logback相关的知识,希望对你有一定的参考价值。

 

springboot logback配置

1.pattern解析:

  %d{yyyy-MM-dd‘T‘HH:mm:ss.SSSXXX}   带时区的时间

  %level   日志级别

  [%thread]   线程名

  [%logger{50}:%line]    打印日志对应的方法和行数
  [uuid:%X{operation_id}]   这个是logback的MDC机制,没有使用mdc的话不需要用这个字段
  %msg%n          这个是要打印的数据,%n是换行
  %caller{2, DISP_CALLER_EVAL}   这个是指打印调用栈,即当前方法和调用这个方法的父级方法(这里配置2,只会往上打印一层)

<property name="error.pattern" value="%d{yyyy-MM-dd‘T‘HH:mm:ss.SSSXXX} %level [%thread] [%logger{50}:%line] [uuid:%X{operation_id}] %msg%n %caller{2, DISP_CALLER_EVAL}"></property>

2.将不同级别的日志打印到不同的文件:
  下面的一句话表示这个appender只接受error日志的级别,其它级别的日志丢弃
      <filter class="ch.qos.logback.classic.filter.LevelFilter">
             <level>ERROR</level>
             <onMatch>ACCEPT</onMatch>
             <onMismatch>DENY</onMismatch>
         </filter>
3.logger配置
  每个logger可以配置对应的package,打印级别,additivity="true"表示这个logger匹配的日志,依然会抛到上层,父logger(也就是root),root可以打印到控制台
  
appender-ref 指定appender,每个appender定义具体的工具方式,打印、压缩策略
      <logger name="com.hikvision.log" level="DEBUG" additivity="false">
              <appender-ref ref="only-error" additivity="false"></appender-ref>
              <appender-ref ref="no-error"></appender-ref>
         </logger>
  1 <configuration scan="true" scanPeriod="30 seconds">
  2 
  3     <!-- <property name="FILE_PATH" value="./logs"></property> -->
  4     <property name="model" value="elk"></property>
  5     <!-- <property name="USER_NAME" value="gc"></property>
  6     <property name="OS" value="win"></property> -->
  7 
  8     <contextName>my_logback_test</contextName>
  9 
 10     <property name="log.pattern"
 11         value="%d{yyyy-MM-dd‘T‘HH:mm:ss.SSSXXX} %level [%thread] [%logger{50}:%line] [uuid:%X{operation_id}] %msg%n"></property>
 12     <property name="error.pattern"
 13         value="%d{yyyy-MM-dd‘T‘HH:mm:ss.SSSXXX} %level [%thread] [%logger{50}:%line] [uuid:%X{operation_id}] %msg%n %caller{2, DISP_CALLER_EVAL}"></property>
 14 
 15     <appender name="no-error"
 16         class="ch.qos.logback.core.rolling.RollingFileAppender">
 17         <file>${log.path:-./logs}/${model}.log</file>
 18         <encoder>
 19             <pattern>${log.pattern}</pattern>
 20             <charset>UTF-8</charset>
 21         </encoder>
 22         <rollingPolicy
 23             class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
 24             <fileNamePattern>${model}.%i.log.zip</fileNamePattern>
 25             <minIndex>1</minIndex>
 26             <maxIndex>10</maxIndex>
 27         </rollingPolicy>
 28         <triggeringPolicy
 29             class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
 30             <maxFileSize>20MB</maxFileSize>
 31         </triggeringPolicy>
 32         <filter class="ch.qos.logback.classic.filter.LevelFilter">
 33             <level>TRACE</level>
 34             <onMatch>ACCEPT</onMatch>
 35             <onMismatch>NEUTRAL</onMismatch>
 36         </filter>
 37         <filter class="ch.qos.logback.classic.filter.LevelFilter">
 38             <level>DEBUG</level>
 39             <onMatch>ACCEPT</onMatch>
 40             <onMismatch>NEUTRAL</onMismatch>
 41         </filter>
 42         <filter class="ch.qos.logback.classic.filter.LevelFilter">
 43             <level>INFO</level>
 44             <onMatch>ACCEPT</onMatch>
 45             <onMismatch>NEUTRAL</onMismatch>
 46         </filter>
 47         <filter class="ch.qos.logback.classic.filter.LevelFilter">
 48             <level>WARN</level>
 49             <onMatch>ACCEPT</onMatch>
 50             <onMismatch>NEUTRAL</onMismatch>
 51         </filter>
 52     </appender>
 53 
 54     <appender name="only-error"
 55         class="ch.qos.logback.core.rolling.RollingFileAppender">
 56         <file>${log.path:-./logs}/${model}.error.log</file>
 57         <encoder>
 58             <pattern>${error.pattern}</pattern>
 59             <charset>UTF-8</charset>
 60         </encoder>
 61         <rollingPolicy
 62             class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
 63             <fileNamePattern>${model}.%i.log.zip</fileNamePattern>
 64             <minIndex>1</minIndex>
 65             <maxIndex>10</maxIndex>
 66         </rollingPolicy>
 67         <triggeringPolicy
 68             class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
 69             <maxFileSize>20MB</maxFileSize>
 70         </triggeringPolicy>
 71         <filter class="ch.qos.logback.classic.filter.LevelFilter">
 72             <level>ERROR</level>
 73             <onMatch>ACCEPT</onMatch>
 74             <onMismatch>DENY</onMismatch>
 75         </filter>
 76     </appender>
 77 
 78     <appender name="includedConsole"
 79         class="ch.qos.logback.core.ConsoleAppender">
 80         <encoder>
 81             <pattern>${log.pattern}</pattern>
 82         </encoder>
 83     </appender>
 84 
 85 
 86     <root level="INFO">
 87         <appender-ref ref="includedConsole"></appender-ref>
 88     </root>
 89     <springProfile name="prod">
 90         <logger name="com.hikvision.log" level="DEBUG" additivity="false">
 91             <appender-ref ref="only-error" additivity="false"></appender-ref>
 92             <appender-ref ref="no-error"></appender-ref>
 93         </logger>
 94     </springProfile>
 95     <springProfile name="dev">
 96         <logger name="com.hikvision.log" level="DEBUG" additivity="true">
 97             <appender-ref ref="only-error" additivity="false"></appender-ref>
 98             <appender-ref ref="no-error"></appender-ref>
 99         </logger>
100     </springProfile>
101     <springProfile name="test">
102         <logger name="com.hikvision.log" level="DEBUG" additivity="true">
103             <appender-ref ref="only-error" additivity="false"></appender-ref>
104             <appender-ref ref="no-error"></appender-ref>
105         </logger>
106     </springProfile>
107 
108 </configuration>

 










以上是关于springboot-logback的主要内容,如果未能解决你的问题,请参考以下文章

微信小程序代码片段

VSCode自定义代码片段——CSS选择器

谷歌浏览器调试jsp 引入代码片段,如何调试代码片段中的js

片段和活动之间的核心区别是啥?哪些代码可以写成片段?

VSCode自定义代码片段——.vue文件的模板

VSCode自定义代码片段6——CSS选择器