Skip to content

Commit

Permalink
update gandlf_logger
Browse files Browse the repository at this point in the history
- Update gandlf_logging_setup
- Update logging_cofing
- Update setup.py
  • Loading branch information
benmalef authored Jul 15, 2024
1 parent ba795fe commit 935d4a3
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 20 deletions.
10 changes: 8 additions & 2 deletions GANDLF/logging_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@ 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:
format: "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
(): colorlog.ColoredFormatter
format: "%(log_color)s%(asctime)s - %(name)s - %(levelname)s - %(message)s"
datefmt: "%Y-%m-%d %H:%M:%S"
filters:
warnings_filter:
Expand All @@ -28,7 +35,6 @@ handlers:
class: logging.handlers.RotatingFileHandler
level: DEBUG
formatter: detailed
filename: tmp/gandlf/gandlf.log
maxBytes: 10485760
backupCount: 2
loggers: # you can add your customized logger
Expand Down
48 changes: 33 additions & 15 deletions GANDLF/utils/gandlf_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,49 @@
import yaml
from pathlib import Path
from importlib import resources
import os
import colorlog


def gandlf_logger_setup(config_path="logging_config.yaml") -> logging.Logger:
def gandlf_logger_setup(log_dir=None, config_path="logging_config.yaml"):
"""
It sets up the logger. Read from logging_config.
It sets up the logger. Reads from logging_config.
If log_dir is None, the logs are flashed to console.
Args:
logger_name (str): logger name, the name should be the same in the logging_config
log_dir (str): dir path for saving the logs
config_path (str): file path for the configuration
Returns:
logging.Logger
"""
# create dir for storing the messages
current_dir = Path.cwd()
directory = Path.joinpath(current_dir, "tmp/gandlf")
directory.mkdir(parents=True, exist_ok=True)
"""

with resources.open_text("GANDLF", config_path) as file:
config_dict = yaml.safe_load(file)
logging.config.dictConfig(config_dict)
if log_dir == None: # flash logs
formatter = colorlog.ColoredFormatter(
"%(log_color)s%(asctime)s - %(name)s - %(levelname)s - %(module)s:%(funcName)s:%(lineno)d - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
log_colors={
"DEBUG": "blue",
"INFO": "green",
"WARNING": "yellow",
"ERROR": "red",
"CRITICAL": "bold_red",
},
)
console_handler = logging.StreamHandler()
console_handler.setFormatter(formatter)
logging.root.setLevel(logging.DEBUG)
logging.root.addHandler(console_handler)

else: # create the log file
output_dir = Path(log_dir)
Path(output_dir).mkdir(parents=True, exist_ok=True)
with resources.open_text("GANDLF", config_path) as file:
config_dict = yaml.safe_load(file)
config_dict["handlers"]["rotatingFileHandler"]["filename"] = str(
Path.joinpath(output_dir, "gandlf.log")
)
logging.config.dictConfig(config_dict)

logging.captureWarnings(True)

# return logging.getLogger(logger_name)


class InfoOnlyFilter(logging.Filter):
"""
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
]

# Any extra files should be located at `GANDLF` module folder (not in repo root)
extra_files = ["logging_config.yml"]
extra_files = ["logging_config.yaml"]
toplevel_package_excludes = ["testing*"]

# specifying version for `black` separately because it is also used to [check for lint](https://github.com/mlcommons/GaNDLF/blob/master/.github/workflows/black.yml)
Expand Down Expand Up @@ -80,6 +80,7 @@
"deprecated",
"packaging==24.0",
"typer==0.9.0",
"colorlog",
]

if __name__ == "__main__":
Expand Down
6 changes: 4 additions & 2 deletions testing/test_full.py
Original file line number Diff line number Diff line change
Expand Up @@ -3190,16 +3190,18 @@ def test_generic_data_split():
def test_generic_logging(capsys):
print("52: Starting test for logging")

gandlf_logger_setup()
gandlf_logger_setup("testing/log")
message = "Testing logging"

logging.debug(message)

# tests if the message is in the file.log
with open("tmp/gandlf/gandlf.log", "r") as log_file:
with open("testing/log/gandlf.log", "r") as log_file:
logs = log_file.read()
assert message in logs

shutil.rmtree("testing/log")

# test the stout info level. The stout must show only INFO messages
message = "Testing stout logging"
logging.info(message)
Expand Down

0 comments on commit 935d4a3

Please sign in to comment.