From 59a05224ff56d3e15e465a21f323d59e1c1ad4cb Mon Sep 17 00:00:00 2001 From: Izalia Mae Date: Fri, 12 Aug 2022 03:22:30 -0400 Subject: [PATCH] add LOG_FILE env var --- relay/logger.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/relay/logger.py b/relay/logger.py index 6351bfc..166cbf2 100644 --- a/relay/logger.py +++ b/relay/logger.py @@ -1,6 +1,8 @@ import logging import os +from pathlib import Path + ## Add the verbose logging level def verbose(message, *args, **kwargs): @@ -14,9 +16,15 @@ setattr(logging, 'VERBOSE', 15) 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() +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 try: @@ -27,8 +35,13 @@ except AttributeError: ## Set logging config +handlers = [logging.StreamHandler()] + +if env_log_file: + handlers.append(logging.FileHandler(env_log_file)) + logging.basicConfig( level = log_level, format = "[%(asctime)s] %(levelname)s: %(message)s", - handlers = [logging.StreamHandler()] + handlers = handlers )