Skip to content

Commit

Permalink
refactor(CLI): Improve modern logs for logs command
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Oct 1, 2021
1 parent f73062f commit d1701bf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
8 changes: 3 additions & 5 deletions lib/plugins/aws/logs.js
Expand Up @@ -4,8 +4,7 @@ const _ = require('lodash');
const dayjs = require('dayjs');
const utc = require('dayjs/plugin/utc');
const wait = require('timers-ext/promise/sleep');
const stripAnsi = require('strip-ansi');
const { legacy, writeText, style } = require('@serverless/utils/log');
const { legacy, writeText } = require('@serverless/utils/log');
const validate = require('./lib/validate');
const formatLambdaLogEvent = require('./utils/formatLambdaLogEvent');
const ServerlessError = require('../../serverless-error');
Expand Down Expand Up @@ -96,9 +95,8 @@ class AwsLogs {
const results = await this.provider.request('CloudWatchLogs', 'filterLogEvents', params);
if (results.events) {
results.events.forEach((e) => {
const text = formatLambdaLogEvent(e.message);
legacy.write(text);
writeText(style.aside(stripAnsi(text).trimRight()));
legacy.write(formatLambdaLogEvent(e.message));
writeText(formatLambdaLogEvent(e.message, { isModern: true }));
});
}

Expand Down
17 changes: 11 additions & 6 deletions lib/plugins/aws/utils/formatLambdaLogEvent.js
Expand Up @@ -3,19 +3,23 @@
const dayjs = require('dayjs');
const chalk = require('chalk');
const os = require('os');
const { style } = require('@serverless/utils/log');

module.exports = (msgParam) => {
module.exports = (msgParam, options = {}) => {
const { isModern } = options;
let msg = msgParam;
const dateFormat = 'YYYY-MM-DD HH:mm:ss.SSS (Z)';

if (msg.startsWith('REPORT')) {
if (isModern) {
if (!msg.startsWith('REPORT')) msg = msg.trimRight();
} else if (msg.startsWith('REPORT')) {
msg += os.EOL;
}

if (msg.startsWith('START') || msg.startsWith('END') || msg.startsWith('REPORT')) {
return chalk.gray(msg);
return isModern ? style.aside(msg) : chalk.gray(msg);
} else if (msg.trim() === 'Process exited before completing request') {
return chalk.red(msg);
return isModern ? style.error(msg) : chalk.red(msg);
}

const splitted = msg.split('\t');
Expand All @@ -38,7 +42,8 @@ module.exports = (msgParam) => {
return msg;
}
const text = msg.split(`${reqId}\t`)[1];
const time = chalk.green(dayjs(date).format(dateFormat));
const time = dayjs(date).format(dateFormat);

return `${time}\t${chalk.yellow(reqId)}\t${level}${text}`;
if (isModern) return `${style.aside(`${time}\t${reqId}`)}\t${level}${text}`;
return `${chalk.green(time)}\t${chalk.yellow(reqId)}\t${level}${text}`;
};

0 comments on commit d1701bf

Please sign in to comment.