UNPKG

4.5 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3const fs = require('fs')
4const args = require('args')
5const path = require('path')
6const pump = require('pump')
7const split = require('split2')
8const { Transform } = require('readable-stream')
9const prettyFactory = require('./')
10const CONSTANTS = require('./lib/constants')
11const { isObject } = require('./lib/utils')
12
13const bourne = require('@hapi/bourne')
14const stripJsonComments = require('strip-json-comments')
15const parseJSON = input => {
16 return bourne.parse(stripJsonComments(input), { protoAction: 'remove' })
17}
18
19const JoyCon = require('joycon')
20const joycon = new JoyCon({
21 parseJSON,
22 files: [
23 'pino-pretty.config.js',
24 '.pino-prettyrc',
25 '.pino-prettyrc.json'
26 ],
27 stopDir: path.dirname(process.cwd())
28})
29joycon.addLoader({
30 test: /\.[^.]*rc$/,
31 loadSync: (path) => parseJSON(fs.readFileSync(path, 'utf-8'))
32})
33
34args
35 .option(['c', 'colorize'], 'Force adding color sequences to the output')
36 .option(['f', 'crlf'], 'Append CRLF instead of LF to formatted lines')
37 .option(['e', 'errorProps'], 'Comma separated list of properties on error objects to show (`*` for all properties)', '')
38 .option(['l', 'levelFirst'], 'Display the log level as the first output field')
39 .option(['k', 'errorLikeObjectKeys'], 'Define which keys contain error objects (`-k err,error`)', 'err,error')
40 .option(['m', 'messageKey'], 'Highlight the message under the specified key', CONSTANTS.MESSAGE_KEY)
41 .option(['o', 'messageFormat'], 'Format output of message')
42 .option(['a', 'timestampKey'], 'Display the timestamp from the specified key', CONSTANTS.TIMESTAMP_KEY)
43 .option(['t', 'translateTime'], 'Display epoch timestamps as UTC ISO format or according to an optional format string (default ISO 8601)')
44 .option(['s', 'search'], 'Specify a search pattern according to jmespath')
45 .option(['i', 'ignore'], 'Ignore one or several keys: (`-i time,hostname`)')
46 .option('config', 'specify a path to a json file containing the pino-pretty options')
47
48args
49 .example('cat log | pino-pretty', 'To prettify logs, simply pipe a log file through')
50 .example('cat log | pino-pretty -m fooMessage', 'To highlight a string at a key other than \'msg\', use')
51 .example('cat log | pino-pretty -a fooTimestamp', 'To display timestamp from a key other than \'time\', use')
52 .example('cat log | pino-pretty -t', 'To convert Epoch timestamps to ISO timestamps use the -t option')
53 .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')
54 .example('cat log | pino-pretty -l', 'To flip level and time/date in standard output use the -l option')
55 .example('cat log | pino-pretty -s "msg == \'hello world\'"', 'Only prints messages with msg equals to \'hello world\'')
56 .example('cat log | pino-pretty -i pid,hostname', 'Prettify logs but don\'t print pid and hostname')
57 .example('cat log | pino-pretty --config=/path/to/config.json', 'Loads options from a config file')
58
59const DEFAULT_VALUE = '\0default'
60
61let opts = args.parse(process.argv, {
62 mri: {
63 default: {
64 messageKey: DEFAULT_VALUE,
65 timestampKey: DEFAULT_VALUE
66 }
67 }
68})
69// Remove default values
70opts = filter(opts, value => value !== DEFAULT_VALUE)
71const config = loadConfig(opts.config)
72// Override config with cli options
73opts = Object.assign({}, config, opts)
74const pretty = prettyFactory(opts)
75const prettyTransport = new Transform({
76 objectMode: true,
77 transform (chunk, enc, cb) {
78 const line = pretty(chunk.toString())
79 if (line === undefined) return cb()
80 cb(null, line)
81 }
82})
83
84pump(process.stdin, split(), prettyTransport, process.stdout)
85
86// https://github.com/pinojs/pino/pull/358
87if (!process.stdin.isTTY && !fs.fstatSync(process.stdin.fd).isFile()) {
88 process.once('SIGINT', function noOp () {})
89}
90
91function loadConfig (configPath) {
92 const files = configPath ? [path.resolve(configPath)] : undefined
93 const result = joycon.loadSync(files)
94 if (result.path && !isObject(result.data)) {
95 configPath = configPath || path.basename(result.path)
96 throw new Error(`Invalid runtime configuration file: ${configPath}`)
97 }
98 if (configPath && !result.data) {
99 throw new Error(`Failed to load runtime configuration file: ${configPath}`)
100 }
101 return result.data
102}
103
104function filter (obj, cb) {
105 return Object.keys(obj).reduce((acc, key) => {
106 const value = obj[key]
107 if (cb(value, key)) {
108 acc[key] = value
109 }
110 return acc
111 }, {})
112}