added console logging
This commit is contained in:
+13
-3
@@ -35,15 +35,16 @@ class PrefixMiddleware:
|
||||
def configure_logging(app: Flask) -> None:
|
||||
log_path = app.config["LOG_FILE"]
|
||||
log_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
formatter = logging.Formatter(
|
||||
"%(asctime)s %(levelname)s %(name)s [%(threadName)s] %(message)s"
|
||||
)
|
||||
handler = RotatingFileHandler(
|
||||
log_path,
|
||||
maxBytes=app.config["LOG_MAX_BYTES"],
|
||||
backupCount=app.config["LOG_BACKUP_COUNT"],
|
||||
encoding="utf-8",
|
||||
)
|
||||
handler.setFormatter(logging.Formatter(
|
||||
"%(asctime)s %(levelname)s %(name)s [%(threadName)s] %(message)s"
|
||||
))
|
||||
handler.setFormatter(formatter)
|
||||
root = logging.getLogger()
|
||||
root.setLevel(app.config["LOG_LEVEL"])
|
||||
# Avoid duplicate handlers when an app factory is called repeatedly in tests.
|
||||
@@ -57,6 +58,15 @@ def configure_logging(app: Flask) -> None:
|
||||
else:
|
||||
handler.close()
|
||||
|
||||
if app.config["LOG_CONSOLE"] and not any(
|
||||
isinstance(item, logging.StreamHandler)
|
||||
and not isinstance(item, logging.FileHandler)
|
||||
for item in root.handlers
|
||||
):
|
||||
console = logging.StreamHandler()
|
||||
console.setFormatter(formatter)
|
||||
root.addHandler(console)
|
||||
|
||||
def uncaught_exception(exception_type, exception, traceback):
|
||||
if issubclass(exception_type, KeyboardInterrupt):
|
||||
return sys.__excepthook__(exception_type, exception, traceback)
|
||||
|
||||
@@ -25,6 +25,7 @@ class Config:
|
||||
LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO").upper()
|
||||
LOG_MAX_BYTES = int(os.getenv("LOG_MAX_BYTES", str(5 * 1024 * 1024)))
|
||||
LOG_BACKUP_COUNT = int(os.getenv("LOG_BACKUP_COUNT", "5"))
|
||||
LOG_CONSOLE = _bool("LOG_CONSOLE", True)
|
||||
NETATMO_TOKEN_FILE = Path(
|
||||
os.getenv("NETATMO_TOKEN_FILE", str(RRD_FOLDER / "netatmo_tokens.json"))
|
||||
).expanduser().resolve()
|
||||
|
||||
Reference in New Issue
Block a user