Socket
Socket
Sign inDemoInstall

pino-pretty

Package Overview
Dependencies
Maintainers
4
Versions
88
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pino-pretty - npm Package Compare versions

Comparing version 7.2.0 to 7.3.0

5

bin.js

@@ -22,2 +22,3 @@ #!/usr/bin/env node

files: [
'pino-pretty.config.cjs',
'pino-pretty.config.js',

@@ -29,6 +30,2 @@ '.pino-prettyrc',

})
joycon.addLoader({
test: /\.[^.]*rc$/,
loadSync: (path) => parseJSON(fs.readFileSync(path, 'utf-8'))
})

@@ -35,0 +32,0 @@ args

15

index.d.ts

@@ -1,9 +0,15 @@

// Type definitions for pino-pretty 4.7
// Type definitions for pino-pretty 7.0
// Project: https://github.com/pinojs/pino-pretty#readme
// Definitions by: Adam Vigneaux <https://github.com/AdamVig>
// tearwyx <https://github.com/tearwyx>
// Minimum TypeScript Version: 3.0
/// <reference types="node" />
import { Transform } from 'stream';
import { OnUnknown } from 'pino-abstract-transport';
type LogDescriptor = Record<string, unknown>;
declare function PinoPretty(options: PrettyOptions_): PinoPretty.Prettifier;
declare function PinoPretty(options: PrettyOptions_): PinoPretty.PrettyStream;

@@ -124,6 +130,7 @@ interface PrettyOptions_ {

type MessageFormatFunc = (log: LogDescriptor, messageKey: string, levelLabel: string) => string;
type PrettyOptions = PrettyOptions_
type PrettyOptions = PrettyOptions_;
type PrettyStream = Transform & OnUnknown;
}
export default PinoPretty;
export { PinoPretty, PrettyOptions_ as PrettyOptions }
export { PinoPretty, PrettyOptions_ as PrettyOptions };

@@ -93,5 +93,5 @@ 'use strict'

const prettifiedLevel = prettifyLevel({ log, colorizer, levelKey })
const prettifiedLevel = prettifyLevel({ log, colorizer, levelKey, prettifier: customPrettifiers.level })
const prettifiedMetadata = prettifyMetadata({ log })
const prettifiedTime = prettifyTime({ log, translateFormat: opts.translateTime, timestampKey })
const prettifiedTime = prettifyTime({ log, translateFormat: opts.translateTime, timestampKey, prettifier: customPrettifiers.time })

@@ -150,2 +150,3 @@ let line = ''

})
if (singleLine) line += EOL
line += prettifiedErrorLog

@@ -200,9 +201,2 @@ } else if (!hideObject) {

}
/* istanbul ignore else */
if (destination.fd === 1) {
// We cannot close the output
destination.end = function () {
this.emit('close')
}
}

@@ -220,2 +214,3 @@ source.on('unknown', function (line) {

module.exports.prettyFactory = prettyFactory
module.exports.colorizerFactory = colors
module.exports.default = build

@@ -221,2 +221,3 @@ 'use strict'

* @param {string} [levelKey='level'] The key to find the level under.
* @param {function} [input.prettifier] A user-supplied formatter to be called instead of colorizer.
*

@@ -227,5 +228,6 @@ * @returns {undefined|string} If `log` does not have a `level` property then

*/
function prettifyLevel ({ log, colorizer = defaultColorizer, levelKey = LEVEL_KEY }) {
function prettifyLevel ({ log, colorizer = defaultColorizer, levelKey = LEVEL_KEY, prettifier }) {
if (levelKey in log === false) return undefined
return colorizer(log[levelKey])
const output = log[levelKey]
return prettifier ? prettifier(output) : colorizer(output)
}

@@ -398,3 +400,3 @@

const joinedLines = joinLinesWithIndentation({ input: lines, ident, eol })
result += `${ident}${keyName}: ${joinedLines}${eol}`
result += `${ident}${keyName}:${joinedLines.startsWith(eol) ? '' : ' '}${joinedLines}${eol}`
})

@@ -429,2 +431,3 @@ }

* string to determine the output; see the `formatTime` function for details.
* @param {function} [input.prettifier] A user-supplied formatter for altering output.
*

@@ -435,3 +438,3 @@ * @returns {undefined|string} If a timestamp property cannot be found then

*/
function prettifyTime ({ log, timestampKey = TIMESTAMP_KEY, translateFormat = undefined }) {
function prettifyTime ({ log, timestampKey = TIMESTAMP_KEY, translateFormat = undefined, prettifier }) {
let time = null

@@ -446,7 +449,5 @@

if (time === null) return undefined
if (translateFormat) {
return '[' + formatTime(time, translateFormat) + ']'
}
const output = translateFormat ? formatTime(time, translateFormat) : time
return `[${time}]`
return prettifier ? prettifier(output) : `[${output}]`
}

@@ -453,0 +454,0 @@

{
"name": "pino-pretty",
"version": "7.2.0",
"version": "7.3.0",
"description": "Prettifier for Pino log lines",

@@ -39,3 +39,3 @@ "type": "commonjs",

"fast-safe-stringify": "^2.0.7",
"joycon": "^3.0.0",
"joycon": "^3.1.1",
"pino-abstract-transport": "^0.5.0",

@@ -57,3 +57,3 @@ "pump": "^3.0.0",

"tap": "^15.0.9",
"tsd": "^0.18.0",
"tsd": "^0.19.0",
"typescript": "^4.4.3"

@@ -60,0 +60,0 @@ },

@@ -156,2 +156,18 @@ <a id="intro"></a>

If you are using `pino-pretty` as a stream and you need to provide options to `pino`,
pass the options as the first argument and `pino-pretty` as second argument:
```js
const pino = require('pino')
const pretty = require('pino-pretty')
const stream = pretty({
prettyPrint: { colorize: true }
})
const logger = pino({ level: 'info' }, stream)
// Nothing is printed
logger.debug('hi')
```
### Handling non-serializable options

@@ -230,2 +246,3 @@

you can specify a prettifier for it:
```js

@@ -243,3 +260,35 @@ {

Additionally, `customPrettifiers` can be used to format the time and level
outputs:
```js
{
customPrettifiers: {
// The argument for this function will be the same
// string that's at the start of the log-line by default:
time: timestamp => `🕰 ${timestamp}`,
// The argument for the level-prettifier may vary depending
// on if the levelKey option is used or not.
// By default this will be the same numerics as the Pino default:
level: logLevel => `LEVEL: ${logLevel}`
}
}
```
Note that prettifiers do not include any coloring, if the stock coloring on
`level` is desired, it can be accomplished using the following:
```js
const { colorizerFactory } = require('pino-pretty')
const levelColorize = colorizerFactory(true)
const levelPrettifier = logLevel => `LEVEL: ${levelColorize(logLevel)}`
//...
{
customPrettifiers: { level: levelPrettifier }
}
```
`messageFormat` option allows you to customize the message output. A template `string` like this can define the format:
```js

@@ -250,3 +299,5 @@ {

```
This option can also be defined as a `function` with this prototype:
```js

@@ -253,0 +304,0 @@ {

@@ -30,2 +30,3 @@ 'use strict'

const epoch = 1522431328992
const formattedEpoch = '2018-03-30 17:35:28.992 +0000'
const pid = process.pid

@@ -131,2 +132,82 @@ const hostname = os.hostname()

t.test('can use a customPrettifier on default level output', (t) => {
t.plan(1)
const veryCustomLevels = {
30: 'ok',
40: 'not great'
}
const customPrettifiers = {
level: (level) => `LEVEL: ${veryCustomLevels[level]}`
}
const pretty = prettyFactory({ customPrettifiers })
const log = pino({}, new Writable({
write (chunk, enc, cb) {
const formatted = pretty(chunk.toString())
t.equal(
formatted,
`[${epoch}] LEVEL: ok (${pid} on ${hostname}): foo\n`
)
cb()
}
}))
log.info({ msg: 'foo' })
})
t.test('can use a customPrettifier on different-level-key output', (t) => {
t.plan(1)
const customPrettifiers = {
level: (level) => `LEVEL: ${level.toUpperCase()}`
}
const pretty = prettyFactory({ levelKey: 'bar', customPrettifiers })
const log = pino({}, new Writable({
write (chunk, enc, cb) {
const formatted = pretty(chunk.toString())
t.equal(
formatted,
`[${epoch}] LEVEL: WARN (${pid} on ${hostname}): foo\n`
)
cb()
}
}))
log.info({ msg: 'foo', bar: 'warn' })
})
t.test('can use a customPrettifier on default time output', (t) => {
t.plan(1)
const customPrettifiers = {
time: (timestamp) => `TIME: ${timestamp}`
}
const pretty = prettyFactory({ customPrettifiers })
const log = pino({}, new Writable({
write (chunk, enc, cb) {
const formatted = pretty(chunk.toString())
t.equal(
formatted,
`TIME: ${epoch} INFO (${pid} on ${hostname}): foo\n`
)
cb()
}
}))
log.info('foo')
})
t.test('can use a customPrettifier on translateTime-time output', (t) => {
t.plan(1)
const customPrettifiers = {
time: (timestamp) => `TIME: ${timestamp}`
}
const pretty = prettyFactory({ customPrettifiers, translateTime: true })
const log = pino({}, new Writable({
write (chunk, enc, cb) {
const formatted = pretty(chunk.toString())
t.equal(
formatted,
`TIME: ${formattedEpoch} INFO (${pid} on ${hostname}): foo\n`
)
cb()
}
}))
log.info('foo')
})
t.test('will format time to UTC', (t) => {

@@ -140,3 +221,3 @@ t.plan(1)

formatted,
`[2018-03-30 17:35:28.992 +0000] INFO (${pid} on ${hostname}): foo\n`
`[${formattedEpoch}] INFO (${pid} on ${hostname}): foo\n`
)

@@ -552,6 +633,6 @@ cb()

' msg: {',
' "a": "[Circular]",',
' "b": {',
' "c": "d"',
' },',
' "a": "[Circular ~]"',
' }',
' }'

@@ -590,2 +671,13 @@ ]

t.test('does not add trailing space if prettified value begins with eol', (t) => {
t.plan(1)
const pretty = prettyFactory({
customPrettifiers: {
calls: val => '\n' + val.map(it => ' ' + it).join('\n')
}
})
const arst = pretty('{"msg":"doing work","calls":["step 1","step 2","step 3"],"level":30}')
t.equal(arst, 'INFO: doing work\n calls:\n step 1\n step 2\n step 3\n')
})
t.test('does not prettify custom key that does not exists', (t) => {

@@ -764,2 +856,28 @@ t.plan(1)

t.test('does not call fs.close on stdout stream', (t) => {
t.plan(2)
const destination = pino.destination({ minLength: 4096, sync: true })
const prettyDestination = pinoPretty({ destination, colorize: false })
const log = pino(prettyDestination)
log.info('this message has been buffered')
const chunks = []
const { close, writeSync } = fs
let closeCalled = false
fs.close = new Proxy(close, {
apply: (target, self, args) => {
closeCalled = true
}
})
fs.writeSync = new Proxy(writeSync, {
apply: (target, self, args) => {
chunks.push(args[1])
return args[1].length
}
})
destination.end()
Object.assign(fs, { close, writeSync })
t.match(chunks.join(''), /INFO .+: this message has been buffered/)
t.equal(closeCalled, false)
})
t.test('stream usage', async (t) => {

@@ -766,0 +884,0 @@ t.plan(1)

@@ -38,2 +38,25 @@ 'use strict'

t.test('loads and applies default config file: pino-pretty.config.cjs', (t) => {
t.plan(1)
// Set translateTime: true on run configuration
const configFile = path.join(tmpDir, 'pino-pretty.config.cjs')
fs.writeFileSync(configFile, 'module.exports = { translateTime: true }')
// Tell the loader to expect ESM modules
const packageJsonFile = path.join(tmpDir, 'package.json')
fs.writeFileSync(packageJsonFile, JSON.stringify({ type: 'module' }, null, 4))
const env = { TERM: 'dumb' }
const child = spawn(process.argv[0], [bin], { env, cwd: tmpDir })
// Validate that the time has been translated
child.on('error', t.threw)
child.stdout.on('data', (data) => {
t.equal(data.toString(), '[2018-03-30 17:35:28.992 +0000] INFO (42 on foo): hello world\n')
})
child.stdin.write(logLine)
t.teardown(() => {
fs.unlinkSync(configFile)
fs.unlinkSync(packageJsonFile)
child.kill()
})
})
t.test('loads and applies default config file: .pino-prettyrc', (t) => {

@@ -40,0 +63,0 @@ t.plan(1)

@@ -284,3 +284,25 @@ 'use strict'

t.test('errorProps: legacy error object at top level', { only: true }, function (t) {
t.test('prettifies legacy error object at top level when singleLine=true', function (t) {
t.plan(4)
const pretty = prettyFactory({ singleLine: true })
const err = Error('hello world')
const expected = err.stack.split('\n')
expected.unshift(err.message)
const log = pino({}, new Writable({
write (chunk, enc, cb) {
const formatted = pretty(chunk.toString())
const lines = formatted.split('\n')
t.equal(lines.length, expected.length + 1)
t.equal(lines[0], `[${epoch}] INFO (${pid} on ${hostname}): ${expected[0]}`)
t.equal(lines[1], ` ${expected[1]}`)
t.equal(lines[2], ` ${expected[2]}`)
cb()
}
}))
log.info({ type: 'Error', stack: err.stack, msg: err.message })
})
t.test('errorProps: legacy error object at top level', function (t) {
const pretty = prettyFactory({ errorProps: '*' })

@@ -287,0 +309,0 @@ const expectedLines = [

'use strict'
const { test } = require('tap')
const getColorizer = require('../../lib/colors')
const getColorizerPrivate = require('../../lib/colors')
const { colorizerFactory: getColorizerPublic } = require('../../index')
test('returns default colorizer', async t => {
const testDefaultColorizer = getColorizer => async t => {
const colorizer = getColorizer()

@@ -40,5 +41,5 @@ let colorized = colorizer(10)

t.equal(colorized, 'foo')
})
}
test('returns colorizing colorizer', async t => {
const testColoringColorizer = getColorizer => async t => {
const colorizer = getColorizer(true)

@@ -77,2 +78,7 @@ let colorized = colorizer(10)

t.equal(colorized, '\u001B[90mfoo\u001B[39m')
})
}
test('returns default colorizer - private export', testDefaultColorizer(getColorizerPrivate))
test('returns default colorizer - public export', testDefaultColorizer(getColorizerPublic))
test('returns colorizing colorizer - private export', testColoringColorizer(getColorizerPrivate))
test('returns colorizing colorizer - public export', testColoringColorizer(getColorizerPublic))

@@ -1,9 +0,9 @@

import prettyFactory from "../../";
import { expectType } from "tsd";
import pretty from "../../";
import PinoPretty, { PinoPretty as PinoPrettyNamed, PrettyOptions } from "../../";
import PinoPrettyDefault from "../../";
import * as PinoPrettyStar from "../../";
import PinoPrettyCjsImport = require ("../../");
import Prettifier = PinoPretty.Prettifier;
import PinoPrettyCjsImport = require("../../");
import PrettyStream = PinoPretty.PrettyStream;
const PinoPrettyCjs = require("../../");

@@ -55,14 +55,9 @@

const pretty = prettyFactory(options);
expectType<Prettifier>(pretty);
expectType<Prettifier>(PinoPrettyNamed(options));
expectType<Prettifier>(PinoPrettyDefault(options));
expectType<Prettifier>(PinoPrettyStar.PinoPretty(options));
expectType<Prettifier>(PinoPrettyStar.default(options));
expectType<Prettifier>(PinoPrettyCjsImport.PinoPretty(options));
expectType<Prettifier>(PinoPrettyCjsImport.default(options));
expectType<PrettyStream>(pretty(options));
expectType<PrettyStream>(PinoPrettyNamed(options));
expectType<PrettyStream>(PinoPrettyDefault(options));
expectType<PrettyStream>(PinoPrettyStar.PinoPretty(options));
expectType<PrettyStream>(PinoPrettyStar.default(options));
expectType<PrettyStream>(PinoPrettyCjsImport.PinoPretty(options));
expectType<PrettyStream>(PinoPrettyCjsImport.default(options));
expectType<any>(PinoPrettyCjs(options));
expectType<string>(pretty({ foo: "bar" }));
expectType<string>(pretty('dummy'));

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc