serverless/lib/plugins/aws/utils/formatLambdaLogEvent.js
Dan Root add4951a4b Better handling for AWS Lambda logging lines.
Refactor duplicated log line formatting from invoke and logs plugins
into utils/formatLambdaLogEvent.js and add tests.

Make log line formatter understand default python logging module output
from Lambda functions.

Add payload output testing to invoke plugin.

Fixes #1796 and #3594
2017-05-19 12:42:02 -07:00

46 lines
1.1 KiB
JavaScript

'use strict';
const moment = require('moment');
const chalk = require('chalk');
const os = require('os');
module.exports = (msgParam) => {
let msg = msgParam;
const dateFormat = 'YYYY-MM-DD HH:mm:ss.SSS (Z)';
if (msg.startsWith('REPORT')) {
msg += os.EOL;
}
if (msg.startsWith('START') || msg.startsWith('END') || msg.startsWith('REPORT')) {
return chalk.gray(msg);
} else if (msg.trim() === 'Process exited before completing request') {
return chalk.red(msg);
}
const splitted = msg.split('\t');
if (splitted.length < 3) {
return msg;
}
let date = '';
let reqId = '';
let level = '';
if (!isNaN((new Date(splitted[0])).getTime())) {
date = splitted[0];
reqId = splitted[1];
level = '';
} else if (!isNaN((new Date(splitted[1])).getTime())) {
date = splitted[1];
reqId = splitted[2];
level = `${chalk.white(splitted[0])}\t`;
} else {
return msg;
}
const text = msg.split(`${reqId}\t`)[1];
const time = chalk.green(moment(date).format(dateFormat));
return `${time}\t${chalk.yellow(reqId)}\t${level}${text}`;
};