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_"
PBAR_POLICY = ENVIRON_KEY_BASE + "PBAR_POLICY"
LOG_POLICY = ENVIRON_KEY_BASE + "LOG_POLICY"
TMP_FOLDER_KEY_BASE = ENVIRON_KEY_BASE + "SC_TMP_"
PREFIX_KEY_BASE = ENVIRON_KEY_BASE + "PREFIX_"
PARAM_SEPARATOR = " "

View File

@@ -1,8 +1,8 @@
import os
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]:
@@ -17,13 +17,25 @@ def all_environ() -> Dict[str, str]:
return d
def pbar_policy() -> List[Literal["print", "file"]]:
def pbar_policy() -> Set[Literal["print", "file"]]:
policy = os.getenv(PBAR_POLICY)
if policy == "print" or policy is None:
return ["print"]
return {"print"}
elif policy == "file":
return ["file"]
return {"file"}
elif policy == "both":
return ["file", "print"]
return {"file", "print"}
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
from typing import Optional
from .env import log_policy
class DebugOnlyFileHandler(logging.FileHandler):
def __init__(
self, filename, mode: str, encoding: Optional[str] = None, delay: bool = False
) -> None:
super().__init__(filename, mode=mode, encoding=encoding, delay=delay)
self.setLevel(logging.DEBUG)
# class DebugOnlyFileHandler(logging.FileHandler):
# def __init__(
# self, filename, mode: str, encoding: Optional[str] = None, delay: bool = False
# ) -> None:
# super().__init__(filename, mode=mode, encoding=encoding, delay=delay)
# self.setLevel(logging.DEBUG)
def emit(self, record: logging.LogRecord) -> None:
if not record.levelno == logging.DEBUG:
return
return super().emit(record)
# def emit(self, record: logging.LogRecord) -> None:
# if not record.levelno == logging.DEBUG:
# return
# return super().emit(record)
DEFAULT_LEVEL = logging.INFO
@@ -80,19 +81,17 @@ def configure_logger(logger):
updated logger
"""
if not hasattr(logger, "already_configured"):
formatter = logging.Formatter("{levelname}: {name}: {message}", style="{")
file_handler1 = DebugOnlyFileHandler("sc-DEBUG.log", "a+")
file_handler1.setFormatter(formatter)
logger.addHandler(file_handler1)
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.setLevel(logging.WARNING)
logger.addHandler(stream_handler)
if "file" in log_policy():
formatter = logging.Formatter("{levelname}: {name}: {message}", style="{")
file_handler1 = logging.FileHandler("scgenerator.log", "a+")
file_handler1.setFormatter(formatter)
file_handler1.setLevel(logging.DEBUG)
logger.addHandler(file_handler1)
if "print" in log_policy():
stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging.INFO)
logger.addHandler(stream_handler)
logger.setLevel(logging.DEBUG)
logger.already_configured = True