App Engine Node.js:如何链接应用程序日志和请求日志 [英] App Engine Node.js: how to link app logs and requests logs

查看:80
本文介绍了App Engine Node.js:如何链接应用程序日志和请求日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在App Engine Standard和Flexible上使用Node.js.

I am using Node.js on App Engine Standard and Flexible.

在日志查看器中,是否可以显示嵌套在请求日志中的应用程序日志?

In the logs viewer, is it possible to display application logs nested inside request logs?

推荐答案

是的,可以关联应用程序日志和请求日志.这是日志查看器"中的最终结果:

Yes it is possible to correlate application logs and request logs. This is the end result in the Logs Viewer:

要实现这一目标,您可以:

To achieve this you can either:

同时使用 @google-cloud/trace-agent @google-cloud/logging-bunyan 模块.这样做时,您的日志将自动使用正确的跟踪ID进行注释(请参见

Use both the @google-cloud/trace-agent and @google-cloud/logging-bunyan modules in your application. When you do so, your logs are automatically annotated with the correct Trace ID (see docs for Bunyan).

从请求标头中提取跟踪ID 如果不想使用跟踪"模块,则可以从请求标头中提取跟踪ID,使用以下代码提取traceId:

Extract the trace ID from the request header If you do not want to use the Trace module, you can extract the trace ID from the request header, use the following code to extract the traceId:

const traceHeader = req && req.headers ? req.headers['x-cloud-trace-context'] || '' : '';
const traceId = traceHeader ? traceHeader.split('/')[0] : '';

然后,您需要填充日志条目的trace属性.使用Bunyan记录器时,请使用以下代码:

Then, you need to populate the trace attribute of your log entries. When using the Bunyan logger, use the following code:

logger.info({
  'logging.googleapis.com/trace': `projects/${project}/traces/${traceId}`
}, 'your message');

这篇关于App Engine Node.js:如何链接应用程序日志和请求日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆