0

如果我在我的代码中使用console.log('message'),它会在Cloudwatch中显示为
2017-03-16T18:58:21.823Z 863c835c-0a7a-11e7-9140-e5018d6e5029 message从Lambda(Node.js)更改Cloudwatch日志的日志格式

是否有任何方法可以删除自动格式设置,以便Cloudwatch只显示我传递给console.log的参数?

+1

什么“自动格式化你指的是每个LAMBDA日志消息被换成了一个时间戳和拉姆达调用ID没有这些信息?日志会是一团糟 –

+0

这是自动格式化,请求ID很容易被减少到更少的字符,可以在时间,请求ID和消息之间添加分隔符,以便更容易解析...你知道吗如何修复它? – maniciam

回答

2

内部处理程序可以覆盖console.log直接写入到stdout

var util = require('util') 

module.exports.handler = function (event, context, done) { 
    console.log = function() { 
    var args = Array.prototype.slice.call(arguments) 
    process.stdout.write(args.map(function (arg) { 
     return util.isPrimitive(arg) ? String(arg) : util.inspect(arg) 
    }).join(' ')) 
    } 

    // the rest of your handler... 
} 
+0

太棒了,这就是我一直在寻找的东西,它看起来像是在异步地发生,但是很难找到一个“齐平”的解决方案上。你知道一种使这个同步的方法吗? – maniciam

+0

@maniciam不幸的没有。一个非完美的解决方案就是用'const finish =(err,data)=> process.stdout.write('done',()=> done(err,data)等东西替换你的处理器的'done'回调函数。 )'然后调用'finish()'而不是直接调用'done()'。 – idbehold