function pino(opts, stream) { var iopts = opts var istream = stream if (iopts && (iopts.writable || iopts._writableState)) { istream = iopts iopts = defaultOptions } iopts = Object.assign({}, defaultOptions, iopts) if (iopts.extreme && iopts.prettyPrint) throw Error('cannot enable pretty print in extreme mode') istream = istream || process.stdout if (iopts.prettyPrint) { var prettyOpts = Object.assign({ messageKey: iopts.messageKey }, iopts.prettyPrint) var pstream = pretty(prettyOpts) var origStream = istream pump(pstream, origStream, function (err) { if (err) instance.emit('error', err) }) istream = pstream } // internal options iopts.stringify = iopts.safe ? stringifySafe : JSON.stringify iopts.formatOpts = {lowres: true} iopts.messageKeyString = `,"${iopts.messageKey}":` iopts.end = ',"v":' + LOG_VERSION + '}\n' iopts.cache = !iopts.extreme ? null : { size: 4096, buf: '' } iopts.chindings = '' if (iopts.enabled === false) { iopts.level = 'silent' } var instance = Object.create(pinoPrototype) instance.stream = istream tools.defineLevelsProperty(instance) tools.applyOptions.call(instance, iopts) if (iopts.cache) setTimeout(waitForFDSettle, 100) var settleTries = 0 function waitForFDSettle () { var isBlockable = tools.streamIsBlockable(istream) if (isBlockable === false && settleTries > 10) { return instance.emit('error', Error('stream must have a file descriptor in extreme mode')) } else if (isBlockable === true) { return events(instance, extremeModeExitHandler) } settleTries += 1 setTimeout(waitForFDSettle, 100) } function extremeModeExitHandler () { var buf = iopts.cache.buf if (buf) { // We need to block the process exit long enough to flush the buffer // to the destination stream. We do that by forcing a synchronous // write directly to the stream's file descriptor. var fd = (istream.fd) ? istream.fd : istream._handle.fd fs.writeSync(fd, buf) } } return instance }
n/a
function pretty(opts) { var timeTransOnly = opts && opts.timeTransOnly var formatter = opts && opts.formatter var levelFirst = opts && opts.levelFirst var messageKey = opts && opts.messageKey messageKey = messageKey || 'msg' var stream = split(mapLine) var ctx var levelColors var pipe = stream.pipe stream.pipe = function (dest, opts) { ctx = new chalk.constructor({ enabled: !!(chalk.supportsColor && dest.isTTY) }) levelColors = { default: ctx.white, 60: ctx.bgRed, 50: ctx.red, 40: ctx.yellow, 30: ctx.green, 20: ctx.blue, 10: ctx.grey } pipe.call(stream, dest, opts) } return stream function mapLine (line) { var parsed = new Parse(line) var value = parsed.value if (parsed.err || !isPinoLine(value)) { // pass through return line + '\n' } if (formatter) { return opts.formatter(parsed.value) + '\n' } if (timeTransOnly) { value.time = asISODate(value.time) return JSON.stringify(value) + '\n' } line = (levelFirst) ? asColoredLevel(value) + ' [' + asISODate(value.time) + ']' : '[' + asISODate(value.time) + '] ' + asColoredLevel(value) line += ' (' if (value.name) { line += value.name + '/' } line += value.pid + ' on ' + value.hostname + ')' line += ': ' if (value[messageKey]) { line += ctx.cyan(value[messageKey]) } line += '\n' if (value.type === 'Error') { line += ' ' + withSpaces(value.stack) + '\n' } else { line += filter(value, messageKey) } return line } function asISODate (time) { return new Date(time).toISOString() } function asColoredLevel (value) { if (levelColors.hasOwnProperty(value.level)) { return levelColors[value.level](levels[value.level]) } else { return levelColors.default(levels.default) } } }
n/a
isStandardLevel = function (level) { if (Infinity === level) { return true } return keys.indexOf(level) > -1 }
...
this._levelVal = num
for (var key in this.levels.values) {
if (num > this.levels.values[key]) {
this[key] = tools.noop
continue
}
this[key] = levels.isStandardLevel(key) ? pinoPrototype[key] : tools.genLog(this.
levels.values[key])
}
}
Object.defineProperty(pinoPrototype, 'levelVal', {
get: getLevelVal,
set: setLevelVal
})
...
isStandardLevelVal = function (val) { if (!isFinite(val)) { return true } return keys.indexOf(val + '') > -1 }
...
data += '"' + key + '":' + this.stringify(value) + ','
}
}
data = this.chindings + data.substr(0, data.length - 1)
var opts = {
level: bindings.level || this.level,
levelVal: levels.isStandardLevelVal(this.levelVal) ? undefined : this.levelVal,
serializers: bindings.hasOwnProperty('serializers') ? Object.assign({}, this.serializers, bindings.serializers) : this
.serializers,
stringify: this.stringify,
end: this.end,
name: this.name,
timestamp: this.timestamp,
slowtime: this.slowtime,
chindings: data,
...
function asErrValue(err) { var obj = { type: err.constructor.name, message: err.message, stack: err.stack } for (var key in err) { if (obj[key] === undefined) { obj[key] = err[key] } } return obj }
n/a
function asReqValue(req) { return { method: req.method, url: req.url, headers: req.headers, remoteAddress: req.connection.remoteAddress, remotePort: req.connection.remotePort } }
n/a
function asResValue(res) { return { statusCode: res.statusCode, header: res._header } }
n/a
function mapHttpRequest(req) { return { req: asReqValue(req) } }
...
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
...
function mapHttpResponse(res) { return { res: asResValue(res) } }
...
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
...
function asErrValue(err) { var obj = { type: err.constructor.name, message: err.message, stack: err.stack } for (var key in err) { if (obj[key] === undefined) { obj[key] = err[key] } } return obj }
n/a
function asReqValue(req) { return { method: req.method, url: req.url, headers: req.headers, remoteAddress: req.connection.remoteAddress, remotePort: req.connection.remotePort } }
n/a
function asResValue(res) { return { statusCode: res.statusCode, header: res._header } }
n/a
function getNoTime() { return '' }
n/a
function getSlowTime() { return ',"time":"' + (new Date()).toISOString() + '"' }
n/a
function getTime() { return ',"time":' + Date.now() }
n/a
function applyOptions(opts) { this.serializers = opts.serializers this.stringify = opts.stringify this.end = opts.end this.name = opts.name this.timestamp = opts.timestamp this.slowtime = opts.slowtime this.chindings = opts.chindings this.cache = opts.cache this.formatOpts = opts.formatOpts this.onTerminated = opts.onTerminated this.messageKey = opts.messageKey this.messageKeyString = opts.messageKeyString if (opts.level && opts.levelVal) { var levelIsStandard = levels.isStandardLevel(opts.level) var valIsStandard = levels.isStandardLevelVal(opts.levelVal) if (valIsStandard) throw Error('level value is already used: ' + opts.levelVal) if (levelIsStandard === false && valIsStandard === false) this.addLevel(opts.level, opts.levelVal) } this._setLevel(opts.level) this._baseLog = flatstr(baseLog + (this.name === undefined ? '' : '"name":' + this.stringify(this.name) + ',')) if (opts.timestamp === false) { this.time = time.getNoTime } else if (opts.slowtime) { this.time = time.getSlowTime } else { this.time = time.getTime } }
n/a
function copy(a, b) { for (var k in b) { a[k] = b[k] } return a }
...
Object.defineProperty(pinoPrototype, 'level', {
get: pinoPrototype._getLevel,
set: pinoPrototype._setLevel
})
Object.defineProperty(pinoPrototype, '_lscache', {
value: tools.copy({}, levels.lscache)
})
Object.defineProperty(
pinoPrototype,
'LOG_VERSION',
{value: LOG_VERSION}
)
...
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'}) }
...
if (iopts.enabled === false) {
iopts.level = 'silent'
}
var instance = Object.create(pinoPrototype)
instance.stream = istream
tools.defineLevelsProperty(instance)
tools.applyOptions.call(instance, iopts)
if (iopts.cache) setTimeout(waitForFDSettle, 100)
var settleTries = 0
function waitForFDSettle () {
var isBlockable = tools.streamIsBlockable(istream)
if (isBlockable === false && settleTries > 10) {
...
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) { o = n[0] } this.write(m, o, z) } }
...
enumerable: true
}
})
var levelMethods = ['fatal', 'error', 'warn', 'info', 'debug', 'trace'
;]
levelMethods.forEach(function (m) {
Object.defineProperty(pinoPrototype, m, {
value: tools.genLog(levels.levels[m]),
enumerable: true,
writable: true
})
})
function getLevelVal () {
return this._levelVal
...
function noop() {}
n/a
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 }
...
instance.stream = istream
tools.defineLevelsProperty(instance)
tools.applyOptions.call(instance, iopts)
if (iopts.cache) setTimeout(waitForFDSettle, 100)
var settleTries = 0
function waitForFDSettle () {
var isBlockable = tools.streamIsBlockable(istream)
if (isBlockable === false && settleTries > 10) {
return instance.emit('error', Error('stream must have a file descriptor in extreme mode'))
} else if (isBlockable === true) {
return events(instance, extremeModeExitHandler)
}
settleTries += 1
setTimeout(waitForFDSettle, 100)
...