简体   繁体   English

结构化 Stackdriver 日志 - 将 MDC 添加到日志

[英]Structured Stackdriver logs - adding MDC to logs

Added MDC to logs to be able track specific error logs in Stackdriver Dashboard and Logging Console.向日志添加了 MDC,以便能够在 Stackdriver Dashboard 和 Logging Console 中跟踪特定的错误日志。 Current implementation is working fine on local machine but on cloud it is not - just don't include my MDC to log entry.当前的实现在本地机器上运行良好,但在云上却不是 - 只是不要将我的 MDC 包含到日志条目中。 The problem is that I cannot figure out what might be a problem at all.问题是我根本无法弄清楚可能是什么问题。

Local logs output(contains "contextKey": "someValue"):本地日志输出(包含“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 container log of the same service(no "contextKey": "someValue" in this log entry):同一服务的 Kubernetes 容器日志(此日志条目中没有 "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"}

My logback.xml:我的 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>

Controller which trigger log creation with defined MDC:使用定义的 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")
    }

}

Your MDC fields should be available at "labels" node of log entity.您的 MDC 字段应该在日志实体的“标签”节点上可用。 Did you check there?你去那里检查了吗?

Also at Google Console in Logs Viewer it's nice to set MDC fields to be shown in logs records.同样在日志查看器中的 Google 控制台中,设置 MDC 字段以显示在日志记录中也很不错。 For that set "labels.YouCustomMDCField: at View Options -> Modify custom fields (at right top of the screen)对于该设置“labels.YouCustomMDCField:在查看选项 -> 修改自定义字段(在屏幕右上角)

Update : However you're using different approach than me for log to Stackdriver.更新:但是,您使用与我不同的方法来登录 Stackdriver。 Im using我正在使用

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

and it's do the trick (me also has Spring).它可以解决问题(我也有 Spring)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM