简体   繁体   English

Spring启动stackdriver日志记录是textPayload而不是jsonPayload

[英]Spring boot stackdriver logging is textPayload and not jsonPayload

I have a log filter that logs out essential request information for debugging and log analytics. 我有一个日志过滤器,用于记录调试和日志分析的基本请求信息。 But as you can see, the text payload is really hard to read. 但正如您所看到的,文本有效负载实际上很难阅读。

I don't want to have to copy + paste this text payload into a text editor every single time. 我不想每次都将这个文本有效负载复制+粘贴到文本编辑器中。 Is there a way to make stack driver print this in a collapsable json instead? 有没有办法让堆栈驱动程序在可折叠的json中打印出来?

堆栈驱动程序中的文本负载的图像

More info: - GKE pod 更多信息: - GKE pod

@Component
class LogFilter : WebFilter {

    private val logger = LoggerFactory.getLogger(LogFilter::class.java)

    override fun filter(exchange: ServerWebExchange, chain: WebFilterChain): Mono<Void> {
        return chain
                .filter(exchange)
                .doAfterTerminate {
                    val request = exchange.request
                    val path = request.uri.path
                    val routesToExclude = listOf("actuator")
                    var isExcludedRoute = false

                    for (r in routesToExclude) { if (path.contains(r)) { isExcludedRoute = true; break; } }

                    if (!isExcludedRoute) {
                        val startTime = System.currentTimeMillis()
                        val statusCode = exchange.response.statusCode?.value()
                        val requestTime = System.currentTimeMillis() - startTime
                        val msg = "Served $path as $statusCode in $requestTime msec"
                        val requestPrintMap = mutableMapOf<Any, Any>()
                        requestPrintMap["method"] = if (request.method != null) {
                            request.method.toString()
                        } else "UNKNOWN"
                        requestPrintMap["path"] = path.toString()
                        requestPrintMap["query_params"] = request.queryParams
                        requestPrintMap["headers"] = request.headers

                        requestPrintMap["status_code"] = statusCode.toString()
                        requestPrintMap["request_time"] = requestTime
                        requestPrintMap["msg"] = msg

                        logger.info(JSONObject(requestPrintMap).toString())
                    }
                }
    }

}

What you will need to do is customize Fluentd in GKE . 您需要做的是在GKE中自定义Fluentd Pretty much it's creating a Fluend daemonset for logging instead of the default logging method. 几乎它正在创建一个用于记录的Fluend守护进程而不是默认的记录方法。

Once that is done, you can setup structured logging to send jsonPayload logs to Stackdriver Logging. 完成后,您可以设置结构化日志记录,以将jsonPayload日志发送到Stackdriver Logging。

The default Stackdriver logging agent configuration for Kubernetes will detect single-line JSON and convert it to jsonPayload . Kubernetes的默认Stackdriver日志代理配置将检测单行JSON并将其转换为jsonPayload You can configure Spring to log as single-line JSON (eg, via JsonLayout 1 ) and let the logging agent pick up the JSON object (see https://cloud.google.com/logging/docs/agent/configuration#process-payload ). 您可以将Spring配置为单行JSON(例如,通过JsonLayout 1 )并让日志代理获取JSON对象(请参阅https://cloud.google.com/logging/docs/agent/configuration#process-有效载荷 )。

1 Some of the JSON field names are different (eg, JsonLayout uses "level" for the log level, while the Stackdriver logging agent recognizes "severity"), so you may have to override addCustomDataToJsonMap to fully control the resulting log entries. 1某些JSON字段名称不同(例如, JsonLayout对日志级别使用“level”,而Stackdriver日志记录代理程序识别 “severity”),因此您可能必须覆盖addCustomDataToJsonMap以完全控制生成的日志条目。

See also GKE & Stackdriver: Java logback logging format? 另请参阅GKE和Stackdriver:Java logback日志记录格式?

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

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