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

Nicely format logged warnings #223

Merged
merged 4 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- We now log steps rather than epochs as units of progress during training.
- Validation performance metrics are logged (and added to tensorboard) at the validation epoch, and training loss is logged at the end of training epoch, i.e. training and validation metrics are logged asynchronously.
- Irrelevant warning messages on the console output and in the log file are no longer shown.
- Nicely format logged warnings.

### Removed

Expand Down
18 changes: 13 additions & 5 deletions casanovo/casanovo.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
from pathlib import Path
from typing import Optional, Tuple

warnings.formatwarning = lambda message, category, *args, **kwargs: (
f"{category.__name__}: {message}"
)
warnings.filterwarnings("ignore", category=DeprecationWarning)
warnings.filterwarnings(
"ignore",
Expand Down Expand Up @@ -283,8 +286,9 @@ def setup_logging(

# Configure logging.
logging.captureWarnings(True)
root = logging.getLogger()
root.setLevel(logging.DEBUG)
root_logger = logging.getLogger()
root_logger.setLevel(logging.DEBUG)
warnings_logger = logging.getLogger("py.warnings")

# Formatters for file vs console:
console_formatter = logging.Formatter("{levelname}: {message}", style="{")
Expand All @@ -297,13 +301,17 @@ def setup_logging(
console_handler = logging.StreamHandler(sys.stderr)
console_handler.setLevel(logging_levels[verbosity.lower()])
console_handler.setFormatter(console_formatter)
root.addHandler(console_handler)
root_logger.addHandler(console_handler)
warnings_logger.addHandler(console_handler)
file_handler = logging.FileHandler(output.with_suffix(".log"))
file_handler.setFormatter(log_formatter)
root.addHandler(file_handler)
root_logger.addHandler(file_handler)
warnings_logger.addHandler(file_handler)

# Disable dependency non-critical log messages.
logging.getLogger("depthcharge").setLevel(logging.INFO)
logging.getLogger("depthcharge").setLevel(
logging_levels[verbosity.lower()]
)
logging.getLogger("fsspec").setLevel(logging.WARNING)
logging.getLogger("github").setLevel(logging.WARNING)
logging.getLogger("h5py").setLevel(logging.WARNING)
Expand Down
Loading