logWrite replaced with logging module

This commit is contained in:
2023-06-23 11:25:27 +02:00
parent 23467a88ef
commit 4b43e76822
5 changed files with 50 additions and 27 deletions

View File

@@ -1,8 +1,11 @@
import logging
from smtplib import SMTP, SMTP_SSL
from ssl import create_default_context
from traceback import print_exc
from modules.utils import configGet, logWrite
from modules.utils import configGet
logger = logging.getLogger(__name__)
try:
if configGet("use_ssl", "mailer", "smtp") is True:
@@ -10,7 +13,7 @@ try:
configGet("host", "mailer", "smtp"),
configGet("port", "mailer", "smtp"),
)
logWrite(f"Initialized SMTP SSL connection")
logger.info("Initialized SMTP SSL connection")
elif configGet("use_tls", "mailer", "smtp") is True:
mail_sender = SMTP(
configGet("host", "mailer", "smtp"),
@@ -18,21 +21,21 @@ try:
)
mail_sender.starttls(context=create_default_context())
mail_sender.ehlo()
logWrite(f"Initialized SMTP TLS connection")
logger.info("Initialized SMTP TLS connection")
else:
mail_sender = SMTP(
configGet("host", "mailer", "smtp"), configGet("port", "mailer", "smtp")
)
mail_sender.ehlo()
logWrite(f"Initialized SMTP connection")
logger.info("Initialized SMTP connection")
except Exception as exp:
logWrite(f"Could not initialize SMTP connection to: {exp}")
logger.error("Could not initialize SMTP connection to: %s", exp)
print_exc()
try:
mail_sender.login(
configGet("login", "mailer", "smtp"), configGet("password", "mailer", "smtp")
)
logWrite(f"Successfully initialized mailer")
logger.info("Successfully initialized mailer")
except Exception as exp:
logWrite(f"Could not login into provided SMTP account due to: {exp}")
logger.error("Could not login into provided SMTP account due to: %s", exp)

View File

@@ -1,15 +1,11 @@
import logging
from pathlib import Path
from traceback import print_exc
from traceback import format_exc
from typing import Any, Union
from ujson import JSONDecodeError, dumps, loads
# Print to stdout and then to log
def logWrite(message: str, debug: bool = False) -> None:
# save to log file and rotation is to be done
# logAppend(f'{message}', debug=debug)
print(f"{message}", flush=True)
logger = logging.getLogger(__name__)
def jsonLoad(filepath: Union[str, Path]) -> Any:
@@ -25,13 +21,17 @@ def jsonLoad(filepath: Union[str, Path]) -> Any:
try:
output = loads(file.read())
except JSONDecodeError:
logWrite(
f"Could not load json file {filepath}: file seems to be incorrect!\n{print_exc()}"
logger.error(
"Could not load json file %s: file seems to be incorrect!\n%s",
filepath,
format_exc(),
)
raise
except FileNotFoundError:
logWrite(
f"Could not load json file {filepath}: file does not seem to exist!\n{print_exc()}"
logger.error(
"Could not load json file %s: file does not seem to exist!\n%s",
filepath,
format_exc(),
)
raise
file.close()
@@ -50,7 +50,7 @@ def jsonSave(contents: Union[list, dict], filepath: Union[str, Path]) -> None:
file.write(dumps(contents, ensure_ascii=False, indent=4))
file.close()
except Exception as exp:
logWrite(f"Could not save json file {filepath}: {exp}\n{print_exc()}")
logger.error("Could not save json file %s: %s\n%s", filepath, exp, format_exc())
return