SLF4J 将异常记录为 JSON 或单行字符串

SLF4J Logging Exception as JSON or Single-Line String

我需要能够将异常作为单个记录记录在我的日志中,这将使调查 Kibana/Elasticsearch 中的问题变得更加容易。从我从 slf4j 的文档中可以看出,Logger 接口要求消息为 Strings。我唯一的选择是在将异常消息传递给记录器之前从中删除换行符吗?

对于上下文,我使用以下内容:

这是我的自定义异常处理程序的精简版:

import my.error.Error; // custom Error class
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

@ControllerAdvice
public class CustomExceptionHandler extends ResponseEntityExceptionHandler {

  private final Logger logger = LoggerFactory.getLogger(this.getClass());
  private void logError(Error error, Exception ex){
    logger.error(String.format("id: %s, message: %s", error.getId(), ex.getMessage()), ex);
  }

}

最初我试图通过在 src/main/java/resources 内更改我的 logback.xml 文件来改变日志记录行为。不幸的是,这似乎什么也没做,所以我现在假设我正在创建的 CustomExceptionHandler 正在否决 logback.xml 文件中的规范集。具体来说,<encoder><pattern> 已根据其他研究进行了更改。它正在尝试替换所有换行符。

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" debug="true">
    <include resource="org/springframework/boot/logging/logback/base.xml" />

    <appender name="FILE-ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_PATH}/gateway.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${LOG_PATH}/archived/gateway/gateway.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
            <!-- each archived file, size max 5MB -->
            <maxFileSize>5MB</maxFileSize>
            <!-- total size of all archive files, if total size > 10GB, it will delete old archived file -->
            <totalSizeCap>10GB</totalSizeCap>
            <!-- 30 days to keep -->
            <maxHistory>30</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>%d %p %c{1.} [%t] %m MULTIEXCEPTION %replace(%xException){'\n','\u2028'}%nopex%n</pattern>
        </encoder>
    </appender>

    <root level="INFO">
        <appender-ref ref="FILE-ROLLING" level="DEBUG" additivity="false"/>
    </root>

    <springProfile name="local">
        <logger name="my.gateway" level="TRACE" additivity="false">
            <appender-ref ref="CONSOLE" />
            <appender-ref ref="FILE-ROLLING" />
        </logger>
        <logger name="com.netflix" level="DEBUG" additivity="false">
            <appender-ref ref="CONSOLE" />
        </logger>
        <logger name="org.springframework" level="DEBUG" additivity="false">
            <appender-ref ref="CONSOLE" />
        </logger>
        <logger name="com" level="INFO" additivity="false">
            <appender-ref ref="CONSOLE" />
        </logger>
        <logger name="gov" level="INFO" additivity="false">
            <appender-ref ref="CONSOLE" />
        </logger>
        <logger name="org" level="INFO" additivity="false">
            <appender-ref ref="CONSOLE" />
        </logger>
    </springProfile>
</configuration>

链接

我将 logError 方法更改为:

import org.slf4j.MDC; // new import to add property
// ... other imports from before

  private void logError(Error error, Exception exception){
    MDC.put("error id", error.getId().toString()); // add a new property to the thread context
    LOGGER.error(exception.getMessage(), exception);
    MDC.clear(); // remove all new properties after logging the error
  }

我的新 pom.xml 文件依赖项:

  <dependencies>
    <!-- logging -->
    <dependency>
      <groupId>net.logstash.logback</groupId>
      <artifactId>logstash-logback-encoder</artifactId>
      <version>5.2</version>
    </dependency>

    <dependency>
      <groupId>ch.qos.logback.contrib</groupId>
      <artifactId>logback-jackson</artifactId>
      <version>0.1.5</version>
    </dependency>
    <!-- ch.qos.logback.contrib.json.classic.JsonLayout -->
    <dependency>
      <groupId>ch.qos.logback.contrib</groupId>
      <artifactId>logback-json-classic</artifactId>
      <version>0.1.5</version>
    </dependency>
    <!-- Other dependencies -->
    <!-- ... -->
    <!-- ... -->
  </dependencies>

新建logback.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <appender class="ch.qos.logback.core.rolling.RollingFileAppender" name="FILE-ROLLING">
    <!-- Old encoder is removed: 
    <encoder>
      <pattern>%d %p %c{1.} [%t] %m%n</pattern>
    </encoder>
    -->
    <file>${LOG_PATH}/data.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
      <fileNamePattern>${LOG_PATH}/archived/data/data.%d{yyyy-MM-dd}.%i.log.gz
      </fileNamePattern>
      <!-- each archived file, size max 10MB -->
      <maxFileSize>10MB</maxFileSize>
      <!-- total size of all archive files, if total size > 20GB, it will delete old archived file -->
      <maxHistory>60</maxHistory>
      <!-- 60 days to keep -->
      <totalSizeCap>20GB</totalSizeCap>
    </rollingPolicy>
    <!-- TODO : configure a pretty printer -->
    <encoder class="net.logstash.logback.encoder.LogstashEncoder">
      <provider class="net.logstash.logback.composite.loggingevent.ArgumentsJsonProvider"/>
    </encoder>
  </appender>

  <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
    <!-- TODO : configure a pretty printer -->
    <encoder class="net.logstash.logback.encoder.LogstashEncoder">
      <provider class="net.logstash.logback.composite.loggingevent.ArgumentsJsonProvider"/>
    </encoder>
  </appender>
  <!-- The base logback configuration is removed, but note that this will also remove the default Spring start-up messages, so those need to be added back into the <root> 
  <include resource="org/springframework/boot/logging/logback/base.xml"/>
  -->
  <root level="INFO">
    <appender-ref ref="FILE-ROLLING"/>
    <appender-ref ref="CONSOLE"/> <!-- This adds Spring start-up messages back into the logs -->
  </root>
  <springProfile name="local">
    <logger additivity="false" level="TRACE" name="org.springframework.web">
      <appender-ref ref="CONSOLE"/>
    </logger>
  </springProfile>
  <springProfile name="dev">
    <logger additivity="false" level="DEBUG" name="org.springframework">
      <appender-ref ref="FILE-ROLLING"/>
    </logger>
  </springProfile>
  <springProfile name="prod">
    <logger additivity="false" level="DEBUG" name="my.app.path">
      <appender-ref ref="FILE-ROLLING"/>
    </logger>
    <logger additivity="false" level="DEBUG" name="org.jooq">
      <appender-ref ref="FILE-ROLLING"/>
    </logger>
    <logger additivity="false" level="DEBUG" name="org.springframework">
      <appender-ref ref="FILE-ROLLING"/>
    </logger>
  </springProfile>
</configuration>

SLF4J Docs - Mapped Diagnostic Context (MDC)
Baeldung Example - MDC + Log4J2