Bug 1011432 - Move structured log stdlogging adapter to its own module, r=ahal

The previous design did not work well with multiprocessing on
Windows since it didn't pickle correctly
This commit is contained in:
James Graham 2014-05-21 16:36:38 +01:00
parent da8ffec5c2
commit e01c21986e
3 changed files with 42 additions and 51 deletions

View File

@ -0,0 +1,40 @@
import logging
from structuredlog import StructuredLogger, log_levels
class UnstructuredHandler(logging.Handler):
def __init__(self, name=None, level=logging.NOTSET):
self.structured = StructuredLogger(name)
logging.Handler.__init__(self, level=level)
def emit(self, record):
if record.levelname in log_levels:
log_func = getattr(self.structured, record.levelname.lower())
else:
log_func = self.logger.debug
log_func(record.msg)
def handle(self, record):
self.emit(record)
class LoggingWrapper(object):
def __init__(self, wrapped):
self.wrapped = wrapped
self.wrapped.addHandler(UnstructuredHandler(self.wrapped.name,
logging.getLevelName(self.wrapped.level)))
def add_handler(self, handler):
self.addHandler(handler)
def remove_handler(self, handler):
self.removeHandler(handler)
def __getattr__(self, name):
return getattr(self.wrapped, name)
def std_logging_adapter(logger):
"""Adapter for stdlib logging so that it produces structured
messages rather than standard logging messages
:param logger: logging.Logger to wrap"""
return LoggingWrapper(logger)

View File

@ -234,53 +234,3 @@ class StructuredLogFileLike(object):
def flush(self):
pass
_wrapper_cls = None
def std_logging_adapter(logger):
"""
Adapter for stdlib logging so that it produces structured
messages rather than standard logging messages
:param logger: logging.Logger to wrap
"""
global _wrapper_cls
import logging
if _wrapper_cls is not None:
return _wrapper_cls(logger)
class UnstructuredHandler(logging.Handler):
def __init__(self, name=None, level=logging.NOTSET):
self.structured = StructuredLogger(name)
logging.Handler.__init__(self, level=level)
def emit(self, record):
if record.levelname in log_levels:
log_func = getattr(self.structured, record.levelname.lower())
else:
log_func = self.logger.debug
log_func(record.msg)
def handle(self, record):
self.emit(record)
class LoggingWrapper(object):
def __init__(self, wrapped):
self.wrapped = wrapped
self.wrapped.addHandler(UnstructuredHandler(self.wrapped.name,
logging.getLevelName(self.wrapped.level)))
def add_handler(self, handler):
self.addHandler(handler)
def remove_handler(self, handler):
self.removeHandler(handler)
def __getattr__(self, name):
return getattr(self.wrapped, name)
_wrapper_cls = LoggingWrapper
return LoggingWrapper(logger)

View File

@ -9,6 +9,7 @@ from mozlog.structured import (
commandline,
reader,
structuredlog,
stdadapter,
)
@ -124,7 +125,7 @@ class TestStructuredLog(BaseStructuredTest):
std_logger = logging.getLogger("test")
std_logger.setLevel("DEBUG")
logger = structuredlog.std_logging_adapter(std_logger)
logger = stdadapter.std_logging_adapter(std_logger)
try:
for level in ["critical", "error", "warning", "info", "debug"]: