结构化的堆叠式日志 - 将MDC添加到日志中



将MDC添加到日志中,以便能够跟踪Stackdriver仪表板和日志记录控制台中的特定错误日志。当前的实现在本地机器上工作正常,但在云上不是 - 只是不包括我的MDC到日志条目。问题是我根本无法弄清楚什么可能是一个问题。

本地日志输出(包含" contextKey":" somevalue"):

{"traceId":"615b35dc7f639027","spanId":"615b35dc7f639027","spanExportable":"false","contextKey":"someValue","timestampSeconds":1552311117,"timestampNanos":665000000,"severity":"ERROR","thread":"reactor-http-nio-3","logger":"com.example.someservice.controller.MyController", ...}

kubernetes容器日志相同的服务(no" contextKey":" somevalue"在此日志条目中:

):
{"traceId":"8d7287fa0ebdacfce9b88097e290ecbf","spanId":"96967afbe05dbf0e","spanExportable":"false","X-B3-ParentSpanId":"224dcb9869488858","parentId":"224dcb9869488858","timestampSeconds":1552312549,"timestampNanos":752000000,"severity":"ERROR","thread":"reactor-http-epoll-2","logger":"com.example.someservice.controller.MyController","message":"Something went wrong","context":"default","logging.googleapis.com/trace":"projects/my-project/traces/8d7287fa0ebdacfce9b88097e290ecbf","logging.googleapis.com/spanId":"96967afbe05dbf0e"}

我的logback.xml:

<configuration>
    <appender name="CONSOLE_JSON" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
            <layout class="org.springframework.cloud.gcp.autoconfigure.logging.StackdriverJsonLayout">
                <projectId>${projectId}</projectId>
            </layout>
        </encoder>
    </appender>
    <springProfile name="local,dev,test">
        <root level="INFO">
            <appender-ref ref="CONSOLE_JSON"/>
        </root>
    </springProfile>
</configuration>

使用定义的MDC触发日志创建的控制器:

@RestController
@RequestMapping("v1/my-way")
@Slf4j
public class MyController {
    @GetMapping
    public void read() {
        MDC.put("contextKey", "someValue");
        log.error("Something went wrong");
        MDC.remove("contextKey")
    }
}

您的MDC字段应在日志实体的"标签"节点上可用。你在那里检查了吗?

同样在日志观看器中的Google控制台上,设置MDC字段非常好,以在日志记录中显示。对于该集," labels.youcustommdcfield:at View选项 ->修改自定义字段(在屏幕的右上方)

update :但是,您使用与我不同的方法来堆叠。我正在使用

<appender name="STACKDRIVER" class="com.google.cloud.logging.logback.LoggingAppender">
    <log>
        ${log_file_name}
    </log>
</appender>

这是一个技巧(我也有春天)。

相关内容

  • 没有找到相关文章

最新更新