mirror of
https://github.com/serverless/serverless.git
synced 2026-01-25 15:07:39 +00:00
improve log output of emit
This commit is contained in:
parent
65ce09bad5
commit
3e8d8477eb
@ -5,6 +5,15 @@ const fdk = require('@serverless/fdk');
|
||||
const path = require('path');
|
||||
const stdin = require('get-stdin');
|
||||
const userStats = require('../../utils/userStats');
|
||||
const chalk = require('chalk');
|
||||
|
||||
const prettifyValue = value => {
|
||||
const prettified = JSON.stringify(value, null, 2).replace(
|
||||
new RegExp('\\n', 'g'),
|
||||
`\n ${chalk.yellow('|')} `
|
||||
);
|
||||
return ` ${chalk.yellow('|')} ${prettified}`;
|
||||
};
|
||||
|
||||
class Emit {
|
||||
constructor(serverless, options) {
|
||||
@ -114,17 +123,20 @@ class Emit {
|
||||
if (this.options.datatype) {
|
||||
emitParams.dataType = this.options.datatype;
|
||||
}
|
||||
const prefix = chalk.yellow(' Serverless | ');
|
||||
return eventGateway
|
||||
.emit(emitParams)
|
||||
.then(() => {
|
||||
const msg = `Successfully emitted the event ${name} as datatype ${emitParams.dataType ||
|
||||
'application/json'} with:`;
|
||||
this.serverless.cli.consoleLog(`${msg}\n${JSON.stringify(data)}`);
|
||||
const msg = `${prefix}Emitted the event ${name} as datatype ${emitParams.dataType ||
|
||||
'application/json'}:`;
|
||||
this.serverless.cli.consoleLog(`${msg}`);
|
||||
this.serverless.cli.consoleLog(prettifyValue(data));
|
||||
})
|
||||
.catch(() => {
|
||||
const msg = `Failed to emit the event ${name} as datatype ${emitParams.dataType ||
|
||||
'application/json'} with:`;
|
||||
this.serverless.cli.consoleLog(`${msg}\n${JSON.stringify(data)}`);
|
||||
const msg = `${prefix}Failed to emit the event ${name} as datatype ${emitParams.dataType ||
|
||||
'application/json'}:`;
|
||||
this.serverless.cli.consoleLog(`${msg}`);
|
||||
this.serverless.cli.consoleLog(prettifyValue(data));
|
||||
throw new Error(`Failed to emit the event ${name}`);
|
||||
});
|
||||
}
|
||||
|
||||
@ -6,11 +6,13 @@ const caret = colorText('|');
|
||||
|
||||
const ignoredMessage = ['Running in development mode with embedded etcd.'];
|
||||
|
||||
const prettifyValue = value =>
|
||||
JSON.stringify(value, null, 2).replace(
|
||||
const prettifyValue = value => {
|
||||
const prettified = JSON.stringify(value, null, 2).replace(
|
||||
new RegExp('\\n', 'g'),
|
||||
`\n ${caret} `
|
||||
);
|
||||
return ` ${caret} ${prettified}`;
|
||||
};
|
||||
|
||||
const transformMessages = parsedMsg => {
|
||||
if (parsedMsg.msg === 'Function registered.') {
|
||||
@ -22,11 +24,15 @@ const transformMessages = parsedMsg => {
|
||||
const text = `Received event ${event.event}\n`;
|
||||
const dataTypeText = ` ${caret} dataType: ${event.dataType}\n`;
|
||||
const ppData = prettifyValue(event.data);
|
||||
const dataText = ` ${caret} data:\n ${caret} ${ppData}`;
|
||||
const dataText = ` ${caret} data:\n${ppData}`;
|
||||
return `${text}${dataTypeText}${dataText}\n`;
|
||||
} else if (parsedMsg.msg === 'Function invoked.') {
|
||||
const text = `Invoked function ${parsedMsg.functionId}\n`;
|
||||
return `${text}`;
|
||||
} else if (parsedMsg.msg === 'Function invocation failed.') {
|
||||
const text = `Failed to invoke function ${parsedMsg.functionId}\n`;
|
||||
const errorText = ` ${caret} error: ${parsedMsg.error}\n`;
|
||||
return `${text}${errorText}`;
|
||||
}
|
||||
return `${parsedMsg.msg.trim()}\n`;
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user