Skip to content

Commit

Permalink
parital implementation of child level independence
Browse files Browse the repository at this point in the history
  • Loading branch information
NicoVogel committed Jun 2, 2023
1 parent c37c082 commit 437419c
Showing 1 changed file with 40 additions and 22 deletions.
62 changes: 40 additions & 22 deletions browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ const stdSerializers = {
err: asErrValue,
errWithCause: asErrValue
}
function levelToValue (level, logger) {
return level === 'silent'
? Infinity
: logger.levels.values[level]
}
const logRefSymbol = Symbol('pino.logRef')

function shouldSerialize (serialize, serializers) {
if (Array.isArray(serialize)) {
Expand Down Expand Up @@ -60,6 +66,7 @@ function pino (opts) {
const level = opts.level || 'info'
const logger = Object.create(proto)
if (!logger.log) logger.log = noop
logger[logRefSymbol] = new Map()

Object.defineProperty(logger, 'levelVal', {
get: getLevelVal
Expand Down Expand Up @@ -94,9 +101,7 @@ function pino (opts) {
if (transmit) logger._logEvent = createLogEventShape()

function getLevelVal () {
return this.level === 'silent'
? Infinity
: this.levels.values[this.level]
return levelToValue(this.level, this)
}

function getLevel () {
Expand All @@ -108,15 +113,15 @@ function pino (opts) {
}
this._level = level

set(setOpts, logger, 'error', 'log') // <-- must stay first
set(setOpts, logger, 'fatal', 'error')
set(setOpts, logger, 'warn', 'error')
set(setOpts, logger, 'info', 'log')
set(setOpts, logger, 'debug', 'log')
set(setOpts, logger, 'trace', 'log')
set(this, setOpts, logger, 'error', 'log') // <-- must stay first
set(this, setOpts, logger, 'fatal', 'error')
set(this, setOpts, logger, 'warn', 'error')
set(this, setOpts, logger, 'info', 'log')
set(this, setOpts, logger, 'debug', 'log')
set(this, setOpts, logger, 'trace', 'log')

customLevels.forEach(function (level) {
set(setOpts, logger, level, 'log')
customLevels.forEach((level) => {
set(this, setOpts, logger, level, 'log')
})
}

Expand Down Expand Up @@ -145,6 +150,9 @@ function pino (opts) {
this.info = bind(parent, bindings, 'info')
this.debug = bind(parent, bindings, 'debug')
this.trace = bind(parent, bindings, 'trace')

this.level = parent.level

if (childSerializers) {
this.serializers = childSerializers
this._serialize = childSerialize
Expand Down Expand Up @@ -203,19 +211,27 @@ pino.levels = {
pino.stdSerializers = stdSerializers
pino.stdTimeFunctions = Object.assign({}, { nullTime, epochTime, unixTime, isoTime })

function set (opts, logger, level, fallback) {
function set (self, opts, logger, level, fallback) {
const proto = Object.getPrototypeOf(logger)
logger[level] = logger.levelVal > logger.levels.values[level]

const ref = logger[logRefSymbol].get(self) ?? {}
ref[level] = levelToValue(self.level, logger) > levelToValue(level, logger)
? noop
: (proto[level] ? proto[level] : (_console[level] || _console[fallback] || noop))
logger[logRefSymbol].set(self, ref)

wrap(ref, self, opts, logger, level)

wrap(opts, logger, level)
// must happen after `wrap` to ensure that the logger function is correct
if (self === logger) {
logger[level] = ref[level]
}
}

function wrap (opts, logger, level) {
if (!opts.transmit && logger[level] === noop) return
function wrap (ref, self, opts, logger, level) {
if (!opts.transmit && ref[level] === noop) return

logger[level] = (function (write) {
ref[level] = (function (write) {
return function LOG () {
const ts = opts.timestamp()
const args = new Array(arguments.length)
Expand All @@ -229,7 +245,7 @@ function wrap (opts, logger, level) {
else write.apply(proto, args)

if (opts.transmit) {
const transmitLevel = opts.transmit.level || logger.level
const transmitLevel = opts.transmit.level || self._level
const transmitValue = logger.levels.values[transmitLevel]
const methodValue = logger.levels.values[level]
if (methodValue < transmitValue) return
Expand All @@ -238,13 +254,13 @@ function wrap (opts, logger, level) {
methodLevel: level,
methodValue,
transmitLevel,
transmitValue: logger.levels.values[opts.transmit.level || logger.level],
transmitValue: logger.levels.values[opts.transmit.level || self._level],
send: opts.transmit.send,
val: logger.levelVal
val: levelToValue(self._level, logger)
}, args)
}
}
})(logger[level])
})(ref[level])
}

function asObject (logger, level, args, ts) {
Expand Down Expand Up @@ -290,7 +306,9 @@ function bind (parent, bindings, level) {
for (var i = 1; i < args.length; i++) {
args[i] = arguments[i - 1]
}
return parent[level].apply(this, args)
// TODO: find a way to not break the binding argument chain
const ref = parent[logRefSymbol].get(this)
return ref[level].apply(this, args)
}
}

Expand Down

0 comments on commit 437419c

Please sign in to comment.