-
Notifications
You must be signed in to change notification settings - Fork 431
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
Move CLI's logging initialization into a dedicated module #5275
Draft
kenodegard
wants to merge
15
commits into
conda:main
Choose a base branch
from
kenodegard:logging
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
dc07a9c
Move CLI logging init into a new dedicated module
kenodegard a8118db
Remove DuplicateFilter.clear
kenodegard 490d28b
Configure conda_build root logger
kenodegard 13cd79c
Add news
kenodegard d764d30
Set log level to INFO for test_extra_meta
kenodegard 98e837a
Adjust logging imports
kenodegard 2764e0c
Move init_logging import to top
kenodegard f93d11c
Update environ.py
kenodegard d864abc
Undo conda modifying root logger
kenodegard 9f64bb3
Restore default log level to INFO
kenodegard 27e7a20
Remove unnecessary logging config
kenodegard 267b569
Correct logging test
kenodegard d6fdde4
Import warnings, not warn
kenodegard fdde7c8
Correct imports
kenodegard 22930e6
Ruff
kenodegard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# Copyright (C) 2014 Anaconda, Inc | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
from __future__ import annotations | ||
|
||
import logging | ||
import logging.config | ||
import os | ||
import os.path | ||
import sys | ||
from functools import lru_cache | ||
from pathlib import Path | ||
from typing import TYPE_CHECKING | ||
|
||
from conda.base.context import context | ||
from yaml import safe_load | ||
|
||
if TYPE_CHECKING: | ||
from logging import LogRecord | ||
|
||
|
||
# https://stackoverflow.com/a/31459386/1170370 | ||
class LessThanFilter(logging.Filter): | ||
def __init__(self, exclusive_maximum: int, name: str = "") -> None: | ||
super().__init__(name) | ||
self.max_level = exclusive_maximum | ||
|
||
def filter(self, record: LogRecord) -> bool: | ||
return record.levelno < self.max_level | ||
|
||
|
||
class GreaterThanFilter(logging.Filter): | ||
def __init__(self, exclusive_minimum: int, name: str = "") -> None: | ||
super().__init__(name) | ||
self.min_level = exclusive_minimum | ||
|
||
def filter(self, record: LogRecord) -> bool: | ||
return record.levelno > self.min_level | ||
|
||
|
||
class DuplicateFilter(logging.Filter): | ||
msgs: set[str] = set() | ||
|
||
def filter(self, record: LogRecord) -> bool: | ||
try: | ||
return record.msg not in self.msgs | ||
finally: | ||
self.msgs.add(record.msg) | ||
|
||
|
||
@lru_cache | ||
def init_logging() -> None: | ||
""" | ||
Default initialization of logging for conda-build CLI. | ||
|
||
When using conda-build as a CLI tool (not as a library) we wish to limit logging to | ||
avoid duplication and to otherwise offer some default behavior. | ||
|
||
This is a onetime initialization that should be called at the start of CLI execution. | ||
""" | ||
# undo conda messing with the root logger | ||
logging.getLogger(None).setLevel(logging.WARNING) | ||
|
||
# load the logging configuration from the config file | ||
config_file = context.conda_build.get("log_config_file") | ||
if config_file: | ||
config_file = Path(os.path.expandvars(config_file)).expanduser().resolve() | ||
logging.config.dictConfig(safe_load(config_file.read_text())) | ||
|
||
log = logging.getLogger("conda_build") | ||
|
||
# historically conda_build has defaulted the logging to INFO and so all of the | ||
# log.info is viewed as default output, until we convert all of the existing | ||
# log.info to standard print statements we will need to continue defaulting to INFO | ||
if log.level == logging.NOTSET: | ||
log.setLevel(logging.INFO) | ||
|
||
# we don't want propagation to the root logger in CLI, but we do want it in tests | ||
# this is a pytest limitation: https://github.com/pytest-dev/pytest/issues/3697 | ||
log.propagate = "PYTEST_CURRENT_TEST" in os.environ | ||
|
||
if not log.handlers: | ||
# only add our handlers when none are added via logging.config | ||
|
||
# filter DEBUG/INFO messages to stdout | ||
log.addHandler(stdout := logging.StreamHandler(sys.stdout)) | ||
stdout.addFilter(LessThanFilter(logging.WARNING)) | ||
stdout.addFilter(DuplicateFilter()) # avoid duplicate messages | ||
stdout.setFormatter(logging.Formatter("%(levelname)s: %(message)s")) | ||
|
||
# filter WARNING/ERROR/CRITICAL messages to stderr | ||
log.addHandler(stderr := logging.StreamHandler(sys.stderr)) | ||
stderr.addFilter(GreaterThanFilter(logging.INFO)) | ||
stderr.addFilter(DuplicateFilter()) # avoid duplicate messages | ||
stderr.setFormatter(logging.Formatter("%(levelname)s: %(message)s")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To remove this we will first need to remove
initialize_root_logger
from conda's log initialization (and then wait some number of conda releases until we can bump up conda-build's minimum conda dependency): https://github.com/conda/conda/blob/eb459543b0c280011216f2f22eedc1c8dcbd4d9f/conda/gateways/logging.py#L142-L147