From 3e8d8477ebe78be57e2f61d4fa90fc2bf29911c2 Mon Sep 17 00:00:00 2001 From: Nik Graf Date: Fri, 11 Aug 2017 07:33:42 +0200 Subject: [PATCH] improve log output of emit --- lib/plugins/emit/index.js | 24 ++++++++++++++++++------ lib/plugins/run/utils/logEventGateway.js | 12 +++++++++--- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/lib/plugins/emit/index.js b/lib/plugins/emit/index.js index da2076c4b..086bd77f1 100644 --- a/lib/plugins/emit/index.js +++ b/lib/plugins/emit/index.js @@ -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}`); }); } diff --git a/lib/plugins/run/utils/logEventGateway.js b/lib/plugins/run/utils/logEventGateway.js index 55506af30..77fdc7952 100644 --- a/lib/plugins/run/utils/logEventGateway.js +++ b/lib/plugins/run/utils/logEventGateway.js @@ -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`; };