master: allow filtering forwarded logs using logging package functions.

Given a message sent on "ssh.foo" to "mypkg.mymod", instead of logging
it to "mitogen.ctx.ssh.foo" in the master process, with the message
prefixed with the original logger name, instead log it to
"mypkg.mymod.[ssh.foo]", permitting normal logging package filtering
features to work as they usually do.

This also helps tidy up logging output a little bit.
pull/612/head
David Wilson 5 years ago
parent feb1654305
commit 20532ea591

@ -387,26 +387,26 @@ class LogForwarder(object):
if msg.is_dead: if msg.is_dead:
return return
logger = self._cache.get(msg.src_id)
if logger is None:
context = self._router.context_by_id(msg.src_id) context = self._router.context_by_id(msg.src_id)
if context is None: if context is None:
LOG.error('%s: dropping log from unknown context ID %d', LOG.error('%s: dropping log from unknown context %d',
self, msg.src_id) self, msg.src_id)
return return
name = '%s.%s' % (RLOG.name, context.name)
self._cache[msg.src_id] = logger = logging.getLogger(name)
name, level_s, s = msg.data.decode('utf-8', 'replace').split('\x00', 2) name, level_s, s = msg.data.decode('utf-8', 'replace').split('\x00', 2)
logger_name = '%s.[%s]' % (name, context.name)
logger = self._cache.get(logger_name)
if logger is None:
self._cache[logger_name] = logger = logging.getLogger(logger_name)
# See logging.Handler.makeRecord() # See logging.Handler.makeRecord()
record = logging.LogRecord( record = logging.LogRecord(
name=logger.name, name=logger.name,
level=int(level_s), level=int(level_s),
pathname='(unknown file)', pathname='(unknown file)',
lineno=0, lineno=0,
msg=('%s: %s' % (name, s)), msg=s,
args=(), args=(),
exc_info=None, exc_info=None,
) )

Loading…
Cancel
Save