Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI: Modern logs adjustements for logs and info commands #10037

Merged
merged 5 commits into from
Oct 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 1 addition & 4 deletions lib/cli/handle-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,7 @@ module.exports = async (exception, options = {}) => {
const errorMsg = stripAnsi(
exceptionTokens.stack && !isUserError ? exceptionTokens.stack : exceptionTokens.message
);
writeText(style.error('\nError:'));
if (errorMsg) {
writeText(` ${errorMsg.split('\n').join('\n ')}`);
}
writeText(null, style.error('Error:'), errorMsg);

legacy.consoleLog(chalk.yellow(' Get Support --------------------------------------------'));
legacy.consoleLog(`${chalk.yellow(' Docs: ')}docs.serverless.com`);
Expand Down
1 change: 0 additions & 1 deletion lib/cli/write-service-outputs.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const { writeText, style } = require('@serverless/utils/log');

module.exports = (serviceOutputs) => {
writeText();
for (const [section, entries] of serviceOutputs) {
if (typeof entries === 'string') {
writeText(`${style.aside(`${section}:`)} ${entries}`);
Expand Down
5 changes: 3 additions & 2 deletions lib/plugins/aws/deploy/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const validateTemplate = require('./lib/validateTemplate');
const updateStack = require('../lib/updateStack');
const existsDeploymentBucket = require('./lib/existsDeploymentBucket');
const path = require('path');
const { style, log, progress } = require('@serverless/utils/log');
const { style, log, progress, writeText } = require('@serverless/utils/log');
const memoize = require('memoizee');

const mainProgress = progress.get('main');
Expand Down Expand Up @@ -189,7 +189,8 @@ class AwsDeploy {
)}s)`
)}`
);
if (this.serverless.serviceOutputs) writeServiceOutputs(this.serverless.serviceOutputs);
writeText();
writeServiceOutputs(this.serverless.serviceOutputs);
},
};
}
Expand Down
11 changes: 9 additions & 2 deletions lib/plugins/aws/info/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const BbPromise = require('bluebird');
const { progress } = require('@serverless/utils/log');
const { progress, writeText, style } = require('@serverless/utils/log');
const writeServiceOutputs = require('../../../cli/write-service-outputs');
const validate = require('../lib/validate');
const getStackInfo = require('./getStackInfo');
Expand Down Expand Up @@ -75,7 +75,14 @@ class AwsInfo {
'finalize': () => {
if (this.serverless.processedInput.commands.join(' ') !== 'info') return;

if (this.serverless.serviceOutputs) writeServiceOutputs(this.serverless.serviceOutputs);
writeText(
null,
`${style.aside('service:')} ${this.serverless.service.service}`,
`${style.aside('stage:')} ${this.provider.getStage()}`,
`${style.aside('region:')} ${this.provider.getRegion()}`,
`${style.aside('stack:')} ${this.provider.naming.getStackName()}`
);
writeServiceOutputs(this.serverless.serviceOutputs);
},
};
}
Expand Down
8 changes: 3 additions & 5 deletions lib/plugins/aws/logs.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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}`;
};