From 5554d7f7ecb5532f64addd9212aa9228bc82c53d Mon Sep 17 00:00:00 2001 From: Andrew Pikul Date: Wed, 18 Dec 2024 10:19:20 -0500 Subject: [PATCH] Better format code for commenting --- logistro/__init__.py | 30 ++++++++++++++-------------- logistro/custom_logging.py | 41 +++++++++++++++++++------------------- 2 files changed, 36 insertions(+), 35 deletions(-) diff --git a/logistro/__init__.py b/logistro/__init__.py index 242d375..9f4b9b7 100644 --- a/logistro/__init__.py +++ b/logistro/__init__.py @@ -1,21 +1,21 @@ -from .custom_logging import betterConfig -from .custom_logging import coerce_logger -from .custom_logging import DEBUG2 -from .custom_logging import getLogger -from .custom_logging import getPipeLogger -from .custom_logging import human_formatter -from .custom_logging import set_human -from .custom_logging import set_structured -from .custom_logging import structured_formatter +from logistro.custom_logging import betterConfig +from logistro.custom_logging import coerce_logger +from logistro.custom_logging import DEBUG2 +from logistro.custom_logging import getLogger +from logistro.custom_logging import getPipeLogger +from logistro.custom_logging import human_formatter +from logistro.custom_logging import set_human +from logistro.custom_logging import set_structured +from logistro.custom_logging import structured_formatter __all__ = [ - coerce_logger, DEBUG2, - human_formatter, - structured_formatter, - set_human, - set_structured, - betterConfig, getLogger, getPipeLogger, + betterConfig, + set_human, + set_structured, + human_formatter, + structured_formatter, + coerce_logger, ] diff --git a/logistro/custom_logging.py b/logistro/custom_logging.py index c552551..2371699 100644 --- a/logistro/custom_logging.py +++ b/logistro/custom_logging.py @@ -9,15 +9,16 @@ ## New Constants and Globals -#: A more verbose debug +#: A more verbose version of logging.DEBUG DEBUG2 = 5 + logging.addLevelName(DEBUG2, "DEBUG2") #: These are useless when logging external process output. See getPipeLogger() pipe_attr_blacklist = ["filename", "funcName", "threadName", "taskName"] # Our basic formatting list -output = { +_output = { "time": "%(asctime)s", "name": "%(name)s", "level": "%(levelname)s", @@ -29,26 +30,26 @@ } # A more readable, human readable string -date_string = "%a, %d-%b %H:%M:%S" +_date_string = "%a, %d-%b %H:%M:%S" # async taskName not supported below 3.12, remove it if bool(sys.version_info[:3] < (3, 12)): - del output["task"] + del _output["task"] # make human output a little more readable -output_human = output.copy() -output_human["func"] += "()" +_output_human = _output.copy() +_output_human["func"] += "()" # generate format string human_formatter = logging.Formatter( - ":".join(output_human.values()), - datefmt=date_string, + ":".join(_output_human.values()), + datefmt=_date_string, ) -structured_formatter = logging.Formatter(json.dumps(output)) +structured_formatter = logging.Formatter(json.dumps(_output)) # We set this as the logging class just to add a debug1 and debug2 function -class LogistroLogger(logging.getLoggerClass()): +class _LogistroLogger(logging.getLoggerClass()): def debug1(self, msg, *args, **kwargs): super().log(logging.DEBUG, msg, *args, stacklevel=2, **kwargs) @@ -56,7 +57,7 @@ def debug2(self, msg, *args, **kwargs): super().log(DEBUG2, msg, *args, stacklevel=2, **kwargs) -logging.setLoggerClass(LogistroLogger) +logging.setLoggerClass(_LogistroLogger) def set_human(): @@ -89,17 +90,17 @@ def coerce_logger(logger, formatter=None): handler.setFormatter(formatter) -def run_once(f): - def wrapper(*args, **kwargs): - if not wrapper.has_run: - wrapper.has_run = True +def _run_once(f): + def betterConfig(*args, **kwargs): + if not betterConfig.has_run: + betterConfig.has_run = True return f(*args, **kwargs) - wrapper.has_run = False - return wrapper + betterConfig.has_run = False + return betterConfig -@run_once +@_run_once def betterConfig(**kwargs): """betterConfig is a wrapper over logging.basicConfig which provides our defaults (level set by --logistro-level and formatter @@ -121,7 +122,7 @@ def getLogger(name=None): return logger -class PipeLoggerFilter: +class _PipeLoggerFilter: def __init__(self, parser): self._parser = parser @@ -162,7 +163,7 @@ def getPipeLogger( """ IFS = IFS.encode("utf-8") logger = getLogger(name) - logger.addFilter(PipeLoggerFilter(parser)) + logger.addFilter(_PipeLoggerFilter(parser)) r, w = os.pipe() # needs r, logger, and default_level