JSON使用nodeJS中的Winston登录Google Cloud Logging

时间:2016-05-14 11:27:19

标签: syslog winston google-cloud-logging

我正在Google Compute Engine的Ubuntu 14.04上运行nodeJS服务器。我想为我的应用程序使用谷歌云日志记录,因此我根据https://cloud.google.com/logging/docs/agent/installation安装了谷歌流利的日志记录代理

我使用winston和winston-syslog来编写日志。这是代码。

var winston = require('winston');
var winstonsyslog = require('winston-syslog').Syslog;

var options = {
    json : true
};

winston.add(winston.transports.Syslog, options);

当我使用

编写日志时
winston.log('info', "27", { anything: 'This is metadata' });

我正在

{
  metadata: {…}    
  textPayload: "May 14 10:47:44 localhost node[7633]: {"anything":"This is metadata","level":"info","message":"27"}"    
  insertId: "..."    
  log: "syslog.local0.info"    
}

如何获取structPayload而不是textPayload,它将日志显示为JSON而不是String。

1 个答案:

答案 0 :(得分:0)

日志代理拥有自己的配置文件,其中大多数都有format none(请参阅https://github.com/GoogleCloudPlatform/fluentd-catch-all-config)。因此,所有日志行都转到textPayload

解决方案是编写自己的流利配置文件并使用fluent-plugin-google-cloud作为输出。 fluent-plugin-google-cloud应作为gem 直接安装,而不是使用Logging Agent只要您的条目是有效的JSON,您就会在Stackdriver上看到structPayload设置的条目正常。键/值过滤器也可以使用。

我从未使用过winston,但这里是示例配置:

<source>
    @type tail
    format apache
    path /var/log/apache/access.log
    tag apache.access
</source>

<match **>
    @type google_cloud
    buffer_chunk_limit 10k
</match>

请注意buffer_chunk_limit 10k,将其设置为太大的值或将其保留为默认值可能会导致Google::Apis::ClientError badRequest: Request payload size exceeds the limit