AWS Lambda:console.error未覆盖

时间:2017-10-06 11:52:20

标签: javascript node.js amazon-web-services aws-lambda

我想覆盖Lambda函数中的默认控制台语句,但它不起作用

const logClone = console.log;

console.success = function () {
  const args = Array.from(arguments);
  args.unshift('SUCCESS -');
  return logClone.apply(console, args);
};
const errorClone = console.error;
console.error = function () {
  const args = Array.from(arguments);
  args.unshift('ERROR -');
  return errorClone.apply(console, args);
};



exports.handler = (event, context, callback) => {
    console.success("I'm in success");
    console.error("I'm in Error");
    callback(null, {statusCode:200,body:'reached'});  // Echo back the first key value    
};

输出如下:

SUCCESS - I'm in success
I'm in Error

仅打印SUCCESS,但ERROR无效

截图

enter image description here

1 个答案:

答案 0 :(得分:0)

const logClone = console.log;
console.success = function () {
  const args = Array.from(arguments);
  args.unshift('SUCCESS -');
  return logClone.apply(console, args);
};

const errorClone = console.error;
console.failure = function () {
  const args = Array.from(arguments);
  args.unshift('ERROR -');
  return errorClone.apply(console, args);
};

exports.handler = (event, context, callback) => {
    console.success("I'm in success");
    console.failure("I'm in Error");

    console.error('testing')
    callback(null, {statusCode:200,body:'reached'});  // Echo back the first key value    
};

使用上面的代码,您可以在该行中添加'ERROR -''SUCCESS-'。但是在node.js中,console.logconsole.error在日志文件中都是可视的。只是 console.log 输出发送到 stdout console.error 输出发送到 stderr 。在浏览器中,您可以看到它们之间的视觉差异。我相信,Lambda将stdout和stderr重定向到一个你可以在cloudwatch中看到的文件。

但是,在Python中,logger.error(similar to console.error)在行开头的时间戳之前添加了一个额外的[ERROR]文本,这样可以更容易地识别错误。