mirror of
https://github.com/pinojs/pino.git
synced 2025-12-08 20:36:13 +00:00
* Issue #382, pretty print error with null stack (#383) * fix issue #382 pretty print error with null stack * prints stack: "null" when error is a property * errors with a null stack, #382 * Bumped v4.15.4. * added lastTime meta parameter (#385) * Bumped v4.16.0. * Remove CLI and add robust prettifier support * Use required prettifier directly and tweak documentation * Address documentation concerns * Remove chalk * Add bin with deprecation notice * Add URL to deprecation notice * deprecated => removed * Move split2 to dev dependencies * Move asMetaWrapper into Pino and update pretty docs
141 lines
3.4 KiB
JavaScript
141 lines
3.4 KiB
JavaScript
'use strict'
|
|
|
|
var format = require('quick-format-unescaped')
|
|
var util = require('util')
|
|
var levels = require('./levels')
|
|
var serializers = require('pino-std-serializers')
|
|
var isStandardLevelVal = levels.isStandardLevelVal
|
|
var isStandardLevel = levels.isStandardLevel
|
|
|
|
function noop () {}
|
|
|
|
function copy (a, b) {
|
|
for (var k in b) { a[k] = b[k] }
|
|
return a
|
|
}
|
|
|
|
function applyOptions (self, opts) {
|
|
self.serializers = opts.serializers
|
|
self.chindings = opts.chindings
|
|
|
|
if (opts.level && opts.levelVal) {
|
|
var levelIsStandard = isStandardLevel(opts.level)
|
|
var valIsStandard = isStandardLevelVal(opts.levelVal)
|
|
if (valIsStandard) throw Error('level value is already used: ' + opts.levelVal)
|
|
if (levelIsStandard === false && valIsStandard === false) self.addLevel(opts.level, opts.levelVal)
|
|
}
|
|
self._setLevel(opts.level)
|
|
}
|
|
|
|
function asMetaWrapper (pretty, dest) {
|
|
const parsed = Symbol('parsedChindings')
|
|
|
|
if (!dest) {
|
|
dest = process.stdout
|
|
} else if (!dest.write) {
|
|
throw new Error('the destination must be writable')
|
|
}
|
|
|
|
return {
|
|
[Symbol.for('needsMetadata')]: true,
|
|
lastLevel: 0,
|
|
lastMsg: null,
|
|
lastObj: null,
|
|
lastLogger: null,
|
|
write (chunk) {
|
|
var chindings = this.lastLogger[parsed]
|
|
|
|
if (!chindings) {
|
|
chindings = JSON.parse('{"v":1' + this.lastLogger.chindings + '}')
|
|
this.lastLogger[parsed] = chindings
|
|
}
|
|
|
|
const obj = Object.assign({
|
|
level: this.lastLevel,
|
|
msg: this.lastMsg,
|
|
time: this.lastTime
|
|
}, chindings, this.lastObj)
|
|
|
|
const formatted = pretty(obj)
|
|
dest.write(formatted)
|
|
}
|
|
}
|
|
}
|
|
|
|
function defineLevelsProperty (onObject) {
|
|
Object.defineProperty(onObject, 'levels', {
|
|
value: {
|
|
values: copy({}, levels.levels),
|
|
labels: copy({}, levels.nums)
|
|
},
|
|
enumerable: true
|
|
})
|
|
Object.defineProperty(onObject.levels.values, 'silent', {value: Infinity})
|
|
Object.defineProperty(onObject.levels.labels, Infinity, {value: 'silent'})
|
|
}
|
|
|
|
function streamIsBlockable (s) {
|
|
if (s.hasOwnProperty('_handle') && s._handle.hasOwnProperty('fd') && s._handle.fd) return true
|
|
if (s.hasOwnProperty('fd') && s.fd) return true
|
|
return false
|
|
}
|
|
|
|
function countInterp (s, i) {
|
|
var n = 0
|
|
var pos = 0
|
|
while (true) {
|
|
pos = s.indexOf(i, pos)
|
|
if (pos >= 0) {
|
|
++n
|
|
pos += 2
|
|
} else break
|
|
}
|
|
return n
|
|
}
|
|
|
|
function genLog (z) {
|
|
return function LOG (a, b, c, d, e, f, g, h, i, j, k) {
|
|
var l = 0
|
|
var m = null
|
|
var n = null
|
|
var o
|
|
var p
|
|
if (typeof a === 'object' && a !== null) {
|
|
m = a
|
|
n = [b, c, d, e, f, g, h, i, j, k]
|
|
l = 1
|
|
|
|
if (m.method && m.headers && m.socket) {
|
|
m = serializers.mapHttpRequest(m)
|
|
} else if (typeof m.setHeader === 'function') {
|
|
m = serializers.mapHttpResponse(m)
|
|
}
|
|
} else {
|
|
n = [a, b, c, d, e, f, g, h, i, j, k]
|
|
}
|
|
p = n.length = arguments.length - l
|
|
if (p > 1) {
|
|
l = typeof a === 'string' ? countInterp(a, '%j') : 0
|
|
if (l) {
|
|
n.length = l + countInterp(a, '%d') + countInterp(a, '%s') + 1
|
|
o = `${util.format.apply(null, n)}`
|
|
} else {
|
|
o = format(n, this.formatOpts)
|
|
}
|
|
} else if (p === 1) {
|
|
o = n[0]
|
|
}
|
|
this.write(m, o, z)
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
noop: noop,
|
|
copy: copy,
|
|
applyOptions: applyOptions,
|
|
asMetaWrapper: asMetaWrapper,
|
|
defineLevelsProperty: defineLevelsProperty,
|
|
streamIsBlockable: streamIsBlockable,
|
|
genLog: genLog
|
|
}
|