【发布时间】:2020-04-11 22:40:30
【问题描述】:
我正在尝试使用 google-cloud/logging-winston 云功能。
我想使用 prefix 和 labels,但是当我根据 google-cloud/logging-winston 文档配置它们时,没有任何效果。
如图所示。标签被添加到 textpayload 并且根本不使用前缀。
知道什么是错的以及如何解决这个问题......
/**
* Responds to any HTTP request.
*
* @param {!express:Request} req HTTP request context.
* @param {!express:Response} res HTTP response context.
*/
exports.helloWorld = (req, res) => {
const winston = require('winston');
// Imports the Google Cloud client library for Winston
const {LoggingWinston} = require('@google-cloud/logging-winston');
const loggingWinston = new LoggingWinston({
serviceContext: {
service: 'winston-test',
version: '1'
},
prefix: 'DataInflow'
});
// Create a Winston logger that streams to Stackdriver Logging
// Logs will be written to: "projects/YOUR_PROJECT_ID/logs/winston_log"
const logger = winston.createLogger({
level: 'info',
transports: [
new winston.transports.Console(),
// Add Stackdriver Logging
loggingWinston,
],
});
// Writes some log entries
//logger.debug('Testing labels', { labels: { module: 'Test Winston Logging' }});
logger.error('Testing labels', { custom_metadata: 'yes', labels: { module: 'Test Winston Logging' }});
let message = req.query.message || req.body.message || 'Hello World!';
res.status(200).send(message);
};
Edit#1:如果我指定 keyFilename=path-to-sa-json-file 那么它可以工作。
const loggingWinston = new LoggingWinston({
serviceContext: {
service: 'winston-test',
version: '1'
},
prefix: 'DataInflow',
keyFilename=path-to-sa-json-file.json
});
但这很奇怪。 Cloud Function 和 logging-winston 库应使用应用程序默认凭据 (ADC)。
因此,如果我与 CF 一起使用的服务帐户具有堆栈驱动程序写入权限,那么应该没问题。
我的理解错了吗?任何有其他见解的人...
Edit#2: 似乎是 Nodejs 10 上的 Cloud Functions 的一个问题。它在 Nodejs 8 上运行良好,无需指定 keyFilename...很奇怪...
【问题讨论】:
-
可能是因为 Nodejs10 运行时,因为它在 beta。但首先我想知道你是如何使用“前缀”参数的?我问是因为在LoggingWinston 库中没有这样的参数。
-
@nibrass-haider 查看最新文档googleapis.dev/nodejs/logging-winston/latest/index.html
-
查看这个官方文档,Finding credentials automatically。您是否遵循了这些步骤?如果是,请告诉我。
-
@nibrass-haider,我想我找到了问题所在。我需要在标签 winston-logs 下而不是在默认标签下找到日志...此外,如果您抛出错误,它们也应该显示在错误报告中...
-
你解决问题了吗?如果是,您可以为其他社区成员发布答案。如果您能解决问题,请告诉我。
标签: google-cloud-platform winston google-cloud-logging