[loggingTools] added LogMixin class
This is useful to quickly add logging functionality to classes, and to reduce boilerplate. It adds a 'log' property to the class inheriting from it, which uses logging.getLogger to get a logging.Logger (sigleton) object named after <module>.<class> of self.
This commit is contained in:
parent
d8a9ca6db4
commit
cca1d20b0e
@ -471,6 +471,18 @@ class CapturingLogHandler(logging.Handler):
|
|||||||
return True
|
return True
|
||||||
assert 0, "Pattern '%s' not found in logger records" % regexp
|
assert 0, "Pattern '%s' not found in logger records" % regexp
|
||||||
|
|
||||||
|
|
||||||
|
class LogMixin(object):
|
||||||
|
""" Mixin class that adds a 'log' property getter returning an instance
|
||||||
|
of logging.Logger class, which is named after self <module>.<class> name.
|
||||||
|
"""
|
||||||
|
|
||||||
|
@property
|
||||||
|
def log(self):
|
||||||
|
name = ".".join([self.__class__.__module__, self.__class__.__name__])
|
||||||
|
return logging.getLogger(name)
|
||||||
|
|
||||||
|
|
||||||
def deprecateArgument(name, msg, category=UserWarning):
|
def deprecateArgument(name, msg, category=UserWarning):
|
||||||
""" Raise a warning about deprecated function argument 'name'. """
|
""" Raise a warning about deprecated function argument 'name'. """
|
||||||
warnings.warn(
|
warnings.warn(
|
||||||
|
@ -7,19 +7,15 @@ code, as well as the 'extra' programs 'fpgm', 'ppgm' (i.e. 'prep'), and 'cvt'.
|
|||||||
from __future__ import print_function, division, absolute_import
|
from __future__ import print_function, division, absolute_import
|
||||||
from fontTools.misc.py23 import *
|
from fontTools.misc.py23 import *
|
||||||
from . import DefaultTable
|
from . import DefaultTable
|
||||||
import logging
|
from fontTools.misc.loggingTools import LogMixin
|
||||||
|
|
||||||
|
|
||||||
class table_T_S_I__1(DefaultTable.DefaultTable):
|
class table_T_S_I__1(LogMixin, DefaultTable.DefaultTable):
|
||||||
|
|
||||||
extras = {0xfffa: "ppgm", 0xfffb: "cvt", 0xfffc: "reserved", 0xfffd: "fpgm"}
|
extras = {0xfffa: "ppgm", 0xfffb: "cvt", 0xfffc: "reserved", 0xfffd: "fpgm"}
|
||||||
|
|
||||||
indextable = "TSI0"
|
indextable = "TSI0"
|
||||||
|
|
||||||
def __init__(self, tag=None):
|
|
||||||
super(table_T_S_I__1, self).__init__(tag)
|
|
||||||
self.log = logging.getLogger(self.__class__.__module__)
|
|
||||||
|
|
||||||
def decompile(self, data, ttFont):
|
def decompile(self, data, ttFont):
|
||||||
totalLength = len(data)
|
totalLength = len(data)
|
||||||
indextable = ttFont[self.indextable]
|
indextable = ttFont[self.indextable]
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
from __future__ import print_function, division, absolute_import
|
from __future__ import print_function, division, absolute_import
|
||||||
from fontTools.misc.py23 import *
|
from fontTools.misc.py23 import *
|
||||||
from fontTools.misc.loggingTools import (
|
from fontTools.misc.loggingTools import (
|
||||||
LevelFormatter, Timer, configLogger, ChannelsFilter)
|
LevelFormatter, Timer, configLogger, ChannelsFilter, LogMixin)
|
||||||
import logging
|
import logging
|
||||||
import textwrap
|
import textwrap
|
||||||
import time
|
import time
|
||||||
@ -148,3 +148,25 @@ def test_ChannelsFilter(logger):
|
|||||||
|
|
||||||
logging.getLogger(n+'.C.DE').debug('neither this one!')
|
logging.getLogger(n+'.C.DE').debug('neither this one!')
|
||||||
assert before == stream.getvalue()
|
assert before == stream.getvalue()
|
||||||
|
|
||||||
|
|
||||||
|
def test_LogMixin():
|
||||||
|
|
||||||
|
class Base(object):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class A(LogMixin, Base):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class B(A):
|
||||||
|
pass
|
||||||
|
|
||||||
|
a = A()
|
||||||
|
b = B()
|
||||||
|
|
||||||
|
assert hasattr(a, 'log')
|
||||||
|
assert hasattr(b, 'log')
|
||||||
|
assert isinstance(a.log, logging.Logger)
|
||||||
|
assert isinstance(b.log, logging.Logger)
|
||||||
|
assert a.log.name == "loggingTools_test.A"
|
||||||
|
assert b.log.name == "loggingTools_test.B"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user