Pat*_*ite 3 node.js winston typescript google-cloud-platform google-cloud-run
我正在尝试以 Google Cloud 正确提取日志级别的方式格式化我的日志。这是在 Cloud Run 上运行的,带有打字稿。Cloud Run 正在从容器输出中获取日志。
如果我执行以下操作,google 会正确解析日志行:
console.log(JSON.stringify({
severity: 'ERROR',
message: 'This is testing a structured log error for GCP'
}));
Run Code Online (Sandbox Code Playgroud)
我尝试了多种不同的方式来使用 winston 进行格式化,结果如下:
useFormat = format.combine(
format((info, opts) => {
info['severity'] = info.level;
delete info.level;
return info;
})(),
format.json());
this.winston = winston.createLogger({
level: logLevel,
format: useFormat,
transports: [new winston.transports.Console()]
});
Run Code Online (Sandbox Code Playgroud)
看起来它会工作(它正确输出 json 行),我在 GCP 日志中得到了这个:

任何帮助表示赞赏。
结果我很接近,只需要 .upperCase() 日志级别(我正在映射 Verbose -> Debug,我真的不明白为什么 GCP 决定做一个与其他人完全不同的日志级别系统)。新代码:
useFormat =
format.combine(
format((info, opts) => {
let level = info.level.toUpperCase();
if(level === 'VERBOSE') {
level = 'DEBUG';
}
info['severity'] = level;
delete info.level;
return info;
})(),
format.json());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
869 次 |
| 最近记录: |