Skip to content

Commit

Permalink
feat: add augment CLI program
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Oct 1, 2017
1 parent d79e70a commit efb1f08
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
66 changes: 66 additions & 0 deletions src/bin/commands/augment.js
Original file line number Diff line number Diff line change
@@ -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);
};

0 comments on commit efb1f08

Please sign in to comment.