Skip to content

Commit

Permalink
Wrap logger manipulation around a lock
Browse files Browse the repository at this point in the history
Just doing it as the standard library does, even if I do not have the proof it is the right thing to do
  • Loading branch information
kdeldycke committed Jan 20, 2025
1 parent a76856a commit ccd4591
Showing 1 changed file with 40 additions and 37 deletions.
77 changes: 40 additions & 37 deletions click_extra/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,37 +146,39 @@ def extra_basic_config(
Add more parameters for even greater configurability of the logger, by
re-implementing those supported by ``logging.basicConfig``.
"""
# Fetch the logger or create a new one.
logger = logging.getLogger(logger_name)

# Remove and close any existing handlers. Copy of:
# https://github.com/python/cpython/blob/2b5dbd1/Lib/logging/__init__.py#L2028-L2031
if force:
for h in logger.handlers[:]:
logger.removeHandler(h)
h.close()

# If no handlers provided, create a new one with the default handler class.
if not handlers:
handlers = (handler_class(),)

# Set up the formatter with a default message format.
formatter = formatter_class(
fmt=format,
datefmt=datefmt,
style=style,
)
with logging._lock:
# Fetch the logger or create a new one.
logger = logging.getLogger(logger_name)

# Remove and close any existing handlers. Copy of:
# https://github.com/python/cpython/blob/2b5dbd1/Lib/logging/__init__.py#L2028-L2031
# https://loguru.readthedocs.io/en/stable/resources/recipes.html#avoiding-logs-to-be-printed-twice-on-the-terminal
if force:
for h in logger.handlers[:]:
logger.removeHandler(h)
h.close()

# If no handlers provided, create a new one with the default handler class.
if not handlers:
handlers = (handler_class(),)

# Set up the formatter with a default message format.
formatter = formatter_class(
fmt=format,
datefmt=datefmt,
style=style,
)

# Attach handlers to the loggers.
for h in handlers:
if h.formatter is None:
h.setFormatter(formatter)
logger.addHandler(h)
# Attach handlers to the loggers.
for h in handlers:
if h.formatter is None:
h.setFormatter(formatter)
logger.addHandler(h)

if level is not None:
logger.setLevel(level)
if level is not None:
logger.setLevel(level)

return logger
return logger


class VerbosityOption(ExtraOption):
Expand Down Expand Up @@ -273,15 +275,16 @@ def __init__(
if not param_decls:
param_decls = ("--verbosity", "-v")

# Use the provided logger instance as-is.
if isinstance(default_logger, Logger):
logger = default_logger
# If a string is provided, use it as the logger name.
elif isinstance(default_logger, str):
logger = logging.getLogger(default_logger)
# ``None`` will produce a default root logger.
else:
logger = extra_basic_config(default_logger)
with logging._lock:
# A logger object has been provided, use it as-is.
if isinstance(default_logger, Logger):
logger = default_logger
# Retrieves the logger object as it is from the registry, if it exists.
elif default_logger in logging.Logger.manager.loggerDict:
logger = logging.getLogger(default_logger)
# Create a new logger with Click Extra's default configuration.
else:
logger = extra_basic_config(default_logger)

# Store the logger name for later use.
self.logger_name = logger.name
Expand Down

0 comments on commit ccd4591

Please sign in to comment.