add LOG_FILE env var

This commit is contained in:
Izalia Mae 2022-08-12 03:22:30 -04:00
parent fc7de1a3bc
commit 59a05224ff

View file

@ -1,6 +1,8 @@
import logging import logging
import os import os
from pathlib import Path
## Add the verbose logging level ## Add the verbose logging level
def verbose(message, *args, **kwargs): def verbose(message, *args, **kwargs):
@ -14,9 +16,15 @@ setattr(logging, 'VERBOSE', 15)
logging.addLevelName(15, 'VERBOSE') logging.addLevelName(15, 'VERBOSE')
## Get log level from environment if possible ## Get log level and file from environment if possible
env_log_level = os.environ.get('LOG_LEVEL', 'INFO').upper() env_log_level = os.environ.get('LOG_LEVEL', 'INFO').upper()
try:
env_log_file = Path(os.environ.get('LOG_FILE')).expanduser().resolve()
except TypeError:
env_log_file = None
## Make sure the level from the environment is valid ## Make sure the level from the environment is valid
try: try:
@ -27,8 +35,13 @@ except AttributeError:
## Set logging config ## Set logging config
handlers = [logging.StreamHandler()]
if env_log_file:
handlers.append(logging.FileHandler(env_log_file))
logging.basicConfig( logging.basicConfig(
level = log_level, level = log_level,
format = "[%(asctime)s] %(levelname)s: %(message)s", format = "[%(asctime)s] %(levelname)s: %(message)s",
handlers = [logging.StreamHandler()] handlers = handlers
) )