2022-09-11 22:59:19 +02:00
|
|
|
import sys
|
|
|
|
from functools import partialmethod
|
|
|
|
from loguru import logger
|
|
|
|
|
2022-09-11 23:08:17 +02:00
|
|
|
STDOUT_LEVELS = ["GENERATION", "PROMPT"]
|
2022-09-12 00:49:51 +02:00
|
|
|
INIT_LEVELS = ["INIT", "INIT_OK", "INIT_WARN", "INIT_ERR"]
|
2022-09-12 00:19:51 +02:00
|
|
|
MESSAGE_LEVELS = ["MESSAGE"]
|
2022-09-12 16:30:19 +02:00
|
|
|
# By default we're at error level or higher
|
2022-09-12 16:47:10 +02:00
|
|
|
verbosity = 20
|
2022-09-12 16:30:19 +02:00
|
|
|
quiet = 0
|
|
|
|
|
|
|
|
def set_logger_verbosity(count):
|
|
|
|
global verbosity
|
|
|
|
# The count comes reversed. So count = 0 means minimum verbosity
|
|
|
|
# While count 5 means maximum verbosity
|
|
|
|
# So the more count we have, the lowe we drop the versbosity maximum
|
2022-09-12 16:47:10 +02:00
|
|
|
verbosity = 20 - (count * 10)
|
2022-09-12 16:30:19 +02:00
|
|
|
|
|
|
|
def quiesce_logger(count):
|
|
|
|
global quiet
|
|
|
|
# The bigger the count, the more silent we want our logger
|
|
|
|
quiet = count * 10
|
2022-09-11 23:08:17 +02:00
|
|
|
|
2022-09-11 22:59:19 +02:00
|
|
|
def is_stdout_log(record):
|
2022-09-12 16:30:19 +02:00
|
|
|
if record["level"].name not in STDOUT_LEVELS:
|
|
|
|
return(False)
|
|
|
|
if record["level"].no < verbosity + quiet:
|
|
|
|
return(False)
|
|
|
|
return(True)
|
2022-09-11 23:08:17 +02:00
|
|
|
|
2022-09-11 23:55:48 +02:00
|
|
|
def is_init_log(record):
|
2022-09-12 16:30:19 +02:00
|
|
|
if record["level"].name not in INIT_LEVELS:
|
|
|
|
return(False)
|
|
|
|
if record["level"].no < verbosity + quiet:
|
|
|
|
return(False)
|
|
|
|
return(True)
|
2022-09-11 23:55:48 +02:00
|
|
|
|
2022-09-12 00:19:51 +02:00
|
|
|
def is_msg_log(record):
|
2022-09-12 16:30:19 +02:00
|
|
|
if record["level"].name not in MESSAGE_LEVELS:
|
|
|
|
return(False)
|
|
|
|
if record["level"].no < verbosity + quiet:
|
|
|
|
return(False)
|
|
|
|
return(True)
|
2022-09-12 00:19:51 +02:00
|
|
|
|
2022-09-11 22:59:19 +02:00
|
|
|
def is_stderr_log(record):
|
2022-09-12 16:30:19 +02:00
|
|
|
if record["level"].name in STDOUT_LEVELS + INIT_LEVELS + MESSAGE_LEVELS:
|
|
|
|
return(False)
|
|
|
|
if record["level"].no < verbosity + quiet:
|
|
|
|
return(False)
|
|
|
|
return(True)
|
|
|
|
|
|
|
|
def test_logger():
|
|
|
|
logger.generation("This is a generation message\nIt is typically multiline\nThee Lines".encode("unicode_escape").decode("utf-8"))
|
|
|
|
logger.prompt("This is a prompt message")
|
|
|
|
logger.debug("Debug Message")
|
|
|
|
logger.info("Info Message")
|
|
|
|
logger.warning("Info Warning")
|
|
|
|
logger.error("Error Message")
|
|
|
|
logger.critical("Critical Message")
|
|
|
|
logger.init("This is an init message", status="Starting")
|
|
|
|
logger.init_ok("This is an init message", status="OK")
|
|
|
|
logger.init_warn("This is an init message", status="Warning")
|
|
|
|
logger.init_err("This is an init message", status="Error")
|
|
|
|
logger.message("This is user message")
|
|
|
|
sys.exit()
|
|
|
|
|
2022-09-11 23:08:17 +02:00
|
|
|
|
2022-09-11 22:59:19 +02:00
|
|
|
logfmt = "<level>{level: <10}</level> | <green>{name}</green>:<green>{function}</green>:<green>{line}</green> - <level>{message}</level>"
|
|
|
|
genfmt = "<level>{level: <10}</level> @ <green>{time:YYYY-MM-DD HH:mm:ss}</green> | <level>{message}</level>"
|
2022-09-12 01:13:12 +02:00
|
|
|
initfmt = "<magenta>INIT </magenta> | <level>{extra[status]: <10}</level> | <magenta>{message}</magenta>"
|
2022-09-12 00:19:51 +02:00
|
|
|
msgfmt = "<level>{level: <10}</level> | <level>{message}</level>"
|
2022-09-11 23:08:17 +02:00
|
|
|
|
2022-09-11 23:55:48 +02:00
|
|
|
logger.level("GENERATION", no=24, color="<cyan>")
|
|
|
|
logger.level("PROMPT", no=23, color="<yellow>")
|
2022-09-12 01:00:03 +02:00
|
|
|
logger.level("INIT", no=31, color="<white>")
|
|
|
|
logger.level("INIT_OK", no=31, color="<green>")
|
|
|
|
logger.level("INIT_WARN", no=31, color="<yellow>")
|
|
|
|
logger.level("INIT_ERR", no=31, color="<red>")
|
2022-09-12 16:30:19 +02:00
|
|
|
# Messages contain important information without which this application might not be able to be used
|
|
|
|
# As such, they have the highest priority
|
|
|
|
logger.level("MESSAGE", no=61, color="<green>")
|
2022-09-11 23:08:17 +02:00
|
|
|
|
2022-09-11 22:59:19 +02:00
|
|
|
logger.__class__.generation = partialmethod(logger.__class__.log, "GENERATION")
|
2022-09-11 23:08:17 +02:00
|
|
|
logger.__class__.prompt = partialmethod(logger.__class__.log, "PROMPT")
|
2022-09-11 23:55:48 +02:00
|
|
|
logger.__class__.init = partialmethod(logger.__class__.log, "INIT")
|
2022-09-12 00:49:51 +02:00
|
|
|
logger.__class__.init_ok = partialmethod(logger.__class__.log, "INIT_OK")
|
|
|
|
logger.__class__.init_warn = partialmethod(logger.__class__.log, "INIT_WARN")
|
|
|
|
logger.__class__.init_err = partialmethod(logger.__class__.log, "INIT_ERR")
|
2022-09-12 00:19:51 +02:00
|
|
|
logger.__class__.message = partialmethod(logger.__class__.log, "MESSAGE")
|
2022-09-11 23:08:17 +02:00
|
|
|
|
2022-09-11 22:59:19 +02:00
|
|
|
config = {
|
|
|
|
"handlers": [
|
|
|
|
{"sink": sys.stderr, "format": logfmt, "colorize":True, "filter": is_stderr_log},
|
2022-09-11 23:08:17 +02:00
|
|
|
{"sink": sys.stdout, "format": genfmt, "level": "PROMPT", "colorize":True, "filter": is_stdout_log},
|
2022-09-12 00:19:51 +02:00
|
|
|
{"sink": sys.stdout, "format": initfmt, "level": "INIT", "colorize":True, "filter": is_init_log},
|
|
|
|
{"sink": sys.stdout, "format": msgfmt, "level": "MESSAGE", "colorize":True, "filter": is_msg_log}
|
2022-09-11 22:59:19 +02:00
|
|
|
],
|
|
|
|
}
|
|
|
|
logger.configure(**config)
|