1
Fork 0
mirror of https://github.com/Steffo99/unimore-bda-6.git synced 2024-11-25 17:24:20 +00:00
bda-6-steffo/unimore_bda_6/log.py

54 lines
1.5 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-03 22:27:44 +00:00
this_log = logging.getLogger(__name__)
2023-02-01 16:46:25 +00:00
def install_log_handler(loggers: list[logging.Logger] = None):
if loggers is None:
loggers = [
logging.getLogger("__main__"),
logging.getLogger("unimore_bda_6"),
]
2023-02-01 16:46:25 +00:00
for logger in loggers:
coloredlogs.install(
logger=logger,
2023-02-08 18:46:05 +00:00
level="DEBUG" if __debug__ else "INFO",
fmt="{asctime} | {name:<80} | {levelname:>8} | {message}",
style="{",
level_styles=dict(
debug=dict(color="white"),
info=dict(color="cyan"),
warning=dict(color="yellow"),
error=dict(color="red"),
2023-02-08 18:46:05 +00:00
critical=dict(color="black", background="red", bold=True),
),
field_styles=dict(
asctime=dict(color='magenta'),
levelname=dict(color='blue', bold=True),
name=dict(color='blue'),
),
isatty=True,
)
2023-02-03 22:27:44 +00:00
this_log.debug("Installed custom log handler on: %s", logger)
2023-02-01 16:46:25 +00:00
2023-02-08 09:54:14 +00:00
logging.getLogger("unimore_bda_6.database.cache").setLevel("INFO")
logging.getLogger("unimore_bda_6.database.datatypes").setLevel("INFO")
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__ = (
"install_log_handler",
2023-02-03 01:10:00 +00:00
"count_passage",
2023-02-01 16:46:25 +00:00
)