generated from roboflow/template-python
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
feat: replace print statements with structured logging #2154
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
Merged
+192
−23
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7a868ea
Initial plan
Copilot e398c57
feat: implement logging system to replace print statements
Copilot 6a79a62
refactor: rename get_logger to _get_logger to mark as internal
Copilot b634dca
Merge branch 'develop' into copilot/implement-logging-system
Borda 434e876
fix(pre_commit): 🎨 auto format pre-commit hooks
pre-commit-ci[bot] 7e815e6
test: add logger tests, fix corrupted-file warning test, add docstrin…
Copilot 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,51 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
| import os | ||
| import sys | ||
|
|
||
|
|
||
| def _get_logger(name: str = "supervision", level: int | None = None) -> logging.Logger: | ||
| """Creates and configures a logger with stdout and stderr handlers. | ||
|
|
||
| This function creates a logger that sends INFO and DEBUG level logs to stdout, | ||
| and WARNING, ERROR, and CRITICAL level logs to stderr. If the logger already | ||
| has handlers, it returns the existing logger without adding new handlers. | ||
|
|
||
| The log level can be specified directly or through the `LOG_LEVEL` environment | ||
| variable. | ||
|
|
||
| Args: | ||
| name: The name of the logger. Defaults to `"supervision"`. | ||
| level: The logging level to set. If `None`, uses the `LOG_LEVEL` environment | ||
| variable, defaulting to `INFO` if not set. | ||
|
|
||
| Returns: | ||
| A configured `logging.Logger` instance. | ||
| """ | ||
| if level is None: | ||
| level = getattr(logging, os.getenv("LOG_LEVEL", "INFO").upper(), logging.INFO) | ||
|
|
||
| logger = logging.getLogger(name) | ||
| logger.setLevel(level) | ||
|
|
||
| if not logger.handlers: | ||
| formatter = logging.Formatter( | ||
| "[%(asctime)s] [%(levelname)s] %(name)s - %(message)s", | ||
| datefmt="%Y-%m-%d %H:%M:%S", | ||
| ) | ||
|
|
||
| stdout_handler = logging.StreamHandler(sys.stdout) | ||
| stdout_handler.setLevel(logging.DEBUG) | ||
| stdout_handler.addFilter(lambda r: r.levelno <= logging.INFO) | ||
| stdout_handler.setFormatter(formatter) | ||
|
|
||
| stderr_handler = logging.StreamHandler(sys.stderr) | ||
| stderr_handler.setLevel(logging.WARNING) | ||
| stderr_handler.setFormatter(formatter) | ||
|
|
||
| logger.addHandler(stdout_handler) | ||
| logger.addHandler(stderr_handler) | ||
| logger.propagate = False | ||
|
|
||
| return logger | ||
Borda marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or 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 hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.