Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core/logging: fix issue with logger setup called multiple times when called with different levels #310

Merged
merged 1 commit into from
Sep 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions my/core/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@ def setup_logger(logger: str | logging.Logger, *, level: LevelIsh = None) -> Non
# if it's already set, the user requested a different logging level, let's respect that
logger.setLevel(lvl)

_setup_handlers_and_formatters(name=logger.name)


# cached since this should only be done once per logger instance
@lru_cache(None)
def _setup_handlers_and_formatters(name: str) -> None:
logger = logging.getLogger(name)

logger.addFilter(AddExceptionTraceback())

ch = logging.StreamHandler()
Expand Down Expand Up @@ -204,7 +212,6 @@ def emit(self, record: logging.LogRecord) -> None:
self.handleError(record)


@lru_cache(None) # cache so it's only initialized once
def make_logger(name: str, *, level: LevelIsh = None) -> logging.Logger:
logger = logging.getLogger(name)
setup_logger(logger, level=level)
Expand All @@ -216,8 +223,7 @@ def make_logger(name: str, *, level: LevelIsh = None) -> logging.Logger:
# OK, when stdout is not a tty, enlighten doesn't log anything, good
def get_enlighten():
# TODO could add env variable to disable enlighten for a module?
from unittest.mock import Mock
# Mock to return stub so cients don't have to think about it
from unittest.mock import Mock # Mock to return stub so cients don't have to think about it

# for now hidden behind the flag since it's a little experimental
if os.environ.get('ENLIGHTEN_ENABLE', None) is None:
Expand Down