logger policy in env

This commit is contained in:
Benoît Sierro
2021-06-09 08:07:44 +02:00
parent af0bbbf82b
commit 4a401a5771
3 changed files with 42 additions and 30 deletions

View File

@@ -258,6 +258,7 @@ valid_variable = dict(
ENVIRON_KEY_BASE = "SCGENERATOR_" ENVIRON_KEY_BASE = "SCGENERATOR_"
PBAR_POLICY = ENVIRON_KEY_BASE + "PBAR_POLICY" PBAR_POLICY = ENVIRON_KEY_BASE + "PBAR_POLICY"
LOG_POLICY = ENVIRON_KEY_BASE + "LOG_POLICY"
TMP_FOLDER_KEY_BASE = ENVIRON_KEY_BASE + "SC_TMP_" TMP_FOLDER_KEY_BASE = ENVIRON_KEY_BASE + "SC_TMP_"
PREFIX_KEY_BASE = ENVIRON_KEY_BASE + "PREFIX_" PREFIX_KEY_BASE = ENVIRON_KEY_BASE + "PREFIX_"
PARAM_SEPARATOR = " " PARAM_SEPARATOR = " "

View File

@@ -1,8 +1,8 @@
import os import os
from pathlib import Path from pathlib import Path
from typing import Dict, List, Literal, Optional from typing import Dict, Literal, Optional, Set
from .const import ENVIRON_KEY_BASE, PBAR_POLICY, TMP_FOLDER_KEY_BASE from .const import ENVIRON_KEY_BASE, PBAR_POLICY, LOG_POLICY, TMP_FOLDER_KEY_BASE
def data_folder(task_id: int) -> Optional[Path]: def data_folder(task_id: int) -> Optional[Path]:
@@ -17,13 +17,25 @@ def all_environ() -> Dict[str, str]:
return d return d
def pbar_policy() -> List[Literal["print", "file"]]: def pbar_policy() -> Set[Literal["print", "file"]]:
policy = os.getenv(PBAR_POLICY) policy = os.getenv(PBAR_POLICY)
if policy == "print" or policy is None: if policy == "print" or policy is None:
return ["print"] return {"print"}
elif policy == "file": elif policy == "file":
return ["file"] return {"file"}
elif policy == "both": elif policy == "both":
return ["file", "print"] return {"file", "print"}
else: else:
return [] return set()
def log_policy() -> Set[Literal["print", "file"]]:
policy = os.getenv(LOG_POLICY)
if policy == "print" or policy is None:
return {"print"}
elif policy == "file":
return {"file"}
elif policy == "both":
return {"file", "print"}
else:
return set()

View File

@@ -1,18 +1,19 @@
import logging import logging
from typing import Optional from typing import Optional
from .env import log_policy
class DebugOnlyFileHandler(logging.FileHandler): # class DebugOnlyFileHandler(logging.FileHandler):
def __init__( # def __init__(
self, filename, mode: str, encoding: Optional[str] = None, delay: bool = False # self, filename, mode: str, encoding: Optional[str] = None, delay: bool = False
) -> None: # ) -> None:
super().__init__(filename, mode=mode, encoding=encoding, delay=delay) # super().__init__(filename, mode=mode, encoding=encoding, delay=delay)
self.setLevel(logging.DEBUG) # self.setLevel(logging.DEBUG)
def emit(self, record: logging.LogRecord) -> None: # def emit(self, record: logging.LogRecord) -> None:
if not record.levelno == logging.DEBUG: # if not record.levelno == logging.DEBUG:
return # return
return super().emit(record) # return super().emit(record)
DEFAULT_LEVEL = logging.INFO DEFAULT_LEVEL = logging.INFO
@@ -80,18 +81,16 @@ def configure_logger(logger):
updated logger updated logger
""" """
if not hasattr(logger, "already_configured"): if not hasattr(logger, "already_configured"):
if "file" in log_policy():
formatter = logging.Formatter("{levelname}: {name}: {message}", style="{") formatter = logging.Formatter("{levelname}: {name}: {message}", style="{")
file_handler1 = DebugOnlyFileHandler("sc-DEBUG.log", "a+") file_handler1 = logging.FileHandler("scgenerator.log", "a+")
file_handler1.setFormatter(formatter) file_handler1.setFormatter(formatter)
file_handler1.setLevel(logging.DEBUG)
logger.addHandler(file_handler1) logger.addHandler(file_handler1)
if "print" in log_policy():
file_handler2 = logging.FileHandler("sc-INFO.log", "a+")
file_handler2.setFormatter(formatter)
file_handler2.setLevel(logging.INFO)
logger.addHandler(file_handler2)
stream_handler = logging.StreamHandler() stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging.WARNING) stream_handler.setLevel(logging.INFO)
logger.addHandler(stream_handler) logger.addHandler(stream_handler)
logger.setLevel(logging.DEBUG) logger.setLevel(logging.DEBUG)