sedi-relay/relay/logger.py

50 lines
960 B
Python
Raw Normal View History

2022-05-06 07:04:51 +00:00
import logging
import os
2022-08-12 07:22:30 +00:00
from pathlib import Path
2022-05-06 07:04:51 +00:00
2022-12-14 10:31:45 +00:00
LEVELS = {
'critical': logging.CRITICAL,
'error': logging.ERROR,
'warning': logging.WARNING,
'info': logging.INFO,
'verbose': 15,
'debug': logging.DEBUG
}
2022-05-06 07:04:51 +00:00
## Add the verbose logging level
def verbose(message, *args, **kwargs):
if not logging.root.isEnabledFor(logging.VERBOSE):
return
logging.log(logging.VERBOSE, message, *args, **kwargs)
setattr(logging, 'verbose', verbose)
setattr(logging, 'VERBOSE', 15)
logging.addLevelName(15, 'VERBOSE')
2022-08-12 07:22:30 +00:00
try:
env_log_file = Path(os.environ.get('LOG_FILE')).expanduser().resolve()
except TypeError:
env_log_file = None
2022-05-06 07:04:51 +00:00
## Set logging config
2022-08-12 07:22:30 +00:00
handlers = [logging.StreamHandler()]
if env_log_file:
handlers.append(logging.FileHandler(env_log_file))
2022-05-06 07:04:51 +00:00
logging.basicConfig(
2022-12-14 10:31:45 +00:00
level = logging.INFO,
2022-05-06 07:04:51 +00:00
format = "[%(asctime)s] %(levelname)s: %(message)s",
2022-08-12 07:22:30 +00:00
handlers = handlers
2022-05-06 07:04:51 +00:00
)
2022-12-14 10:31:45 +00:00
def set_level(level):
logging.getLogger().setLevel(LEVELS[level.lower()])