繁体   English   中英

AWS Lambda:console.error不被覆盖

[英]AWS Lambda : console.error not overriding

我想覆盖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不起作用

屏幕截图

在此处输入图片说明

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.error都是相同的。 它只是将console.log输出发送到stdout,console.error输出发送到stderr 在浏览器中,您可以看到它们之间的视觉差异。 我相信,Lambda会将stdout和stderr都重定向到可以在cloudwatch中看到的文件。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM