文本负载中的自定义标签
Custom labels coming in text payload
我正在尝试使用 google-cloud/logging-winston 云功能。
我想使用 prefix & labels 但是当我根据 google-cloud/logging-winston 文档配置它们时没有任何效果。
如图所示。标签被添加到文本有效负载中,并且根本没有使用前缀。
知道出了什么问题以及如何解决这个问题...
/**
* 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 中使用的服务帐户具有 stackdriver 写入权限,那么应该没问题。
我的理解有误吗?任何有其他见解的人...
Edit#2: 似乎是 Nodejs 10 上 Cloud Functions 的一个问题。它在 Nodejs 8 上工作得很好,没有指定 keyFilename...很奇怪...
我终于解决了这个问题。所有 winston 日志都存储在自定义标签下的 stackdriver 中,所以这就是我的问题。
使用以下代码,我能够将错误记录到 stackdriver 以及错误报告中。
在错误报告中,我可以在 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);
};
我正在尝试使用 google-cloud/logging-winston 云功能。
我想使用 prefix & labels 但是当我根据 google-cloud/logging-winston 文档配置它们时没有任何效果。
如图所示。标签被添加到文本有效负载中,并且根本没有使用前缀。
知道出了什么问题以及如何解决这个问题...
/**
* 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 中使用的服务帐户具有 stackdriver 写入权限,那么应该没问题。
我的理解有误吗?任何有其他见解的人...
Edit#2: 似乎是 Nodejs 10 上 Cloud Functions 的一个问题。它在 Nodejs 8 上工作得很好,没有指定 keyFilename...很奇怪...
我终于解决了这个问题。所有 winston 日志都存储在自定义标签下的 stackdriver 中,所以这就是我的问题。
使用以下代码,我能够将错误记录到 stackdriver 以及错误报告中。
在错误报告中,我可以在 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);
};