07.`logback`日志

本文详细介绍了如何在SpringBoot项目中配置Logback,包括排除log4j依赖、配置不同级别的日志输出、使用异步日志以及设置日志文件的滚动策略。适用于开发和生产环境的日志管理。
1. 核心依赖

spring boot已经集成了logback,不用导依赖,只要导入了spring-boot-starter即可,可以排除掉log4j

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-to-slf4j</artifactId>
        </exclusion>
    </exclusions>
</dependency>
2. 配置文件
  1. 主配置文件

    logging:
      level:
        # 配置打印mybatis的sql语句
        com.test.shop: debug
      config: classpath:logback-spring-dev.xml
    
  2. logback配置文件

    1. logback-spring-dev.xml → 开发环境

      <?xml version="1.0" encoding="UTF-8"?>
      <!-- 
          scan: 当配置文件被修改后, 将会被重新载入。
          scanPeriod:配置监测配置文件是否有修改的时间间隔,如果没有给出时间单位,默认单位是毫秒。当scan为true时,此属性生效。默认的时间间隔为1分钟。
          debug: 当此属性设置为true时,将打印出logback内部日志信息,实时查看logback运行状态。默认值为false。 
      -->
      <configuration scan="true" scanPeriod="60 seconds">
          <!-- 名字随便起,这是开发环境的所以起名为develop -->
          <contextName>develop</contextName>
          
          <!-- 
              定义日志存放路径,通过${LOG_PATH}的方式可以引用该变量插入到logback的上下文
              当前盘符下的webapp/log/,如D:webapp/log/
              如果是Linux,则是根目录下的/webapp/log/
          -->
          <property name="LOG_PATH" value="/webapp/log/" />
          <!-- 定义日志输出格式 -->
          <property name="LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %5p --- [%t] %-40.40logger{39} : %m%n"/>
      
          <!--1. 输出到控制台-->
          <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
              <encoder>
                  <Pattern>${LOG_PATTERN}</Pattern>
                  <charset>utf-8</charset>
              </encoder>
          
              <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
                  <level>debug</level>
              </filter>
          </appender>
      
          <!-- info日志文件 -->
          <appender name="FILE-INFO" class="ch.qos.logback.core.rolling.RollingFileAppender">
              <!--常规日志文件,路径/文件名-->
              <file>${LOG_PATH}/log-info.log</file>
              <!-- 日志记录的滚动策略,按日期、大小进行记录 -->
              <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                  <!--归档日志文件-->
                  <fileNamePattern>${LOG_PATH}/info/log-info.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
                  <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                      <maxFileSize>30MB</maxFileSize>
                  </timeBasedFileNamingAndTriggeringPolicy>
                  <!-- 日志保留天数 -->
                  <maxHistory>15</maxHistory>
              </rollingPolicy> 
              <append>true</append>
              <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
                  <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{32} Line:%-3L - %msg%n</pattern>
                  <charset>utf-8</charset>
              </encoder>
              <filter class="ch.qos.logback.classic.filter.LevelFilter">
                  <level>info</level>
              </filter>
          </appender>
          
          <!-- warn 日志文件 -->
          <appender name="FILE-WARN" class="ch.qos.logback.core.rolling.RollingFileAppender">
              <!-- warn 日志文件 -->
              <file>${LOG_PATH}/log-warn.log</file>
              <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                  <!--归档日志文件-->
                  <fileNamePattern>${LOG_PATH}/warn/log-warn.%d{yyyy-MM}.%i.log</fileNamePattern>
                  <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                      <maxFileSize>30MB</maxFileSize>
                  </timeBasedFileNamingAndTriggeringPolicy>
                  <maxHistory>15</maxHistory>
              </rollingPolicy>
              <append>true</append>
              <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
                  <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger Line:%-3L - %msg%n</pattern>
                  <charset>utf-8</charset>
              </encoder>
              <filter class="ch.qos.logback.classic.filter.LevelFilter">
                  <level>WARN</level>
                  <onMatch>ACCEPT</onMatch>
                  <onMismatch>DENY</onMismatch>
              </filter>
          </appender>
          
          <!-- error 归档日志文件 -->
          <appender name="FILE-ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
              <!-- error 归档日志文件 -->
              <file>${LOG_PATH}/log-err.log</file>
              <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                  <!-- error 归档日志文件 -->
                  <fileNamePattern>${LOG_PATH}/err/log-err.%d{yyyy-MM}.%i.log</fileNamePattern>
                  <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                      <maxFileSize>30MB</maxFileSize>
                  </timeBasedFileNamingAndTriggeringPolicy>
                  <maxHistory>15</maxHistory>
              </rollingPolicy>
              <append>true</append>
              <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
                  <pattern>${LOG_PATTERN}</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="HTTP-INFO" class="ch.qos.logback.core.rolling.RollingFileAppender">
              <file>${LOG_PATH}/log-http.log</file>
              <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                  <fileNamePattern>${LOG_PATH}/http/log-http.%d{yyyy-MM}.%i.log</fileNamePattern>
                  <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                      <maxFileSize>30MB</maxFileSize>
                  </timeBasedFileNamingAndTriggeringPolicy>
                  <maxHistory>15</maxHistory>
              </rollingPolicy>
              <append>true</append>
              <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
                  <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger Line:%-3L - %msg%n</pattern>
                  <charset>utf-8</charset>
              </encoder>
              <filter class="ch.qos.logback.classic.filter.LevelFilter">
                  <level>info</level>
              </filter>
          </appender>
          
          <!-- 异步输出,异步的log片段必须在同步段后面,否则不起作用 -->
          <appender name="FILE-INFO-ASYNC" class="ch.qos.logback.classic.AsyncAppender">
              <!-- 不丢失日志.默认的,如果队列的80%已满,则会丢弃TRACT、DEBUG、INFO级别的日志 -->
              <discardingThreshold>0</discardingThreshold>
              <!-- 更改默认的队列的深度,该值会影响性能.默认值为256 -->
              <queueSize>256</queueSize>
              <!-- 添加附加的appender,最多只能添加一个 -->
              <appender-ref ref="FILE-INFO"/>
          </appender>
          
          <appender name="FILE-WARN-ASYNC" class="ch.qos.logback.classic.AsyncAppender">
              <!-- 不丢失日志.默认的,如果队列的80%已满,则会丢弃TRACT、DEBUG、INFO级别的日志 -->
              <discardingThreshold>0</discardingThreshold>
              <!-- 更改默认的队列的深度,该值会影响性能.默认值为256 -->
              <queueSize>256</queueSize>
              <!-- 添加附加的appender,最多只能添加一个 -->
              <appender-ref ref="FILE-WARN"/>
          </appender>
          
          <appender name="FILE-ERROR-ASYNC" class="ch.qos.logback.classic.AsyncAppender">
              <!-- 不丢失日志.默认的,如果队列的80%已满,则会丢弃TRACT、DEBUG、INFO级别的日志 -->
              <discardingThreshold>0</discardingThreshold>
              <!-- 更改默认的队列的深度,该值会影响性能.默认值为256 -->
              <queueSize>256</queueSize>
              <!-- 添加附加的appender,最多只能添加一个 -->
              <appender-ref ref="FILE-ERROR"/>
          </appender>
          
          <!-- 根日志,用来指定最基础的日志输出级别,只有一个level属性。-->
          <root level="info">
              <!-- 标识这个appender将会添加到这个loger -->
              <appender-ref ref="STDOUT"/>
              <appender-ref ref="FILE-INFO-ASYNC"/>
              <appender-ref ref="FILE-WARN-ASYNC"/>
              <appender-ref ref="FILE-ERROR-ASYNC"/>
          </root>
          
          <!-- 自定义包的日志级别 -->
      	<logger name="cn.zs" level="info"/>
      </configuration>
      
    2. 说明

      1. %d:日期转换,花括号中指定日期的格式。
      2. %-4relative:该条日志输出的时间,这个时间是相对于服务器启动到打印出这条日志的相对时间,4表示时间占用的宽度。
      3. %-5level:日志的级别,日志总共5个级别,分别是debug,info,warn,error,fatal,从左往右日志级别越高,5表示日志级别占用的字符宽度。
      4. %thread:该日志所属的线程。
      5. %logger{32}:输出该日志信息的类,32表示包的层级。
      6. %msg:日志信息。
      7. %n::换行。
    3. 注意

      1. 生产环境不要打印到控制台
Logging system failed to initialize using configuration from 'null' java.lang.IllegalStateException: Logback configuration error detected: ERROR in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Could not create component [layout] of type [org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout] java.lang.ClassNotFoundException: org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout ERROR in ch.qos.logback.core.joran.spi.Interpreter@13:26 - no applicable action for [pattern], current ElementPath is [[configuration][appender][encoder][layout][pattern]] ERROR in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Could not create component [layout] of type [org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout] java.lang.ClassNotFoundException: org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout ERROR in ch.qos.logback.core.joran.spi.Interpreter@23:26 - no applicable action for [pattern], current ElementPath is [[configuration][appender][encoder][layout][pattern]] ERROR in ch.qos.logback.core.joran.action.AppenderAction - Could not create an Appender of type [org.apache.skywalking.apm.toolkit.log.logback.v1.x.log.GRPCLogClientAppender]. ch.qos.logback.core.util.DynamicClassLoadingException: Failed to instantiate type org.apache.skywalking.apm.toolkit.log.logback.v1.x.log.GRPCLogClientAppender ERROR in ch.qos.logback.core.joran.spi.Interpreter@51:112 - ActionException in Action for tag [appender] ch.qos.logback.core.joran.spi.ActionException: ch.qos.logback.core.util.DynamicClassLoadingException: Failed to instantiate type org.apache.skywalking.apm.toolkit.log.logback.v1.x.log.GRPCLogClientAppender ERROR in ch.qos.logback.core.joran.action.AppenderRefAction - Could not find an appender named [GRPC]. Did you define it below instead of above in the configuration file? ERROR in ch.qos.logback.core.joran.action.AppenderRefAction - See http://logback.qos.ch/codes.html#appender_order for more details. at org.springframework.boot.logging.logback.LogbackLoggingSystem.reportConfigurationErrorsIfNecessary(LogbackLoggingSystem.java:189) at org.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration(LogbackLoggingSystem.java:170) at org.springframework.boot.logging.AbstractLoggingSystem.initializeWithConventions(AbstractLoggingSystem.java:80) at org.springframework.boot.logging.AbstractLoggingSystem.initialize(AbstractLoggingSystem.java:60) at org.springframework.boot.logging.logback.LogbackLoggingSystem.initialize(LogbackLoggingSystem.java:132) at org.springframework.boot.context.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:329) at org.springframework.boot.context.logging.LoggingApplicationListener.initialize(LoggingApplicationListener.java:298) at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEnvironmentPreparedEvent(LoggingApplicationListener.java:246) at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:223) at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:178) at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:171) at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:145) at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:133) at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:85) at org.springframework.boot.SpringApplicationRunListeners.lambda$environmentPrepared$2(SpringApplicationRunListeners.java:66) at java.util.ArrayList.forEach(ArrayList.java:1259) at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:120) at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:114) at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:65) at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:344) at org.springframework.boot.SpringApplication.run(SpringApplication.java:302) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1300) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1289) at cn.iocoder.gmy.module.finance.FinanceServerApplication.main(FinanceServerApplication.java:17) Suppressed: java.lang.ClassNotFoundException: org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout at java.net.URLClassLoader.findClass(URLClassLoader.java:382) at java.lang.ClassLoader.loadClass(ClassLoader.java:418) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355) at java.lang.ClassLoader.loadClass(ClassLoader.java:351) at ch.qos.logback.core.util.Loader.loadClass(Loader.java:120) at ch.qos.logback.core.joran.action.NestedComplexPropertyIA.begin(NestedComplexPropertyIA.java:102) at ch.qos.logback.core.joran.spi.Interpreter.callBeginAction(Interpreter.java:269) at ch.qos.logback.core.joran.spi.Interpreter.startElement(Interpreter.java:145) at ch.qos.logback.core.joran.spi.Interpreter.startElement(Interpreter.java:128) at ch.qos.logback.core.joran.spi.EventPlayer.play(EventPlayer.java:50) at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:165) at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:152) at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:110) at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:53) at org.springframework.boot.logging.logback.LogbackLoggingSystem.configureByResourceUrl(LogbackLoggingSystem.java:199) at org.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration(LogbackLoggingSystem.java:165) ... 22 more Suppressed: java.lang.ClassNotFoundException: org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout at java.net.URLClassLoader.findClass(URLClassLoader.java:382) at java.lang.ClassLoader.loadClass(ClassLoader.java:418) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355) at java.lang.ClassLoader.loadClass(ClassLoader.java:351) at ch.qos.logback.core.util.Loader.loadClass(Loader.java:120) at ch.qos.logback.core.joran.action.NestedComplexPropertyIA.begin(NestedComplexPropertyIA.java:102) at ch.qos.logback.core.joran.spi.Interpreter.callBeginAction(Interpreter.java:269) at ch.qos.logback.core.joran.spi.Interpreter.startElement(Interpreter.java:145) at ch.qos.logback.core.joran.spi.Interpreter.startElement(Interpreter.java:128) at ch.qos.logback.core.joran.spi.EventPlayer.play(EventPlayer.java:50) at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:165) at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:152) at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:110) at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:53) at org.springframework.boot.logging.logback.LogbackLoggingSystem.configureByResourceUrl(LogbackLoggingSystem.java:199) at org.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration(LogbackLoggingSystem.java:165) ... 22 more Suppressed: ch.qos.logback.core.util.DynamicClassLoadingException: Failed to instantiate type org.apache.skywalking.apm.toolkit.log.logback.v1.x.log.GRPCLogClientAppender at ch.qos.logback.core.util.OptionHelper.instantiateByClassNameAndParameter(OptionHelper.java:68) at ch.qos.logback.core.util.OptionHelper.instantiateByClassName(OptionHelper.java:44) at ch.qos.logback.core.util.OptionHelper.instantiateByClassName(OptionHelper.java:33) at ch.qos.logback.core.joran.action.AppenderAction.begin(AppenderAction.java:52) at ch.qos.logback.core.joran.spi.Interpreter.callBeginAction(Interpreter.java:269) at ch.qos.logback.core.joran.spi.Interpreter.startElement(Interpreter.java:145) at ch.qos.logback.core.joran.spi.Interpreter.startElement(Interpreter.java:128) at ch.qos.logback.core.joran.spi.EventPlayer.play(EventPlayer.java:50) at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:165) at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:152) at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:110) at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:53) at org.springframework.boot.logging.logback.LogbackLoggingSystem.configureByResourceUrl(LogbackLoggingSystem.java:199) at org.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration(LogbackLoggingSystem.java:165) ... 22 more Caused by: java.lang.ClassNotFoundException: org.apache.skywalking.apm.toolkit.log.logback.v1.x.log.GRPCLogClientAppender at java.net.URLClassLoader.findClass(URLClassLoader.java:382) at java.lang.ClassLoader.loadClass(ClassLoader.java:418) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355) at java.lang.ClassLoader.loadClass(ClassLoader.java:351) at ch.qos.logback.core.util.OptionHelper.instantiateByClassNameAndParameter(OptionHelper.java:55) ... 35 more Suppressed: ch.qos.logback.core.joran.spi.ActionException: ch.qos.logback.core.util.DynamicClassLoadingException: Failed to instantiate type org.apache.skywalking.apm.toolkit.log.logback.v1.x.log.GRPCLogClientAppender at ch.qos.logback.core.joran.action.AppenderAction.begin(AppenderAction.java:76) at ch.qos.logback.core.joran.spi.Interpreter.callBeginAction(Interpreter.java:269) at ch.qos.logback.core.joran.spi.Interpreter.startElement(Interpreter.java:145) at ch.qos.logback.core.joran.spi.Interpreter.startElement(Interpreter.java:128) at ch.qos.logback.core.joran.spi.EventPlayer.play(EventPlayer.java:50) at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:165) at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:152) at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:110) at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:53) at org.springframework.boot.logging.logback.LogbackLoggingSystem.configureByResourceUrl(LogbackLoggingSystem.java:199) at org.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration(LogbackLoggingSystem.java:165) ... 22 more [CIRCULAR REFERENCE:ch.qos.logback.core.util.DynamicClassLoadingException: Failed to instantiate type org.apache.skywalking.apm.toolkit.log.logback.v1.x.log.GRPCLogClientAppender] Exception in thread "main" java.lang.IllegalStateException: java.lang.IllegalStateException: Logback configuration error detected: ERROR in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Could not create component [layout] of type [org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout] java.lang.ClassNotFoundException: org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout ERROR in ch.qos.logback.core.joran.spi.Interpreter@13:26 - no applicable action for [pattern], current ElementPath is [[configuration][appender][encoder][layout][pattern]] ERROR in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Could not create component [layout] of type [org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout] java.lang.ClassNotFoundException: org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout ERROR in ch.qos.logback.core.joran.spi.Interpreter@23:26 - no applicable action for [pattern], current ElementPath is [[configuration][appender][encoder][layout][pattern]] ERROR in ch.qos.logback.core.joran.action.AppenderAction - Could not create an Appender of type [org.apache.skywalking.apm.toolkit.log.logback.v1.x.log.GRPCLogClientAppender]. ch.qos.logback.core.util.DynamicClassLoadingException: Failed to instantiate type org.apache.skywalking.apm.toolkit.log.logback.v1.x.log.GRPCLogClientAppender ERROR in ch.qos.logback.core.joran.spi.Interpreter@51:112 - ActionException in Action for tag [appender] ch.qos.logback.core.joran.spi.ActionException: ch.qos.logback.core.util.DynamicClassLoadingException: Failed to instantiate type org.apache.skywalking.apm.toolkit.log.logback.v1.x.log.GRPCLogClientAppender ERROR in ch.qos.logback.core.joran.action.AppenderRefAction - Could not find an appender named [GRPC]. Did you define it below instead of above in the configuration file? ERROR in ch.qos.logback.core.joran.action.AppenderRefAction - See http://logback.qos.ch/codes.html#appender_order for more details. at org.springframework.boot.context.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:344) at org.springframework.boot.context.logging.LoggingApplicationListener.initialize(LoggingApplicationListener.java:298) at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEnvironmentPreparedEvent(LoggingApplicationListener.java:246) at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:223) at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:178) at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:171) at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:145) at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:133) at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:85) at org.springframework.boot.SpringApplicationRunListeners.lambda$environmentPrepared$2(SpringApplicationRunListeners.java:66) at java.util.ArrayList.forEach(ArrayList.java:1259) at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:120) at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:114) at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:65) at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:344) at org.springframework.boot.SpringApplication.run(SpringApplication.java:302) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1300) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1289) at cn.iocoder.gmy.module.finance.FinanceServerApplication.main(FinanceServerApplication.java:17) Caused by: java.lang.IllegalStateException: Logback configuration error detected: ERROR in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Could not create component [layout] of type [org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout] java.lang.ClassNotFoundException: org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout ERROR in ch.qos.logback.core.joran.spi.Interpreter@13:26 - no applicable action for [pattern], current ElementPath is [[configuration][appender][encoder][layout][pattern]] ERROR in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Could not create component [layout] of type [org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout] java.lang.ClassNotFoundException: org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout ERROR in ch.qos.logback.core.joran.spi.Interpreter@23:26 - no applicable action for [pattern], current ElementPath is [[configuration][appender][encoder][layout][pattern]] ERROR in ch.qos.logback.core.joran.action.AppenderAction - Could not create an Appender of type [org.apache.skywalking.apm.toolkit.log.logback.v1.x.log.GRPCLogClientAppender]. ch.qos.logback.core.util.DynamicClassLoadingException: Failed to instantiate type org.apache.skywalking.apm.toolkit.log.logback.v1.x.log.GRPCLogClientAppender ERROR in ch.qos.logback.core.joran.spi.Interpreter@51:112 - ActionException in Action for tag [appender] ch.qos.logback.core.joran.spi.ActionException: ch.qos.logback.core.util.DynamicClassLoadingException: Failed to instantiate type org.apache.skywalking.apm.toolkit.log.logback.v1.x.log.GRPCLogClientAppender ERROR in ch.qos.logback.core.joran.action.AppenderRefAction - Could not find an appender named [GRPC]. Did you define it below instead of above in the configuration file? ERROR in ch.qos.logback.core.joran.action.AppenderRefAction - See http://logback.qos.ch/codes.html#appender_order for more details. at org.springframework.boot.logging.logback.LogbackLoggingSystem.reportConfigurationErrorsIfNecessary(LogbackLoggingSystem.java:189) at org.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration(LogbackLoggingSystem.java:170) at org.springframework.boot.logging.AbstractLoggingSystem.initializeWithConventions(AbstractLoggingSystem.java:80) at org.springframework.boot.logging.AbstractLoggingSystem.initialize(AbstractLoggingSystem.java:60) at org.springframework.boot.logging.logback.LogbackLoggingSystem.initialize(LogbackLoggingSystem.java:132) at org.springframework.boot.context.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:329) ... 18 more Suppressed: java.lang.ClassNotFoundException: org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout at java.net.URLClassLoader.findClass(URLClassLoader.java:382) at java.lang.ClassLoader.loadClass(ClassLoader.java:418) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355) at java.lang.ClassLoader.loadClass(ClassLoader.java:351) at ch.qos.logback.core.util.Loader.loadClass(Loader.java:120) at ch.qos.logback.core.joran.action.NestedComplexPropertyIA.begin(NestedComplexPropertyIA.java:102) at ch.qos.logback.core.joran.spi.Interpreter.callBeginAction(Interpreter.java:269) at ch.qos.logback.core.joran.spi.Interpreter.startElement(Interpreter.java:145) at ch.qos.logback.core.joran.spi.Interpreter.startElement(Interpreter.java:128) at ch.qos.logback.core.joran.spi.EventPlayer.play(EventPlayer.java:50) at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:165) at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:152) at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:110) at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:53) at org.springframework.boot.logging.logback.LogbackLoggingSystem.configureByResourceUrl(LogbackLoggingSystem.java:199) at org.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration(LogbackLoggingSystem.java:165) ... 22 more Suppressed: java.lang.ClassNotFoundException: org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout at java.net.URLClassLoader.findClass(URLClassLoader.java:382) at java.lang.ClassLoader.loadClass(ClassLoader.java:418) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355) at java.lang.ClassLoader.loadClass(ClassLoader.java:351) at ch.qos.logback.core.util.Loader.loadClass(Loader.java:120) at ch.qos.logback.core.joran.action.NestedComplexPropertyIA.begin(NestedComplexPropertyIA.java:102) at ch.qos.logback.core.joran.spi.Interpreter.callBeginAction(Interpreter.java:269) at ch.qos.logback.core.joran.spi.Interpreter.startElement(Interpreter.java:145) at ch.qos.logback.core.joran.spi.Interpreter.startElement(Interpreter.java:128) at ch.qos.logback.core.joran.spi.EventPlayer.play(EventPlayer.java:50) at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:165) at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:152) at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:110) at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:53) at org.springframework.boot.logging.logback.LogbackLoggingSystem.configureByResourceUrl(LogbackLoggingSystem.java:199) at org.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration(LogbackLoggingSystem.java:165) ... 22 more Suppressed: ch.qos.logback.core.util.DynamicClassLoadingException: Failed to instantiate type org.apache.skywalking.apm.toolkit.log.logback.v1.x.log.GRPCLogClientAppender at ch.qos.logback.core.util.OptionHelper.instantiateByClassNameAndParameter(OptionHelper.java:68) at ch.qos.logback.core.util.OptionHelper.instantiateByClassName(OptionHelper.java:44) at ch.qos.logback.core.util.OptionHelper.instantiateByClassName(OptionHelper.java:33) at ch.qos.logback.core.joran.action.AppenderAction.begin(AppenderAction.java:52) at ch.qos.logback.core.joran.spi.Interpreter.callBeginAction(Interpreter.java:269) at ch.qos.logback.core.joran.spi.Interpreter.startElement(Interpreter.java:145) at ch.qos.logback.core.joran.spi.Interpreter.startElement(Interpreter.java:128) at ch.qos.logback.core.joran.spi.EventPlayer.play(EventPlayer.java:50) at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:165) at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:152) at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:110) at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:53) at org.springframework.boot.logging.logback.LogbackLoggingSystem.configureByResourceUrl(LogbackLoggingSystem.java:199) at org.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration(LogbackLoggingSystem.java:165) ... 22 more Caused by: java.lang.ClassNotFoundException: org.apache.skywalking.apm.toolkit.log.logback.v1.x.log.GRPCLogClientAppender at java.net.URLClassLoader.findClass(URLClassLoader.java:382) at java.lang.ClassLoader.loadClass(ClassLoader.java:418) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355) at java.lang.ClassLoader.loadClass(ClassLoader.java:351) at ch.qos.logback.core.util.OptionHelper.instantiateByClassNameAndParameter(OptionHelper.java:55) ... 35 more Suppressed: ch.qos.logback.core.joran.spi.ActionException: ch.qos.logback.core.util.DynamicClassLoadingException: Failed to instantiate type org.apache.skywalking.apm.toolkit.log.logback.v1.x.log.GRPCLogClientAppender at ch.qos.logback.core.joran.action.AppenderAction.begin(AppenderAction.java:76) at ch.qos.logback.core.joran.spi.Interpreter.callBeginAction(Interpreter.java:269) at ch.qos.logback.core.joran.spi.Interpreter.startElement(Interpreter.java:145) at ch.qos.logback.core.joran.spi.Interpreter.startElement(Interpreter.java:128) at ch.qos.logback.core.joran.spi.EventPlayer.play(EventPlayer.java:50) at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:165) at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:152) at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:110) at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:53) at org.springframework.boot.logging.logback.LogbackLoggingSystem.configureByResourceUrl(LogbackLoggingSystem.java:199) at org.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration(LogbackLoggingSystem.java:165) ... 22 more [CIRCULAR REFERENCE:ch.qos.logback.core.util.DynamicClassLoadingException: Failed to instantiate type org.apache.skywalking.apm.toolkit.log.logback.v1.x.log.GRPCLogClientAppender] 已与地址为 ''127.0.0.1:49670',传输: '套接字'' 的目标虚拟机断开连接
09-12
在Spring Boot项目中,Logback日志系统初始化失败,出现`org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout`和`org.apache.skywalking.apm.toolkit.log.logback.v1.x.log.GRPCLogClientAppender`类找不到的错误,通常是由于缺少相关依赖导致的。可以按照以下步骤解决: ### 引入SkyWalking相关依赖 确保项目中引入了SkyWalking的相关依赖,并且版本一致。可以在`pom.xml`中添加以下依赖: ```xml <dependency> <groupId>org.apache.skywalking</groupId> <artifactId>apm-toolkit-trace</artifactId> <version>8.12.0</version> </dependency> <dependency> <groupId>org.apache.skywalking</groupId> <artifactId>apm-toolkit-opentracing</artifactId> <version>8.12.0</version> </dependency> <dependency> <groupId>org.apache.skywalking</groupId> <artifactId>apm-toolkit-logback-1.x</artifactId> <version>8.12.0</version> </dependency> ``` 上述依赖确保了项目中包含了所需的SkyWalking日志工具类,其中`apm-toolkit-logback-1.x`依赖提供了`TraceIdPatternLogbackLayout`和`GRPCLogClientAppender`类[^2]。 ### 检查依赖版本一致性 确保引入的SkyWalking依赖版本与项目中使用的SkyWalking Agent版本一致。版本不一致可能会导致类找不到的问题。 ### 刷新Maven依赖 在引入依赖后,需要刷新Maven项目,确保依赖被正确下载。可以在IDE中使用Maven的刷新功能,或者在命令行中执行以下命令: ```bash mvn clean install ``` ### 检查日志配置文件 确保`logback.xml`文件中正确配置了日志输出设置,示例如下: ```xml <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder"> <layout class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout"> <pattern>${log.pattern}</pattern> </layout> </encoder> ``` 上述配置确保了日志输出时能够正确使用`TraceIdPatternLogbackLayout`类[^1]。 ### 检查IDE缓存 有时候IDE的缓存可能会导致类找不到的问题,可以尝试清理IDE的缓存并重新构建项目。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值