pino-pretty
Advanced tools
Comparing version 7.3.0 to 7.4.0
@@ -94,3 +94,3 @@ 'use strict' | ||
const prettifiedLevel = prettifyLevel({ log, colorizer, levelKey, prettifier: customPrettifiers.level }) | ||
const prettifiedMetadata = prettifyMetadata({ log }) | ||
const prettifiedMetadata = prettifyMetadata({ log, prettifiers: customPrettifiers }) | ||
const prettifiedTime = prettifyTime({ log, translateFormat: opts.translateTime, timestampKey, prettifier: customPrettifiers.time }) | ||
@@ -197,3 +197,3 @@ | ||
mkdir: opts.mkdir, | ||
sync: false | ||
sync: opts.sync // by default sonic will be async | ||
}) | ||
@@ -200,0 +200,0 @@ } |
@@ -284,2 +284,6 @@ 'use strict' | ||
* be prettified. | ||
* @param {object} input.prettifiers A set of functions used to prettify each | ||
* key of the input log's metadata. The keys are the keys of the metadata (like | ||
* `hostname`, `pid`, `name`, etc), and the values are functions which take the | ||
* metadata value and return a string. Each key is optional. | ||
* | ||
@@ -289,3 +293,3 @@ * @returns {undefined|string} If no metadata is found then `undefined` is | ||
*/ | ||
function prettifyMetadata ({ log }) { | ||
function prettifyMetadata ({ log, prettifiers = {} }) { | ||
let line = '' | ||
@@ -297,9 +301,12 @@ | ||
if (log.name) { | ||
line += log.name | ||
line += prettifiers.name ? prettifiers.name(log.name) : log.name | ||
} | ||
if (log.name && log.pid) { | ||
line += '/' + log.pid | ||
} else if (log.pid) { | ||
line += log.pid | ||
if (log.pid) { | ||
const prettyPid = prettifiers.pid ? prettifiers.pid(log.pid) : log.pid | ||
if (log.name && log.pid) { | ||
line += '/' + prettyPid | ||
} else { | ||
line += prettyPid | ||
} | ||
} | ||
@@ -310,3 +317,3 @@ | ||
// the leading space. | ||
line += `${line === '(' ? 'on' : ' on'} ${log.hostname}` | ||
line += `${line === '(' ? 'on' : ' on'} ${prettifiers.hostname ? prettifiers.hostname(log.hostname) : log.hostname}` | ||
} | ||
@@ -318,3 +325,3 @@ | ||
if (log.caller) { | ||
line += `${line === '' ? '' : ' '}<${log.caller}>` | ||
line += `${line === '' ? '' : ' '}<${prettifiers.caller ? prettifiers.caller(log.caller) : log.caller}>` | ||
} | ||
@@ -321,0 +328,0 @@ |
{ | ||
"name": "pino-pretty", | ||
"version": "7.3.0", | ||
"version": "7.4.0", | ||
"description": "Prettifier for Pino log lines", | ||
@@ -49,3 +49,3 @@ "type": "commonjs", | ||
"devDependencies": { | ||
"@types/node": "^16.9.2", | ||
"@types/node": "^17.0.0", | ||
"pino": "^7.0.0", | ||
@@ -52,0 +52,0 @@ "pre-commit": "^1.2.2", |
@@ -147,3 +147,3 @@ <a id="intro"></a> | ||
const stream = pretty({ | ||
prettyPrint: { colorize: true } | ||
colorize: true | ||
}) | ||
@@ -157,2 +157,5 @@ const logger = pino(stream) | ||
### Usage as a stream | ||
If you are using `pino-pretty` as a stream and you need to provide options to `pino`, | ||
@@ -165,3 +168,3 @@ pass the options as the first argument and `pino-pretty` as second argument: | ||
const stream = pretty({ | ||
prettyPrint: { colorize: true } | ||
colorize: true | ||
}) | ||
@@ -174,3 +177,19 @@ const logger = pino({ level: 'info' }, stream) | ||
### Usage with Jest | ||
Logging with Jest is _problematic_, as the test framework requires no asynchronous operation to | ||
continue after the test has finished. The following is the only supported way to use this module | ||
with Jest: | ||
```js | ||
import pino from 'pino' | ||
import pretty from 'pino-pretty' | ||
test('test pino-pretty', () => { | ||
const logger = pino(pretty({ sync: true })); | ||
logger.info('Info'); | ||
logger.error('Error'); | ||
}); | ||
``` | ||
### Handling non-serializable options | ||
@@ -237,2 +256,8 @@ | ||
// You can also configure some SonicBoom options directly | ||
sync: false, // by default we write asynchronously | ||
append: true, // the file is opened with the 'a' flag | ||
mdkdir: true, // create the target destination | ||
customPrettifiers: {} | ||
@@ -263,3 +288,3 @@ } | ||
Additionally, `customPrettifiers` can be used to format the time and level | ||
Additionally, `customPrettifiers` can be used to format the `time`, `hostname`, `pid`, `name`, `caller` and `level` | ||
outputs: | ||
@@ -278,2 +303,8 @@ | ||
level: logLevel => `LEVEL: ${logLevel}` | ||
// other prettifiers can be used for the other keys if needed, for example | ||
hostname: hostname => colorGreen(hostname) | ||
pid: pid => colorRed(hostname) | ||
name: name => colorBlue(name) | ||
caller: caller => colorCyan(caller) | ||
} | ||
@@ -280,0 +311,0 @@ } |
@@ -173,2 +173,42 @@ 'use strict' | ||
t.test('can use a customPrettifier on name output', (t) => { | ||
t.plan(1) | ||
const customPrettifiers = { | ||
name: (hostname) => `NAME: ${hostname}` | ||
} | ||
const pretty = prettyFactory({ customPrettifiers }) | ||
const log = pino({}, new Writable({ | ||
write (chunk, enc, cb) { | ||
const formatted = pretty(chunk.toString()) | ||
t.equal( | ||
formatted, | ||
`[${epoch}] INFO (NAME: logger/${pid} on ${hostname}): foo\n` | ||
) | ||
cb() | ||
} | ||
})) | ||
const child = log.child({ name: 'logger' }) | ||
child.info({ msg: 'foo' }) | ||
}) | ||
t.test('can use a customPrettifier on hostname and pid output', (t) => { | ||
t.plan(1) | ||
const customPrettifiers = { | ||
hostname: (hostname) => `HOSTNAME: ${hostname}`, | ||
pid: (pid) => `PID: ${pid}` | ||
} | ||
const pretty = prettyFactory({ customPrettifiers }) | ||
const log = pino({}, new Writable({ | ||
write (chunk, enc, cb) { | ||
const formatted = pretty(chunk.toString()) | ||
t.equal( | ||
formatted, | ||
`[${epoch}] INFO (PID: ${pid} on HOSTNAME: ${hostname}): foo\n` | ||
) | ||
cb() | ||
} | ||
})) | ||
log.info({ msg: 'foo' }) | ||
}) | ||
t.test('can use a customPrettifier on default time output', (t) => { | ||
@@ -193,2 +233,21 @@ t.plan(1) | ||
t.test('can use a customPrettifier on the caller', (t) => { | ||
t.plan(1) | ||
const customPrettifiers = { | ||
caller: (caller) => `CALLER: ${caller}` | ||
} | ||
const pretty = prettyFactory({ customPrettifiers }) | ||
const log = pino({}, new Writable({ | ||
write (chunk, enc, cb) { | ||
const formatted = pretty(chunk.toString()) | ||
t.equal( | ||
formatted, | ||
`[${epoch}] INFO (${pid} on ${hostname}) <CALLER: test.js:10>: foo\n` | ||
) | ||
cb() | ||
} | ||
})) | ||
log.info({ msg: 'foo', caller: 'test.js:10' }) | ||
}) | ||
t.test('can use a customPrettifier on translateTime-time output', (t) => { | ||
@@ -907,2 +966,25 @@ t.plan(1) | ||
t.test('sync option', async (t) => { | ||
t.plan(1) | ||
const tmpDir = path.join(__dirname, '.tmp_' + Date.now()) | ||
t.teardown(() => rimraf(tmpDir, noop)) | ||
const destination = join(tmpDir, 'output') | ||
const pretty = pinoPretty({ | ||
singleLine: true, | ||
colorize: false, | ||
mkdir: true, | ||
append: false, | ||
sync: true, | ||
destination | ||
}) | ||
const log = pino(pretty) | ||
log.info({ msg: 'message', extra: { foo: 'bar', number: 42 }, upper: 'foobar' }) | ||
const formatted = fs.readFileSync(destination, 'utf8') | ||
t.equal(formatted, `[${epoch}] INFO (${pid} on ${hostname}): message {"extra":{"foo":"bar","number":42},"upper":"foobar"}\n`) | ||
}) | ||
t.end() | ||
@@ -909,0 +991,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
317776
3379
343