50 lines
960 B
Python
50 lines
960 B
Python
import logging
|
|
import os
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
LEVELS = {
|
|
'critical': logging.CRITICAL,
|
|
'error': logging.ERROR,
|
|
'warning': logging.WARNING,
|
|
'info': logging.INFO,
|
|
'verbose': 15,
|
|
'debug': logging.DEBUG
|
|
}
|
|
|
|
|
|
## 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')
|
|
|
|
try:
|
|
env_log_file = Path(os.environ.get('LOG_FILE')).expanduser().resolve()
|
|
|
|
except TypeError:
|
|
env_log_file = None
|
|
|
|
|
|
## Set logging config
|
|
handlers = [logging.StreamHandler()]
|
|
|
|
if env_log_file:
|
|
handlers.append(logging.FileHandler(env_log_file))
|
|
|
|
logging.basicConfig(
|
|
level = logging.INFO,
|
|
format = "[%(asctime)s] %(levelname)s: %(message)s",
|
|
handlers = handlers
|
|
)
|
|
|
|
|
|
def set_level(level):
|
|
logging.getLogger().setLevel(LEVELS[level.lower()])
|