Source code for mlthon.basics.logging
import logging
import os
import sys
import typing
if "_LOGGING_SETUP" not in globals():
_LOGGING_SETUP = False
[docs]def setup_logging(log_filepath: str, display_output_in_stdout: bool = False):
global _LOGGING_SETUP
if _LOGGING_SETUP is True:
return
_LOGGING_SETUP = True
print("Log filepath: %s" % log_filepath)
log_line_format = '[%(asctime)s] %(filename)s:%(lineno)d %(levelname)s: %(message)s'
logging.basicConfig(filename=log_filepath, level=logging.INFO, format=log_line_format, datefmt='%Y-%m-%d %H:%M:%S')
# If the code is running in pycharm, display output to stdout
if "PYCHARM_HOSTED" in os.environ or display_output_in_stdout:
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setFormatter(logging.Formatter(log_line_format))
logging.getLogger().addHandler(console_handler)
[docs]def get_logger(client: typing.Any):
return get_named_logger(type(client).__name__)
[docs]def get_named_logger(name: str):
return logging.getLogger(name)