1
Fork 0
mirror of https://github.com/Steffo99/unimore-bda-6.git synced 2024-11-22 07:54:19 +00:00
bda-6-steffo/unimore_bda_6/log.py

86 lines
2.8 KiB
Python
Raw Normal View History

2023-02-03 01:10:00 +00:00
import collections
2023-02-01 16:46:25 +00:00
import logging
import coloredlogs
2023-02-10 03:07:34 +00:00
import pathlib
2023-02-01 16:46:25 +00:00
2023-02-14 01:25:38 +00:00
logging.addLevelName(11, "SUCCESS")
logging.addLevelName(12, "FAILURE")
2023-02-03 22:27:44 +00:00
this_log = logging.getLogger(__name__)
2023-02-01 16:46:25 +00:00
2023-02-10 03:07:34 +00:00
def install_general_log_handlers():
"""
Setup the `unimore_bda_6` and ``__main__`` loggers.
2023-02-01 16:46:25 +00:00
2023-02-10 03:07:34 +00:00
With colors!
"""
main_logger: logging.Logger = logging.getLogger("__main__")
interesting_loggers: list[logging.Logger] = [
main_logger,
logging.getLogger("unimore_bda_6"),
]
chatty_loggers: list[logging.Logger] = [
logging.getLogger("unimore_bda_6.database.cache"),
logging.getLogger("unimore_bda_6.database.datatypes"),
]
this_log.debug("Installing console handlers...")
for logger in interesting_loggers:
coloredlogs.install(
logger=logger,
2023-02-08 18:46:05 +00:00
level="DEBUG" if __debug__ else "INFO",
2023-02-10 03:07:34 +00:00
fmt="{asctime} | {name} | {levelname} | {message}",
style="{",
level_styles=dict(
debug=dict(color="white"),
info=dict(color="cyan"),
2023-02-14 01:25:38 +00:00
warning=dict(color="yellow", bold=True),
error=dict(color="red", bold=True),
2023-02-08 18:46:05 +00:00
critical=dict(color="black", background="red", bold=True),
2023-02-14 01:25:38 +00:00
success=dict(color="green"),
failure=dict(color="yellow"),
),
field_styles=dict(
asctime=dict(color='magenta'),
levelname=dict(color='blue', bold=True),
name=dict(color='blue'),
),
isatty=True,
)
2023-02-10 03:07:34 +00:00
this_log.debug("Installed console log handler on: %s", logger)
this_log.debug("Silencing chatty loggers...")
for logger in chatty_loggers:
logger.setLevel("INFO")
this_log.debug("Silenced: %s", logger)
2023-02-01 16:46:25 +00:00
2023-02-10 03:07:34 +00:00
log_file_path = pathlib.Path("./data/logs/last_run.tsv")
log_directory_path = log_file_path.parent
this_log.debug("Ensuring %s exists...", log_directory_path)
log_directory_path.mkdir(parents=True, exist_ok=True)
this_log.debug("Ensuring %s exists...", log_file_path)
open(log_file_path, "w").close()
this_log.debug("Installing FileHandler for the __main__ logger at %s...", log_file_path)
file_handler = logging.FileHandler(log_file_path, mode="w")
file_handler.formatter = logging.Formatter("{asctime}\t{name}\t{levelname}\t{message}", style="{")
main_logger.addHandler(file_handler)
this_log.debug("Installed FileHandler for the __main__ logger at %s!", log_file_path)
2023-02-08 09:54:14 +00:00
2023-02-01 16:46:25 +00:00
2023-02-03 01:10:00 +00:00
_passage_counts = collections.defaultdict(lambda: 0)
2023-02-03 22:27:44 +00:00
def count_passage(log: logging.Logger, key: str, mod: int):
2023-02-03 01:10:00 +00:00
_passage_counts[key] += 1
if not _passage_counts[key] % mod:
log.debug("%s - %d calls", key, _passage_counts[key])
2023-02-01 16:46:25 +00:00
__all__ = (
2023-02-10 03:07:34 +00:00
"install_general_log_handlers",
2023-02-03 01:10:00 +00:00
"count_passage",
2023-02-01 16:46:25 +00:00
)