cppo-ng/blocksfree/logging.py
T. Joseph Carter 82d851e39a Create blocksfree package for logger
The section of cppo containing the logging code has been moved to its own very
short module inside a (bare) Python package.  This is messily done for now, but
I wanted this to be a minimal commit.
2017-07-07 02:21:42 -07:00

34 lines
903 B
Python

import sys
import logging
### LOGGING
# *sigh* No clean/simple way to use str.format() type log strings without
# jumping through a few hoops
class Message(object):
def __init__(self, fmt, args):
self.fmt = fmt
self.args = args
def __str__(self):
return self.fmt.format(*self.args)
class StyleAdapter(logging.LoggerAdapter):
def __init__(self, logger, extra=None):
super(StyleAdapter, self).__init__(logger, extra or {})
def log(self, level, msg, *args, **kwargs):
if self.isEnabledFor(level):
msg, kwargs = self.process(msg, kwargs)
self.logger._log(level, Message(str(msg), args), (), **kwargs)
log = StyleAdapter(logging.getLogger(__name__))
# Set up our logging facility
_handler = logging.StreamHandler(sys.stdout)
_formatter = logging.Formatter('%(levelname)s: %(message)s')
_handler.setFormatter(_formatter)
log.logger.addHandler(_handler)
log.setLevel(logging.DEBUG)