如何在使用logstash编码器记录异常时包含mdc密钥

c3frrgcw  于 2021-07-26  发布在  Java
关注(0)|答案(0)|浏览(338)

我有 logback-spring.xml json控制台追加器所在的文件:
包括mdc密钥 requestId :
但是,当记录运行时异常时,此requestid将丢失。当出现异常时,有没有办法在日志中打印requestid?

<configuration>

    <appender name="jsonConsoleAppender" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="net.logstash.logback.encoder.LogstashEncoder">
            <fieldNames>
                <timestamp>log_datetime</timestamp>
                <thread>thread</thread>
                <logger>[ignore]</logger>
                <version>[ignore]</version>
                <levelValue>[ignore]</levelValue>
                <callerClass>class_name</callerClass>
                <callerMethod>method_name</callerMethod>
                <callerFile>[ignore]</callerFile>
                <callerLine>line_num</callerLine>
            </fieldNames>
            <includeMdcKeyName>requestId</includeMdcKeyName>
            <timestampPattern>yyyy-MM-dd HH:mm:ss.SSS</timestampPattern>
            <includeCallerData>true</includeCallerData>
            <throwableConverter class="net.logstash.logback.stacktrace.ShortenedThrowableConverter">
                <maxDepthPerThrowable>30</maxDepthPerThrowable>
                <maxLength>2048</maxLength>
                <shortenedClassNameLength>20</shortenedClassNameLength>
                <exclude>sun\.reflect\..*\.invoke.*</exclude>
                <exclude>net\.sf\.cglib\.proxy\.MethodProxy\.invoke</exclude>
                <rootCauseFirst>true</rootCauseFirst>
                <inlineHash>true</inlineHash>
            </throwableConverter>
        </encoder>
    </appender>

    <springProfile name="!local">
        <root level="INFO">
            <appender-ref ref="jsonConsoleAppender"/>
        </root>
    </springProfile>

</configuration>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题