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
|
|
|
|
|
|
|
## 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
|
|
|
## Get log level and file from environment if possible
|
2022-05-06 07:04:51 +00:00
|
|
|
env_log_level = os.environ.get('LOG_LEVEL', 'INFO').upper()
|
|
|
|
|
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
|
|
|
|
|
|
|
## Make sure the level from the environment is valid
|
|
|
|
try:
|
|
|
|
log_level = getattr(logging, env_log_level)
|
|
|
|
|
|
|
|
except AttributeError:
|
|
|
|
log_level = logging.INFO
|
|
|
|
|
|
|
|
|
|
|
|
## 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(
|
|
|
|
level = log_level,
|
|
|
|
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
|
|
|
)
|