Define explicit adapter level methods for mypy

bi_streaming_no_debugger_stuff
Tyler Goodlet 2021-07-08 12:51:35 -04:00
parent fde52d2464
commit 25779d48a8
1 changed files with 35 additions and 8 deletions

View File

@ -26,14 +26,15 @@ LOG_FORMAT = (
" {thin_white}{filename}{log_color}:{reset}{thin_white}{lineno}{log_color}" " {thin_white}{filename}{log_color}:{reset}{thin_white}{lineno}{log_color}"
" {reset}{bold_white}{thin_white}{message}" " {reset}{bold_white}{thin_white}{message}"
) )
DATE_FORMAT = '%b %d %H:%M:%S' DATE_FORMAT = '%b %d %H:%M:%S'
LEVELS = { LEVELS = {
'GARBAGE': 1,
'TRANSPORT': 5, 'TRANSPORT': 5,
'RUNTIME': 15, 'RUNTIME': 15,
'PDB': 500, 'PDB': 500,
'QUIET': 1000,
} }
STD_PALETTE = { STD_PALETTE = {
'CRITICAL': 'red', 'CRITICAL': 'red',
'ERROR': 'red', 'ERROR': 'red',
@ -43,19 +44,44 @@ STD_PALETTE = {
'RUNTIME': 'white', 'RUNTIME': 'white',
'DEBUG': 'white', 'DEBUG': 'white',
'TRANSPORT': 'cyan', 'TRANSPORT': 'cyan',
'GARBAGE': 'blue',
} }
BOLD_PALETTE = { BOLD_PALETTE = {
'bold': { 'bold': {
level: f"bold_{color}" for level, color in STD_PALETTE.items()} level: f"bold_{color}" for level, color in STD_PALETTE.items()}
} }
class StackLevelAdapter(logging.LoggerAdapter):
def transport(
self,
msg: str,
) -> None:
return self.log(5, msg)
def runtime(
self,
msg: str,
) -> None:
return self.log(15, msg)
def pdb(
self,
msg: str,
) -> None:
return self.log(500, msg)
def get_logger( def get_logger(
name: str = None, name: str = None,
_root_name: str = _proj_name, _root_name: str = _proj_name,
) -> logging.LoggerAdapter:
'''Return the package log or a sub-log for `name` if provided. ) -> StackLevelAdapter:
'''Return the package log or a sub-logger for ``name`` if provided.
''' '''
log = rlog = logging.getLogger(_root_name) log = rlog = logging.getLogger(_root_name)
@ -72,13 +98,14 @@ def get_logger(
# add our actor-task aware adapter which will dynamically look up # add our actor-task aware adapter which will dynamically look up
# the actor and task names at each log emit # the actor and task names at each log emit
logger = logging.LoggerAdapter(log, ActorContextInfo()) logger = StackLevelAdapter(log, ActorContextInfo())
# additional levels # additional levels
for name, val in LEVELS.items(): for name, val in LEVELS.items():
logging.addLevelName(val, name) logging.addLevelName(val, name)
# ex. create ``logger.runtime()``
setattr(logger, name.lower(), partial(logger.log, val)) # ensure customs levels exist as methods
assert getattr(logger, name.lower()), f'Logger does not define {name}'
return logger return logger