-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #893 from benmalef/add_logging_final_version
Add logging final version
- Loading branch information
Showing
23 changed files
with
232 additions
and
16 deletions.
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
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
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
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,47 @@ | ||
version: 1 | ||
formatters: | ||
detailed: | ||
format: "%(asctime)s - %(name)s - %(levelname)s - %(module)s:%(funcName)s:%(lineno)d - %(message)s" | ||
datefmt: "%Y-%m-%d %H:%M:%S" | ||
log_colors: | ||
DEBUG: "white" | ||
INFO: "green" | ||
WARNING: "yellow" | ||
ERROR: "red" | ||
CRITICAL: "bold_red" | ||
simple: | ||
(): colorlog.ColoredFormatter | ||
format: "%(log_color)s%(asctime)s - %(levelname)s - %(message)s" | ||
datefmt: "%Y-%m-%d %H:%M:%S" | ||
filters: | ||
warnings_filter: | ||
(): logging.Filter | ||
name: "py.warnings" | ||
info_only_filter: | ||
(): GANDLF.utils.gandlf_logging.InfoOnlyFilter | ||
handlers: | ||
stdoutHandler: # only display info level | ||
class: logging.StreamHandler | ||
level: INFO | ||
formatter: simple | ||
filters: [info_only_filter] | ||
stream: ext://sys.stdout | ||
stderrHandler: # display warning and above messages | ||
class: logging.StreamHandler | ||
level: WARNING | ||
formatter: detailed | ||
stream: ext://sys.stderr | ||
rotatingFileHandler: | ||
class: logging.handlers.RotatingFileHandler | ||
level: DEBUG | ||
formatter: detailed | ||
maxBytes: 10485760 | ||
backupCount: 2 | ||
loggers: # you can add your customized logger | ||
debug_logger: | ||
level: DEBUG | ||
handlers: [stdoutHandler, rotatingFileHandler, stderrHandler] | ||
propagate: no | ||
root: | ||
level: DEBUG | ||
handlers: [stdoutHandler, rotatingFileHandler, stderrHandler] |
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 |
---|---|---|
|
@@ -68,3 +68,4 @@ | |
) | ||
|
||
from .data_splitter import split_data | ||
from .gandlf_logging import logger_setup, InfoOnlyFilter |
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,61 @@ | ||
import logging | ||
import yaml | ||
from pathlib import Path | ||
from importlib import resources | ||
import tempfile | ||
from GANDLF.utils import get_unique_timestamp | ||
|
||
|
||
def _create_tmp_log_file(): | ||
tmp_dir = Path(tempfile.gettempdir()) | ||
log_dir = Path.joinpath(tmp_dir, ".gandlf") | ||
log_dir.mkdir(parents=True, exist_ok=True) | ||
log_file = Path.joinpath(log_dir, get_unique_timestamp() + ".log") | ||
return log_file | ||
|
||
|
||
def _create_log_file(log_file): | ||
log_file = Path(log_file) | ||
log_file.write_text("Starting GaNDLF logging session \n") | ||
|
||
|
||
def _configure_logging_with_logfile(log_file, config_path): | ||
with resources.open_text("GANDLF", config_path) as file: | ||
config_dict = yaml.safe_load(file) | ||
config_dict["handlers"]["rotatingFileHandler"]["filename"] = str(log_file) | ||
logging.config.dictConfig(config_dict) | ||
|
||
|
||
def logger_setup(log_file=None, config_path="logging_config.yaml") -> None: | ||
""" | ||
It sets up the logger. Reads from logging_config. | ||
Args: | ||
log_file (str): dir path for saving the logs, defaults to `None`, at which time logs are flushed to console. | ||
config_path (str): file path for the configuration | ||
""" | ||
|
||
logging.captureWarnings(True) | ||
log_tmp_file = log_file | ||
if log_file is None: # create tmp file | ||
log_tmp_file = _create_tmp_log_file() | ||
logging.info(f"The logs are saved in {log_tmp_file}") | ||
_create_log_file(log_tmp_file) | ||
_configure_logging_with_logfile(log_tmp_file, config_path) | ||
|
||
|
||
class InfoOnlyFilter(logging.Filter): | ||
""" | ||
Display only INFO messages. | ||
""" | ||
|
||
def filter(self, record): | ||
""" | ||
Determines if the specified record is to be logged. | ||
Args: | ||
record (logging.LogRecord): The log record to be evaluated. | ||
Returns: | ||
bool: True if the log record should be processed, False otherwise. | ||
""" | ||
return record.levelno == logging.INFO |
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 |
---|---|---|
|
@@ -4,3 +4,4 @@ exclude *.toml | |
include setup.py | ||
include .dockerignore | ||
include Dockerfile-* | ||
include logging_config.yml |
Oops, something went wrong.