From efb1f089b049969e49f2e121b7fe1dc0dca63412 Mon Sep 17 00:00:00 2001 From: Gajus Kuizinas Date: Sun, 1 Oct 2017 10:25:37 +0100 Subject: [PATCH] feat: add augment CLI program --- README.md | 4 ++- package.json | 1 + src/bin/commands/augment.js | 66 +++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 src/bin/commands/augment.js diff --git a/README.md b/README.md index 1c5d818..73f6358 100644 --- a/README.md +++ b/README.md @@ -354,9 +354,11 @@ Roarr does not have reserved context property names. However, I encourage use of |Context property name|Use case| |---|---| |`application`|Name of the application (do not use in code intended for distribution; see `package` property instead).| -|`package`|Name of the package.| +|`hostname`|Machine hostname. See `roarr augment --append-hostname` option.| +|`instanceId`|Unique instance ID. Used to distinguish log source in high-concurrency environments. See `roarr augment --append-instance-id` option.| |`logLevel`|Human-readable name of the log-level, e.g. "error". See [API](#api) for build-in loggers with a pre-set log-level.| |`namespace`|Namespace within a package, e.g. function name. Treat the same way that you would construct namespaces when using the [`debug`](https://github.com/visionmedia/debug) package.| +|`package`|Name of the package.| ### Using Roarr in an application diff --git a/package.json b/package.json index c618ae1..5bb64a4 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "prettyjson": "^1.2.1", "split2": "^2.2.0", "sprintf-js": "^1.1.1", + "ulid": "^1.0.0", "yargs": "^9.0.1" }, "description": "JSON logger for Node.js.", diff --git a/src/bin/commands/augment.js b/src/bin/commands/augment.js new file mode 100644 index 0000000..a70aab5 --- /dev/null +++ b/src/bin/commands/augment.js @@ -0,0 +1,66 @@ +// @flow + +import os from 'os'; +import split from 'split2'; +import ulid from 'ulid'; +import { + isRoarrLine +} from './utilities'; + +export const command = 'augment'; +export const desc = 'Augments Roarr logs with additional information.'; + +type LogFormatterConfigurationType = {| + +appendHostname: boolean, + +appendInstanceId: boolean +|}; + +const createLogFormatter = (configuration: LogFormatterConfigurationType) => { + let instanceId; + + if (configuration.appendInstanceId) { + instanceId = ulid(); + } + + const stream = split((line) => { + if (!isRoarrLine(line)) { + return ''; + } + + const message = JSON.parse(line); + + if (configuration.appendHostname) { + message.context.hostname = os.hostname(); + } + + if (configuration.appendInstanceId) { + message.context.instanceId = instanceId; + } + + return JSON.stringify(message) + '\n'; + }); + + return stream; +}; + +// eslint-disable-next-line flowtype/no-weak-types +export const builder = (yargs: Object) => { + return yargs + .options({ + 'append-hostname-id': { + default: false, + type: 'boolean' + }, + 'append-instance-id': { + default: false, + type: 'boolean' + } + }); +}; + +// eslint-disable-next-line flowtype/no-weak-types +export const handler = (argv: Object) => { + process.stdin + .pipe(createLogFormatter(argv)) + .pipe(process.stdout); +};