我正在尝试使用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 = 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一起使用的服务帐户具有stackdriver写入访问权限,那应该没问题。
我的理解错误吗?任何有其他见解的人...
Edit#2:似乎是Nodejs 10上的Cloud Functions的一个问题。它可以在不指定keyFilename的情况下与Nodejs 8一起很好地工作...非常奇怪...
在错误报告中,我可以在service -> winston-test
下和在堆栈驱动程序中在All logs -> winston_log
下找到我的错误
/**
* 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 metadata = {
resource: {
type: 'global',
serviceContext: {
service: 'winston-test',
version: '1'
},
prefix: 'DataInflow'
}
};
const resource = {
// This example targets the "global" resource for simplicity
type: 'global',
};
const loggingWinston = new LoggingWinston({ resource: resource });
// 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(Error('Testing labels'));
let message = req.query.message || req.body.message || 'Hello World!';
res.status(200).send(message);
};