From 3dedff26af459c2060c5b157477bca94433a76e5 Mon Sep 17 00:00:00 2001 From: Gajus Kuizinas Date: Wed, 24 Oct 2018 10:23:56 +0100 Subject: [PATCH] feat: handle truncated Roarr messages --- package.json | 3 +- src/bin/commands/pretty-print.js | 106 ++++++++++++++++++------------- 2 files changed, 65 insertions(+), 44 deletions(-) diff --git a/package.json b/package.json index 4d03c35..0b85e98 100644 --- a/package.json +++ b/package.json @@ -13,9 +13,10 @@ "roarr": "./dist/bin/index.js" }, "dependencies": { - "node-jq": "^1.3.1", "chalk": "^2.4.1", + "node-jq": "^1.3.1", "prettyjson": "^1.2.1", + "roarr": "^2.11.6", "split2": "^3.0.0", "through2": "^2.0.3", "ulid": "^2.3.0", diff --git a/src/bin/commands/pretty-print.js b/src/bin/commands/pretty-print.js index d5a8a73..4c60ca4 100644 --- a/src/bin/commands/pretty-print.js +++ b/src/bin/commands/pretty-print.js @@ -3,6 +3,9 @@ import split from 'split2'; import chalk from 'chalk'; import prettyjson from 'prettyjson'; +import type { + MessageType +} from 'roarr'; import { isRoarrLine } from './utilities'; @@ -41,62 +44,79 @@ const getLogLevelName = (logLevel: number): string => { return logLevels[logLevel] || 'INFO'; }; -const createLogFormatter = (configuration: LogFormatterConfigurationType) => { - const stream = split((line) => { - if (!isRoarrLine(line)) { - return configuration.excludeOrphans ? '' : line + '\n'; - } +const formatMessage = (configuration: LogFormatterConfigurationType, message: MessageType): string => { + let formattedMessage = ''; - const message = JSON.parse(line); + formattedMessage = '[' + new Date(message.time).toISOString() + ']'; - let formattedMessage = ''; + if (message.context.logLevel && typeof message.context.logLevel === 'number') { + const logLevelName = getLogLevelName(message.context.logLevel); - formattedMessage = '[' + new Date(message.time).toISOString() + ']'; + const logLevelColorName = logLevelColorMap[logLevelName]; - if (message.context.logLevel && typeof message.context.logLevel === 'number') { - const logLevelName = getLogLevelName(message.context.logLevel); + if (!logLevelColorName) { + throw new Error('Unexpected state.'); + } - const logLevelColorName = logLevelColorMap[logLevelName]; + formattedMessage += ' ' + logLevelColorName(logLevelName + ' (' + message.context.logLevel + ')'); + } + + if (message.context.package) { + formattedMessage += ' (@' + message.context.package + ')'; + } + + if (message.context.namespace) { + formattedMessage += ' (#' + message.context.namespace + ')'; + } + + formattedMessage += ': ' + message.message + '\n'; + + if (configuration.includeContext && message.context) { + /* eslint-disable no-unused-vars */ + const { + application: tmp0, + hostname: tmp1, + instanceId: tmp2, + logLevel: tmp3, + namespace: tmp4, + package: tmp5, + package: tmp6, + ...rest + } = message.context; + + /* eslint-enable */ + + if (Object.keys(rest).length) { + // eslint-disable-next-line no-console + formattedMessage += prettyjson.render(rest) + '\n\n'; + } + } - if (!logLevelColorName) { - throw new Error('Unexpected state.'); - } + return formattedMessage; +}; - formattedMessage += ' ' + logLevelColorName(logLevelName + ' (' + message.context.logLevel + ')'); - } +const formatInvalidInputMessage = (error: Error, input: string): string => { + return chalk.red('Cannot parse presumed Roarr log message as JSON [' + error.name + ': ' + error.message + ']') + + '\n' + chalk.gray('---INVALID INPUT START---') + + '\n' + input + + '\n' + chalk.gray('---INVALID INPUT END---'); +}; - if (message.context.package) { - formattedMessage += ' (@' + message.context.package + ')'; +const createLogFormatter = (configuration: LogFormatterConfigurationType) => { + const stream = split((line) => { + if (!isRoarrLine(line)) { + return configuration.excludeOrphans ? '' : line + '\n'; } - if (message.context.namespace) { - formattedMessage += ' (#' + message.context.namespace + ')'; - } + let parsedMessage; - formattedMessage += ': ' + message.message + '\n'; - - if (configuration.includeContext && message.context) { - /* eslint-disable no-unused-vars */ - const { - application: tmp0, - hostname: tmp1, - instanceId: tmp2, - logLevel: tmp3, - namespace: tmp4, - package: tmp5, - package: tmp6, - ...rest - } = message.context; - - /* eslint-enable */ - - if (Object.keys(rest).length) { - // eslint-disable-next-line no-console - formattedMessage += prettyjson.render(rest) + '\n\n'; - } + try { + parsedMessage = JSON.parse(line); + } catch (error) { + return formatInvalidInputMessage(error, line); } - return formattedMessage; + return formatMessage(parsedMessage); }); return stream;