-
Notifications
You must be signed in to change notification settings - Fork 414
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
17
commits into
conda:main
Choose a base branch
from
kenodegard:logging
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
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 14 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
a27c919
Move CLI logging init into a new dedicated module
kenodegard 4ed007f
Remove DuplicateFilter.clear
kenodegard b4999b2
Configure conda_build root logger
kenodegard 2ea63a3
Add news
kenodegard dc28852
Merge remote-tracking branch 'upstream/main' into logging
kenodegard 8271662
Set log level to INFO for test_extra_meta
kenodegard 5b95b2d
Adjust logging imports
kenodegard 675cb0a
Move init_logging import to top
kenodegard 331f127
Merge remote-tracking branch 'upstream/main' into logging
kenodegard 916033f
Merge remote-tracking branch 'upstream/main' into logging
kenodegard cb8c434
Update environ.py
kenodegard ec19404
Undo conda modifying root logger
kenodegard 5983950
Restore default log level to INFO
kenodegard f87a366
Remove unnecessary logging config
kenodegard 1a2194c
Correct logging test
kenodegard 7d71926
Rename log.warn to log.warning
kenodegard 105fcf8
Import warnings, not warn
kenodegard File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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,85 @@ | ||
# 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 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) | ||
|
||
|
||
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. | ||
""" | ||
# undo conda messing with the root logger | ||
logging.getLogger(None).setLevel(logging.WARNING) | ||
|
||
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 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: | ||
log.addHandler(stdout := logging.StreamHandler(sys.stdout)) | ||
stdout.addFilter(LessThanFilter(logging.WARNING)) | ||
stdout.addFilter(DuplicateFilter()) | ||
stdout.setFormatter(logging.Formatter("%(levelname)s: %(message)s")) | ||
|
||
log.addHandler(stderr := logging.StreamHandler(sys.stderr)) | ||
stderr.addFilter(GreaterThanFilter(logging.INFO)) | ||
stderr.addFilter(DuplicateFilter()) | ||
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