use logging code from template repo

This commit is contained in:
Francisco Schulz 2023-02-06 17:03:44 +01:00
parent 6e7e127cd3
commit c8f3192b79
2 changed files with 44 additions and 4 deletions

View File

@ -1,5 +1,8 @@
import logging
import os
import pathlib
import sys
from typing import Union
from image_prediction.config import CONFIG
@ -25,3 +28,37 @@ def make_logger_getter():
get_logger = make_logger_getter()
def setup_logger(
level: Union[int, str] = "DEBUG",
file: Union[str, pathlib.Path] = "./logs/dev.log",
save: bool = False
) -> logging.Logger:
LOG_FORMAT = "%(asctime)s [%(levelname)s] - [%(filename)s -> %(funcName)s() -> %(lineno)s] : %(message)s"
DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
logger = logging.getLogger()
logger.setLevel(level)
stream_handler = logging.StreamHandler(sys.stdout)
stream_handler_format = logging.Formatter(LOG_FORMAT, datefmt=DATE_FORMAT)
stream_handler.setFormatter(stream_handler_format)
logger.addHandler(stream_handler)
if save:
file = pathlib.Path(file).absolute()
path = file.parent
if not os.path.exists(path):
os.mkdir(path)
file_handler = logging.FileHandler(file)
file_handler_format = logging.Formatter(LOG_FORMAT, datefmt=DATE_FORMAT)
file_handler.setFormatter(file_handler_format)
logger.addHandler(file_handler)
return logger

View File

@ -12,6 +12,7 @@ from image_prediction.locations import CONFIG_FILE, MLRUNS_DIR
from image_prediction.pipeline import load_model, load_pipeline
from image_prediction.utils.banner import load_banner
from image_prediction.utils.process_wrapping import wrap_in_process
from image_prediction.utils.logger import setup_logger
PYINFRA_CONFIG = config.get_config()
IMAGE_CONFIG = Config(CONFIG_FILE)
@ -19,9 +20,11 @@ MODEL = load_model(MLRUNS_DIR, CONFIG)
BUCKET = PYINFRA_CONFIG.storage_bucket
STORAGE = get_storage(PYINFRA_CONFIG)
logging.getLogger().addHandler(logging.StreamHandler())
logger = logging.getLogger("main")
logger.setLevel(PYINFRA_CONFIG.logging_level_root)
# logger = logging.getLogger(__name__)
# logger.addHandler(logging.StreamHandler())
# logger = logging.getLogger("main")
# logger.setLevel(PYINFRA_CONFIG.logging_level_root)
logger = setup_logger()
# A component of the callback (probably tensorflow) does not release allocated memory (see RED-4206).