Skip to content

Commit

Permalink
Add propagate=False opt to basic_logger()
Browse files Browse the repository at this point in the history
  • Loading branch information
taldcroft committed Feb 4, 2023
1 parent 7733521 commit 435f231
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions ska_helpers/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
__all__ = ["basic_logger"]


def basic_logger(name, format="%(asctime)s %(funcName)s: %(message)s", **kwargs):
def basic_logger(
name,
format="%(asctime)s %(funcName)s: %(message)s",
propagate=False,
**kwargs
):
"""Create logger ``name`` using logging.basicConfig.
This is a thin wrapper around logging.basicConfig, except:
Expand All @@ -16,14 +21,20 @@ def basic_logger(name, format="%(asctime)s %(funcName)s: %(message)s", **kwargs)
It will probably work but it is not guaranteed.
This function does nothing if the ``name`` logger already has handlers
configured, unless the keyword argument *force* is set to ``True``.
configured, unless the keyword argument ``force`` is set to ``True``.
It is a convenience method intended to do one-shot creation of a logger.
The default behaviour is to create a StreamHandler which writes to
``sys.stderr``, set a formatter using the format string ``"%(asctime)s
%(funcName)s: %(message)s"``, and add the handler to the ``name`` logger
with a level of WARNING.
By default the created logger will not propagate to parent loggers. This
is to prevent unexpected logging from other packages that set up a root
logger. To propagate to parent loggers, set ``propagate=True``. See
https://docs.python.org/3/howto/logging.html#logging-flow, in particular
how the log level of parent loggers is ignored in message handling.
Example::
# In __init__.py for a package or in any module
Expand Down Expand Up @@ -72,6 +83,7 @@ def basic_logger(name, format="%(asctime)s %(funcName)s: %(message)s", **kwargs)
:param name: str, logger name
:param format: str, format string for handler
:param propagate: bool, propagate to parent loggers (default=False)
:param **kwargs: other keyword arguments for logging.basicConfig
:returns: Logger object
"""
Expand All @@ -94,4 +106,6 @@ def basic_logger(name, format="%(asctime)s %(funcName)s: %(message)s", **kwargs)
finally:
logging.root = root

logger.propagate = propagate

return logger

0 comments on commit 435f231

Please sign in to comment.