Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
pR0Ps committed Feb 2, 2024
1 parent 165f095 commit 846f374
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions bracelogger.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@
from collections import Mapping


_class_cache = {}
_BLPROXY_LOCAL = {
"_log", "_wrapped", "log", "makeRecord", "getChild",
"debug", "info", "warn", "warning", "error", "exception", "critical", "fatal",
}


_class_cache = {}
def _make_class(name, mixin, type_):
"""Dynamically create a class or pull it from the cache"""
if type_ not in _class_cache:
_class_cache[type_] = type(name, (mixin,) + type_.__mro__, {})
return _class_cache[type_]
Expand Down Expand Up @@ -42,15 +48,14 @@ def getMessage(record):


class BraceloggerMixin(object):
"""A logger wrapper that causes all handled LogRecords to use brace-style formatting"""

__log_functions__ = {"debug", "info", "warn", "warning", "error", "exception", "critical", "fatal"}
"""A proxy to a logging.Logger instance that causes all handled LogRecords
to use brace-style message formatting"""

def __init__(self, logger):
self._wrapped = logger

def __getattribute__(self, key):
if key in type(self).__log_functions__ or key in {"_log", "_wrapped", "log", "handle", "getChild"}:
if key in _BLPROXY_LOCAL:
return super(BraceloggerMixin, self).__getattribute__(key)
return getattr(self._wrapped, key)

Expand All @@ -62,9 +67,11 @@ def __setattr__(self, key, val):
def __delattr__(self, key):
return delattr(self._wrapped, key)

def handle(self, record):
def makeRecord(self, *args, **kwargs):
"""Create a BracelogRecord"""
record = self._wrapped.makeRecord(*args, **kwargs)
record.__class__ = _make_class("BracelogRecord", BracelogRecordMixin, type(record))
return super(BraceloggerMixin, self).handle(record)
return record

def getChild(self, suffix):
"""Get a Bracelogger which is a descendant to this one"""
Expand All @@ -74,4 +81,6 @@ def getChild(self, suffix):

def get_logger(name=None):
logger = logging.getLogger(name)

# Create a new subclass of the existing logger class so introspection works
return _make_class("Bracelogger", BraceloggerMixin, type(logger))(logger)

0 comments on commit 846f374

Please sign in to comment.