Source code

Revision control

Copy as Markdown

Other Tools

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at
from __future__ import absolute_import
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())
log_func = self.logger.debug
def handle(self, record):
class LoggingWrapper(object):
def __init__(self, wrapped):
self.wrapped = wrapped
UnstructuredHandler(, logging.getLevelName(self.wrapped.level)
def add_handler(self, handler):
def remove_handler(self, 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)