mirror of
https://github.com/serverless/serverless.git
synced 2026-01-25 15:07:39 +00:00
feat: Simplify logs command output
This commit is contained in:
parent
7abbde611c
commit
38412ce88b
@ -1,12 +1,11 @@
|
||||
'use strict';
|
||||
|
||||
const chalk = require('chalk');
|
||||
const path = require('path');
|
||||
const validate = require('./lib/validate');
|
||||
const stdin = require('get-stdin');
|
||||
const formatLambdaLogEvent = require('./utils/formatLambdaLogEvent');
|
||||
const ServerlessError = require('../../serverless-error');
|
||||
const { legacy, writeText, style } = require('@serverless/utils/log');
|
||||
const { writeText, style } = require('@serverless/utils/log');
|
||||
|
||||
class AwsInvoke {
|
||||
constructor(serverless, options) {
|
||||
@ -102,26 +101,19 @@ class AwsInvoke {
|
||||
}
|
||||
|
||||
log(invocationReply) {
|
||||
const color = !invocationReply.FunctionError ? (x) => x : chalk.red;
|
||||
|
||||
if (invocationReply.Payload) {
|
||||
const response = JSON.parse(invocationReply.Payload);
|
||||
|
||||
legacy.consoleLog(color(JSON.stringify(response, null, 4)));
|
||||
writeText(JSON.stringify(response, null, 4));
|
||||
}
|
||||
|
||||
if (invocationReply.LogResult) {
|
||||
legacy.consoleLog(
|
||||
chalk.gray('--------------------------------------------------------------------')
|
||||
);
|
||||
writeText(
|
||||
style.aside('--------------------------------------------------------------------')
|
||||
);
|
||||
const logResult = Buffer.from(invocationReply.LogResult, 'base64').toString();
|
||||
logResult.split('\n').forEach((line) => {
|
||||
legacy.consoleLog(formatLambdaLogEvent(line));
|
||||
writeText(formatLambdaLogEvent(line, { isModern: true }));
|
||||
writeText(formatLambdaLogEvent(line));
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@ -4,7 +4,7 @@ const _ = require('lodash');
|
||||
const dayjs = require('dayjs');
|
||||
const utc = require('dayjs/plugin/utc');
|
||||
const wait = require('timers-ext/promise/sleep');
|
||||
const { legacy, writeText } = require('@serverless/utils/log');
|
||||
const { writeText } = require('@serverless/utils/log');
|
||||
const validate = require('./lib/validate');
|
||||
const formatLambdaLogEvent = require('./utils/formatLambdaLogEvent');
|
||||
const ServerlessError = require('../../serverless-error');
|
||||
@ -95,8 +95,10 @@ class AwsLogs {
|
||||
const results = await this.provider.request('CloudWatchLogs', 'filterLogEvents', params);
|
||||
if (results.events) {
|
||||
results.events.forEach((e) => {
|
||||
legacy.write(formatLambdaLogEvent(e.message));
|
||||
writeText(formatLambdaLogEvent(e.message, { isModern: true }));
|
||||
if (e.message.includes('SERVERLESS_ENTERPRISE') || e.message.startsWith('END')) {
|
||||
return;
|
||||
}
|
||||
writeText(formatLambdaLogEvent(e.message));
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@ -1,25 +1,28 @@
|
||||
'use strict';
|
||||
|
||||
const dayjs = require('dayjs');
|
||||
const chalk = require('chalk');
|
||||
const os = require('os');
|
||||
const { style } = require('@serverless/utils/log');
|
||||
|
||||
module.exports = (msgParam, options = {}) => {
|
||||
const { isModern } = options;
|
||||
module.exports = (msgParam) => {
|
||||
let msg = msgParam;
|
||||
const dateFormat = 'YYYY-MM-DD HH:mm:ss.SSS (Z)';
|
||||
const dateFormat = 'YYYY-MM-DD HH:mm:ss.SSS';
|
||||
|
||||
if (isModern) {
|
||||
if (!msg.startsWith('REPORT')) msg = msg.trimRight();
|
||||
} else if (msg.startsWith('REPORT')) {
|
||||
msg += os.EOL;
|
||||
if (!msg.startsWith('REPORT')) msg = msg.trimRight();
|
||||
|
||||
if (msg.startsWith('START')) {
|
||||
msg = 'START';
|
||||
return style.aside(msg);
|
||||
}
|
||||
|
||||
if (msg.startsWith('START') || msg.startsWith('END') || msg.startsWith('REPORT')) {
|
||||
return isModern ? style.aside(msg) : chalk.gray(msg);
|
||||
} else if (msg.trim() === 'Process exited before completing request') {
|
||||
return isModern ? style.error(msg) : chalk.red(msg);
|
||||
if (msg.startsWith('REPORT')) {
|
||||
const splitted = msg.split('\t');
|
||||
// Replace REPORT with END and filter out RequestId
|
||||
msg = `END ${splitted.slice(1).join('\t')}`;
|
||||
return style.aside(msg);
|
||||
}
|
||||
|
||||
if (msg.trim() === 'Process exited before completing request') {
|
||||
return style.error(msg);
|
||||
}
|
||||
|
||||
const splitted = msg.split('\t');
|
||||
@ -44,6 +47,5 @@ module.exports = (msgParam, options = {}) => {
|
||||
const text = msg.split(`${reqId}\t`)[1];
|
||||
const time = dayjs(date).format(dateFormat);
|
||||
|
||||
if (isModern) return `${style.aside(`${time}\t${reqId}`)}\t${level}${text}`;
|
||||
return `${chalk.green(time)}\t${chalk.yellow(reqId)}\t${level}${text}`;
|
||||
return `${style.aside(`${time}\t`)}${level}${text}`;
|
||||
};
|
||||
|
||||
@ -2,30 +2,32 @@
|
||||
|
||||
const expect = require('chai').expect;
|
||||
const dayjs = require('dayjs');
|
||||
const chalk = require('chalk');
|
||||
const os = require('os');
|
||||
const { style } = require('@serverless/utils/log');
|
||||
const formatLambdaLogEvent = require('../../../../../../lib/plugins/aws/utils/formatLambdaLogEvent');
|
||||
|
||||
describe('#formatLambdaLogEvent()', () => {
|
||||
it('should format invocation report', () => {
|
||||
const msg =
|
||||
'REPORT\tRequestId: 99c30000-b01a-11e5-93f7-b8e85631a00e\tDuration: 0.40 ms\tBilled Duration: 100 ms\tMemory Size: 512 MB\tMax Memory Used: 30 MB';
|
||||
'REPORT RequestId: 99c30000-b01a-11e5-93f7-b8e85631a00e\tDuration: 0.40 ms\tBilled Duration: 100 ms\tMemory Size: 512 MB\tMax Memory Used: 30 MB';
|
||||
const expectedMsg = style.aside(
|
||||
'END Duration: 0.40 ms\tBilled Duration: 100 ms\tMemory Size: 512 MB\tMax Memory Used: 30 MB'
|
||||
);
|
||||
|
||||
expect(formatLambdaLogEvent(msg)).to.equal(chalk.grey(msg + os.EOL));
|
||||
expect(formatLambdaLogEvent(msg)).to.deep.equal(expectedMsg);
|
||||
});
|
||||
|
||||
it('should format invocation failures', () => {
|
||||
const msg = 'Process exited before completing request';
|
||||
expect(formatLambdaLogEvent(msg)).to.equal(chalk.red(msg));
|
||||
expect(formatLambdaLogEvent(msg)).to.deep.equal(style.error(msg));
|
||||
});
|
||||
|
||||
it('should format lambda console.log lines', () => {
|
||||
const nodeLogLine = '2016-01-01T12:00:00Z\t99c30000-b01a-11e5-93f7-b8e85631a00e\ttest';
|
||||
const nodeLogLine = '2016-01-01T12:00:00Z\t99c30000-b01a-11e5-93f7-b8e85631a00e\tINFO\ttest';
|
||||
|
||||
let expectedLogMessage = '';
|
||||
const date = dayjs('2016-01-01T12:00:00Z').format('YYYY-MM-DD HH:mm:ss.SSS (Z)');
|
||||
expectedLogMessage += `${chalk.green(date)}\t`;
|
||||
expectedLogMessage += `${chalk.yellow('99c30000-b01a-11e5-93f7-b8e85631a00e')}\t`;
|
||||
const date = dayjs('2016-01-01T12:00:00Z').format('YYYY-MM-DD HH:mm:ss.SSS');
|
||||
expectedLogMessage += `${style.aside(date)}\t`;
|
||||
expectedLogMessage += 'INFO\t';
|
||||
expectedLogMessage += 'test';
|
||||
|
||||
expect(formatLambdaLogEvent(nodeLogLine)).to.equal(expectedLogMessage);
|
||||
@ -36,9 +38,8 @@ describe('#formatLambdaLogEvent()', () => {
|
||||
'[INFO]\t2016-01-01T12:00:00Z\t99c30000-b01a-11e5-93f7-b8e85631a00e\ttest';
|
||||
|
||||
let expectedLogMessage = '';
|
||||
const date = dayjs('2016-01-01T12:00:00Z').format('YYYY-MM-DD HH:mm:ss.SSS (Z)');
|
||||
expectedLogMessage += `${chalk.green(date)}\t`;
|
||||
expectedLogMessage += `${chalk.yellow('99c30000-b01a-11e5-93f7-b8e85631a00e')}\t`;
|
||||
const date = dayjs('2016-01-01T12:00:00Z').format('YYYY-MM-DD HH:mm:ss.SSS');
|
||||
expectedLogMessage += `${style.aside(date)}\t`;
|
||||
expectedLogMessage += `${'[INFO]'}\t`;
|
||||
expectedLogMessage += 'test';
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user