-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
70 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |