Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dropping args and replacing by minimist #350

Merged
merged 2 commits into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 39 additions & 48 deletions bin.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#!/usr/bin/env node

const fs = require('fs')
const args = require('args')
const path = require('path')
const help = require('help-me')({
dir: path.join(__dirname, 'help'),
ext: '.txt'
})
const pump = require('pump')
const sjp = require('secure-json-parse')
const JoyCon = require('joycon')
Expand All @@ -11,6 +14,7 @@ const stripJsonComments = require('strip-json-comments')
const build = require('./')
const CONSTANTS = require('./lib/constants')
const { isObject } = require('./lib/utils')
const minimist = require('minimist')

const parseJSON = input => {
return sjp.parse(stripJsonComments(input), { protoAction: 'remove' })
Expand All @@ -27,57 +31,38 @@ const joycon = new JoyCon({
stopDir: path.dirname(process.cwd())
})

args
.option(['c', 'colorize'], 'Force adding color sequences to the output')
.option(['f', 'crlf'], 'Append CRLF instead of LF to formatted lines')
.option(['e', 'errorProps'], 'Comma separated list of properties on error objects to show (`*` for all properties) (defaults to ``)')
.option(['l', 'levelFirst'], 'Display the log level as the first output field')
.option(['L', 'minimumLevel'], 'Hide messages below the specified log level')
.option(['x', 'customLevels'], 'Override default levels (`-x err:99,info:1`)')
.option(['X', 'customColors'], 'Override default colors using names from https://www.npmjs.com/package/colorette (`-X err:red,info:blue`)')
.option(['U', 'useOnlyCustomProps'], 'Only use custom levels and colors (if provided); don\'t fallback to default levels and colors (-U false)')
.option(['k', 'errorLikeObjectKeys'], 'Define which keys contain error objects (`-k err,error`) (defaults to `err,error`)')
.option(['m', 'messageKey'], 'Highlight the message under the specified key', CONSTANTS.MESSAGE_KEY)
.option('levelKey', 'Detect the log level under the specified key', CONSTANTS.LEVEL_KEY)
.option(['b', 'levelLabel'], 'Output the log level using the specified label', CONSTANTS.LEVEL_LABEL)
.option(['o', 'messageFormat'], 'Format output of message')
.option(['a', 'timestampKey'], 'Display the timestamp from the specified key', CONSTANTS.TIMESTAMP_KEY)
.option(['t', 'translateTime'], 'Display epoch timestamps as UTC ISO format or according to an optional format string (default ISO 8601)')
.option(['i', 'ignore'], 'Ignore one or several keys: (`-i time,hostname`)')
.option(['H', 'hideObject'], 'Hide objects from output (but not error object)')
.option(['S', 'singleLine'], 'Print all non-error objects on a single line')
.option('config', 'specify a path to a json file containing the pino-pretty options')
const cmd = minimist(process.argv.slice(2))

args
.example('cat log | pino-pretty', 'To prettify logs, simply pipe a log file through')
.example('cat log | pino-pretty -m fooMessage', 'To highlight a string at a key other than \'msg\'')
.example('cat log | pino-pretty --levelKey fooLevel', 'To detect the log level at a key other than \'level\'')
.example('cat log | pino-pretty --levelLabel LVL -o "{LVL}"', 'To output the log level label using a key other than \'levelLabel\'')
.example('cat log | pino-pretty -a fooTimestamp', 'To display timestamp from a key other than \'time\'')
.example('cat log | pino-pretty -t', 'To convert Epoch timestamps to ISO timestamps use the -t option')
.example('cat log | pino-pretty -t "SYS:yyyy-mm-dd HH:MM:ss"', 'To convert Epoch timestamps to local timezone format use the -t option with "SYS:" prefixed format string')
.example('cat log | pino-pretty -l', 'To flip level and time/date in standard output use the -l option')
.example('cat log | pino-pretty -L info', 'Only prints messages with a minimum log level of info')
.example('cat log | pino-pretty -i pid,hostname', 'Prettify logs but don\'t print pid and hostname')
.example('cat log | pino-pretty --config=/path/to/config.json', 'Loads options from a config file')
helper(cmd)

const DEFAULT_VALUE = '\0default'

let opts = args.parse(process.argv, {
mri: {
default: {
messageKey: DEFAULT_VALUE,
minimumLevel: DEFAULT_VALUE,
levelKey: DEFAULT_VALUE,
timestampKey: DEFAULT_VALUE
},
// NOTE: The following key-value pairs values should be in sync with the
// short version values defined in each `args.option([value, key], ...)`
alias: {
messageKey: 'm',
minimumLevel: 'L',
timestampKey: 'a'
}
let opts = minimist(process.argv, {
alias: {
colorize: 'c',
crlf: 'f',
errorProps: 'e',
levelFirst: 'l',
minimumLevel: 'L',
customLevels: 'x',
customColors: 'X',
useOnlyCustomProps: 'U',
errorLikeObjectKeys: 'k',
messageKey: 'm',
levelKey: CONSTANTS.LEVEL_KEY,
levelLabel: 'b',
messageFormat: 'o',
timestampKey: 'a',
translateTime: 't',
ignore: 'i',
hideObject: 'H',
singleLine: 'S'
},
default: {
messageKey: DEFAULT_VALUE,
minimumLevel: DEFAULT_VALUE,
levelKey: DEFAULT_VALUE,
timestampKey: DEFAULT_VALUE
}
})

Expand Down Expand Up @@ -121,3 +106,9 @@ function filter (obj, cb) {
return acc
}, {})
}

function helper (cmd) {
if (cmd.h || cmd.help) {
help.toStdout()
}
}
64 changes: 64 additions & 0 deletions help/help.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
Usage: pino pretty [options] [command]

Commands:
help Display help
version Display version

Options:
-c, --colorize Force adding color sequences to the output
-C, --config specify a path to a json file containing the pino-pretty options
-f, --crlf Append CRLF instead of LF to formatted lines
-X, --customColors Override default colors using names from https://www.npmjs.com/package/colorette (`-X err:red,info:blue`)
-x, --customLevels Override default levels (`-x err:99,info:1`)
-k, --errorLikeObjectKeys Define which keys contain error objects (`-k err,error`) (defaults to `err,error`)
-e, --errorProps Comma separated list of properties on error objects to show (`*` for all properties) (defaults to ``)
-h, --help Output usage information
-H, --hideObject Hide objects from output (but not error object)
-i, --ignore Ignore one or several keys: (`-i time,hostname`)
-l, --levelFirst Display the log level as the first output field
-L, --levelKey [value] Detect the log level under the specified key (defaults to "level")
-b, --levelLabel [value] Output the log level using the specified label (defaults to "levelLabel")
-o, --messageFormat Format output of message
-m, --messageKey [value] Highlight the message under the specified key (defaults to "msg")
-L, --minimumLevel Hide messages below the specified log level
-S, --singleLine Print all non-error objects on a single line
-a, --timestampKey [value] Display the timestamp from the specified key (defaults to "time")
-t, --translateTime Display epoch timestamps as UTC ISO format or according to an optional format string (default ISO 8601)
-U, --useOnlyCustomProps Only use custom levels and colors (if provided); don't fallback to default levels and colors (-U false)
-v, --version Output the version number

Examples:
- To prettify logs, simply pipe a log file through
$ cat log | pino-pretty

- To highlight a string at a key other than 'msg'
$ cat log | pino-pretty -m fooMessage

- To detect the log level at a key other than 'level'
$ cat log | pino-pretty --levelKey fooLevel

- To output the log level label using a key other than 'levelLabel'
$ cat log | pino-pretty --levelLabel LVL -o "{LVL}"

- To display timestamp from a key other than 'time'
$ cat log | pino-pretty -a fooTimestamp

- To convert Epoch timestamps to ISO timestamps use the -t option
$ cat log | pino-pretty -t

- To convert Epoch timestamps to local timezone format use the -t option with "SYS:" prefixed format string
$ cat log | pino-pretty -t "SYS:yyyy-mm-dd HH:MM:ss"

- To flip level and time/date in standard output use the -l option
$ cat log | pino-pretty -l

- Only prints messages with a minimum log level of info
$ cat log | pino-pretty -L info

- Prettify logs but don't print pid and hostname
$ cat log | pino-pretty -i pid,hostname

- Loads options from a config file
$ cat log | pino-pretty --config=/path/to/config.json


3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@
"test"
],
"dependencies": {
"args": "5.0.1",
"colorette": "^2.0.7",
"dateformat": "^4.6.3",
"fast-copy": "^2.1.1",
"fast-safe-stringify": "^2.1.1",
"joycon": "^3.1.1",
"help-me": "^4.0.1",
"minimist": "^1.2.6",
"on-exit-leak-free": "^1.0.0",
"pino-abstract-transport": "^1.0.0",
"pump": "^3.0.0",
Expand Down
12 changes: 12 additions & 0 deletions test/cli-rc.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,5 +239,17 @@ test('cli', (t) => {
t.teardown(() => child.kill())
})

t.test('test help', (t) => {
t.plan(1)
const env = { TERM: 'dumb' }
const child = spawn(process.argv[0], [bin, '--help'], { env })
const file = fs.readFileSync('help/help.txt').toString()
child.on('error', t.threw)
child.stdout.on('data', (data) => {
t.equal(data.toString(), file)
})
t.teardown(() => child.kill())
})

t.end()
})