pino-pretty
Advanced tools
Comparing version 3.3.0 to 3.4.0
65
bin.js
@@ -5,2 +5,3 @@ #!/usr/bin/env node | ||
const args = require('args') | ||
const path = require('path') | ||
const pump = require('pump') | ||
@@ -11,3 +12,25 @@ const split = require('split2') | ||
const CONSTANTS = require('./lib/constants') | ||
const { isObject } = require('./lib/utils') | ||
const bourne = require('@hapi/bourne') | ||
const stripJsonComments = require('strip-json-comments') | ||
const parseJSON = input => { | ||
return bourne.parse(stripJsonComments(input), { protoAction: 'remove' }) | ||
} | ||
const JoyCon = require('joycon') | ||
const joycon = new JoyCon({ | ||
parseJSON, | ||
files: [ | ||
'pino-pretty.config.js', | ||
'.pino-prettyrc', | ||
'.pino-prettyrc.json' | ||
], | ||
stopDir: path.dirname(process.cwd()) | ||
}) | ||
joycon.addLoader({ | ||
test: /\.[^.]*rc$/, | ||
loadSync: (path) => parseJSON(fs.readFileSync(path, 'utf-8')) | ||
}) | ||
args | ||
@@ -20,2 +43,3 @@ .option(['c', 'colorize'], 'Force adding color sequences to the output') | ||
.option(['m', 'messageKey'], 'Highlight the message under the specified key', CONSTANTS.MESSAGE_KEY) | ||
.option(['o', 'messageFormat'], 'Format output of message') | ||
.option(['a', 'timestampKey'], 'Display the timestamp from the specified key', CONSTANTS.TIMESTAMP_KEY) | ||
@@ -25,2 +49,3 @@ .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('config', 'specify a path to a json file containing the pino-pretty options') | ||
@@ -36,4 +61,19 @@ args | ||
.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') | ||
const opts = args.parse(process.argv) | ||
const DEFAULT_VALUE = '\0default' | ||
let opts = args.parse(process.argv, { | ||
mri: { | ||
default: { | ||
messageKey: DEFAULT_VALUE, | ||
timestampKey: DEFAULT_VALUE | ||
} | ||
} | ||
}) | ||
// Remove default values | ||
opts = filter(opts, value => value !== DEFAULT_VALUE) | ||
const config = loadConfig(opts.config) | ||
// Override config with cli options | ||
opts = Object.assign({}, config, opts) | ||
const pretty = prettyFactory(opts) | ||
@@ -55,1 +95,24 @@ const prettyTransport = new Transform({ | ||
} | ||
function loadConfig (configPath) { | ||
const files = configPath ? [path.resolve(configPath)] : undefined | ||
const result = joycon.loadSync(files) | ||
if (result.path && !isObject(result.data)) { | ||
configPath = configPath || path.basename(result.path) | ||
throw new Error(`Invalid runtime configuration file: ${configPath}`) | ||
} | ||
if (configPath && !result.data) { | ||
throw new Error(`Failed to load runtime configuration file: ${configPath}`) | ||
} | ||
return result.data | ||
} | ||
function filter (obj, cb) { | ||
return Object.keys(obj).reduce((acc, key) => { | ||
const value = obj[key] | ||
if (cb(value, key)) { | ||
acc[key] = value | ||
} | ||
return acc | ||
}, {}) | ||
} |
@@ -33,2 +33,3 @@ 'use strict' | ||
messageKey: MESSAGE_KEY, | ||
messageFormat: false, | ||
timestampKey: TIMESTAMP_KEY, | ||
@@ -46,2 +47,3 @@ translateTime: false, | ||
const messageKey = opts.messageKey | ||
const messageFormat = opts.messageFormat | ||
const timestampKey = opts.timestampKey | ||
@@ -80,2 +82,4 @@ const errorLikeObjectKeys = opts.errorLikeObjectKeys | ||
const prettifiedMessage = prettifyMessage({ log, messageKey, colorizer, messageFormat }) | ||
if (ignoreKeys) { | ||
@@ -91,3 +95,2 @@ log = Object.keys(log) | ||
const prettifiedLevel = prettifyLevel({ log, colorizer }) | ||
const prettifiedMessage = prettifyMessage({ log, messageKey, colorizer }) | ||
const prettifiedMetadata = prettifyMetadata({ log }) | ||
@@ -94,0 +97,0 @@ const prettifiedTime = prettifyTime({ log, translateFormat: opts.translateTime, timestampKey }) |
@@ -189,2 +189,4 @@ 'use strict' | ||
* message to be prettified. | ||
* @param {string} [input.messageFormat=undefined] A format string that defines how the | ||
* logged message should be formatted, e.g. `'{level} - {pid}'`. | ||
* @param {function} [input.colorizer] A colorizer function that has a | ||
@@ -198,3 +200,12 @@ * `.message(str)` method attached to it. This function should return a colorized | ||
*/ | ||
function prettifyMessage ({ log, messageKey = MESSAGE_KEY, colorizer = defaultColorizer }) { | ||
function prettifyMessage ({ log, messageFormat, messageKey = MESSAGE_KEY, colorizer = defaultColorizer }) { | ||
if (messageFormat) { | ||
const message = String(messageFormat).replace(/{([^{}]+)}/g, function (match, p1) { | ||
if (p1 && log[p1]) { | ||
return log[p1] | ||
} | ||
return '' | ||
}) | ||
return colorizer.message(message) | ||
} | ||
if (messageKey in log === false) return undefined | ||
@@ -201,0 +212,0 @@ if (typeof log[messageKey] !== 'string') return undefined |
{ | ||
"name": "pino-pretty", | ||
"version": "3.3.0", | ||
"version": "3.4.0", | ||
"description": "Prettifier for Pino log lines", | ||
@@ -39,5 +39,7 @@ "main": "index.js", | ||
"jmespath": "^0.15.0", | ||
"joycon": "^2.2.5", | ||
"pump": "^3.0.0", | ||
"readable-stream": "^3.4.0", | ||
"split2": "^3.1.1" | ||
"split2": "^3.1.1", | ||
"strip-json-comments": "^3.0.1" | ||
}, | ||
@@ -47,2 +49,3 @@ "devDependencies": { | ||
"pre-commit": "^1.2.2", | ||
"rimraf": "^3.0.0", | ||
"snazzy": "^8.0.0", | ||
@@ -49,0 +52,0 @@ "standard": "^14.0.0", |
@@ -65,2 +65,4 @@ <a id="intro"></a> | ||
Default: `msg`. | ||
- `--messageFormat` (`-o`): Format output of message, e.g. `{level} - {pid}` will output message: `INFO - 1123` | ||
Default: `false` | ||
- `--timestampKey` (`-m`): Define the key that contains the log timestamp. | ||
@@ -79,2 +81,3 @@ Default: `time`. | ||
- `--ignore` (`-i`): Ignore one or several keys: (`-i time,hostname`) | ||
- `--config`: Specify a path to a config file containing the pino-pretty options. pino-pretty will attempt to read from a `.pino-prettyrc` in your current directory (`process.cwd`) if not specified | ||
@@ -131,2 +134,3 @@ <a id="integration"></a> | ||
messageKey: 'msg', // --messageKey | ||
messageFormat: false // --messageFormat | ||
timestampKey: 'time', // --timestampKey | ||
@@ -146,3 +150,3 @@ translateTime: false, // --translateTime | ||
log properties which will be prettified and value is the prettify function itself. | ||
For example, if a log line contains a `query` propert | ||
For example, if a log line contains a `query` property, | ||
you can specify a prettifier for it: | ||
@@ -149,0 +153,0 @@ ```js |
@@ -94,2 +94,12 @@ 'use strict' | ||
t.test('returns message formatted by `messageFormat` option', async t => { | ||
const str = prettifyMessage({ log: { msg: 'foo', context: 'appModule' }, messageFormat: '{context} - {msg}' }) | ||
t.is(str, 'appModule - foo') | ||
}) | ||
t.test('`messageFormat` supports nested curly brackets', async t => { | ||
const str = prettifyMessage({ log: { level: 30 }, messageFormat: '{{level}}-{level}-{{level}-{level}}' }) | ||
t.is(str, '{30}-30-{30-30}') | ||
}) | ||
t.end() | ||
@@ -96,0 +106,0 @@ }) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
259929
19
2038
165
11
6
3
2
+ Addedjoycon@^2.2.5
+ Addedstrip-json-comments@^3.0.1
+ Addedjoycon@2.2.5(transitive)
+ Addedstrip-json-comments@3.1.1(transitive)