1
0
mirror of https://github.com/EDCD/EDMarketConnector.git synced 2025-06-12 13:22:54 +03:00

Fix up and expand on docstrings.

This commit is contained in:
Athanasius 2020-07-30 23:59:10 +01:00
parent 657253b3e3
commit 282e3ddbc5

View File

@ -1,4 +1,6 @@
""" """
Set up required logging for the application.
This module provides for a common logging-powered log facility. This module provides for a common logging-powered log facility.
Mostly it implements a logging.Filter() in order to get two extra Mostly it implements a logging.Filter() in order to get two extra
members on the logging.LogRecord instance for use in logging.Formatter() members on the logging.LogRecord instance for use in logging.Formatter()
@ -51,9 +53,11 @@ class Logger:
Users of this class should then call getLogger() to get the Users of this class should then call getLogger() to get the
logging.Logger instance. logging.Logger instance.
""" """
def __init__(self, logger_name: str, loglevel: int = _default_loglevel): def __init__(self, logger_name: str, loglevel: int = _default_loglevel):
""" """
Set up a `logging.Logger` with our preferred configuration. Set up a `logging.Logger` with our preferred configuration.
This includes using an EDMCContextFilter to add 'class' and 'qualname' This includes using an EDMCContextFilter to add 'class' and 'qualname'
expansions for logging.Formatter(). expansions for logging.Formatter().
""" """
@ -77,13 +81,17 @@ class Logger:
def get_logger(self) -> logging.Logger: def get_logger(self) -> logging.Logger:
""" """
:return: The logging.Logger instance. Obtain the self.logger of the class instance.
Not to be confused with logging.getLogger().
""" """
return self.logger return self.logger
def get_plugin_logger(name: str, loglevel: int = _default_loglevel) -> logging.Logger: def get_plugin_logger(name: str, loglevel: int = _default_loglevel) -> logging.Logger:
""" """
Return a logger suitable for a plugin.
'Found' plugins need their own logger to call out where the logging is 'Found' plugins need their own logger to call out where the logging is
coming from, but we don't need to set up *everything* for them. coming from, but we don't need to set up *everything* for them.
@ -111,12 +119,15 @@ def get_plugin_logger(name: str, loglevel: int = _default_loglevel) -> logging.L
class EDMCContextFilter(logging.Filter): class EDMCContextFilter(logging.Filter):
""" """
Implements filtering to add extra format specifiers, and tweak others.
logging.Filter sub-class to place extra attributes of the calling site logging.Filter sub-class to place extra attributes of the calling site
into the record. into the record.
""" """
def filter(self, record: logging.LogRecord) -> bool: def filter(self, record: logging.LogRecord) -> bool:
""" """
Attempt to set the following in the LogRecord: Attempt to set/change fields in the LogRecord.
1. class = class name(s) of the call site, if applicable 1. class = class name(s) of the call site, if applicable
2. qualname = __qualname__ of the call site. This simplifies 2. qualname = __qualname__ of the call site. This simplifies
@ -150,13 +161,15 @@ class EDMCContextFilter(logging.Filter):
return True return True
@classmethod @classmethod
def caller_attributes(cls, module_name: str = ''): def caller_attributes(cls, module_name: str = '') -> Tuple[str, str, str]:
""" """
Figure out our caller's class name(s) and qualname Determine extra or changed fields for the caller.
Ref: <https://gist.github.com/techtonik/2151726#gistcomment-2333747> 1. qualname finds the relevant object and its __qualname__
2. caller_class_names is just the full class names of the calling
:return: Tuple[str, str, str]: The caller's class name(s), qualname and module_name class if relevant.
3. module is munged if we detect the caller is an EDMC plugin,
whether internal or found.
""" """
# Go up through stack frames until we find the first with a # Go up through stack frames until we find the first with a
# type(f_locals.self) of logging.Logger. This should be the start # type(f_locals.self) of logging.Logger. This should be the start
@ -206,8 +219,7 @@ class EDMCContextFilter(logging.Filter):
################################################################### ###################################################################
# Fixups for EDMC plugins # Fixups for EDMC plugins
################################################################### ###################################################################
# TODO: This assumes caller is only one level into the plugin folder # TODO: Refactor this bit out into another function
# Need to walk back up checking.
# Is this a 'found' plugin calling us? # Is this a 'found' plugin calling us?
file_name = pathlib.Path(frame_info.filename).expanduser() file_name = pathlib.Path(frame_info.filename).expanduser()
plugin_dir = pathlib.Path(config.plugin_dir).expanduser() plugin_dir = pathlib.Path(config.plugin_dir).expanduser()