From a755508205066725d21186463518ccc9441b316d Mon Sep 17 00:00:00 2001 From: Jan Caha Date: Tue, 23 May 2023 11:12:44 +0200 Subject: [PATCH 1/4] remove doubled code --- dbsync_daemon.py | 68 ------------------------------------------------ 1 file changed, 68 deletions(-) diff --git a/dbsync_daemon.py b/dbsync_daemon.py index 21c9c4d..899a74e 100644 --- a/dbsync_daemon.py +++ b/dbsync_daemon.py @@ -48,73 +48,6 @@ def pyinstaller_path_fix() -> None: pyinstaller_update_path() -LOGGER: logging.Logger = None - - -def setup_logger( - log_path, - log_verbosity: str, - with_time=True, - with_level=True, -) -> logging.Logger: - global LOGGER - LOGGER = logging.getLogger(f"{log_path}") - if log_verbosity == "messages": - LOGGER.setLevel(logging.DEBUG) - elif log_verbosity == "errors": - LOGGER.setLevel(logging.WARNING) - else: - LOGGER.setLevel(logging.WARNING) - if not LOGGER.handlers: - log_handler = logging.FileHandler( - log_path, - mode="a", - ) - format = "%(asctime)s -" if with_time else "" - format += "%(levelname)s - %(message)s" if with_level else "%(message)s" - log_handler.setFormatter(logging.Formatter(format)) - LOGGER.addHandler(log_handler) - - -def handle_error( - error: typing.Union[ - Exception, - str, - ] -): - if LOGGER: - LOGGER.error(str(error)) - print( - "Error: " + str(error), - file=sys.stderr, - ) - sys.exit(1) - - -def handle_message( - msg: str, -): - if LOGGER: - LOGGER.debug(msg) - print(msg) - - -def is_pyinstaller() -> bool: - if getattr(sys, "frozen", False) and platform.system() == "Windows": - return True - return False - - -def pyinstaller_update_path() -> None: - path = pathlib.Path(__file__).parent / "lib" - os.environ["PATH"] += os.pathsep + path.as_posix() - - -def pyinstaller_path_fix() -> None: - if is_pyinstaller(): - pyinstaller_update_path() - - def setup_logger( log_path: pathlib.Path = None, log_verbosity: str = logging.DEBUG, with_time=True, with_level=True ) -> logging.Logger: @@ -148,7 +81,6 @@ def handle_error_and_exit(error: typing.Union[str, Exception]): def main(): - pyinstaller_path_fix() parser = argparse.ArgumentParser( From 7836af7aad65e9020246baf96d4c859b2ca8c9cf Mon Sep 17 00:00:00 2001 From: Jan Caha Date: Tue, 23 May 2023 11:42:11 +0200 Subject: [PATCH 2/4] move all log functions to separate file for clarify --- dbsync_daemon.py | 47 +++++++++-------------------------------- log_functions.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 37 deletions(-) create mode 100644 log_functions.py diff --git a/dbsync_daemon.py b/dbsync_daemon.py index 899a74e..be4edea 100644 --- a/dbsync_daemon.py +++ b/dbsync_daemon.py @@ -11,7 +11,6 @@ import logging import os import pathlib -import typing import dbsync from version import ( @@ -24,6 +23,8 @@ update_config_path, ) +from log_functions import setup_logger, handle_error_and_exit + def is_pyinstaller() -> bool: if ( @@ -48,38 +49,6 @@ def pyinstaller_path_fix() -> None: pyinstaller_update_path() -def setup_logger( - log_path: pathlib.Path = None, log_verbosity: str = logging.DEBUG, with_time=True, with_level=True -) -> logging.Logger: - log = logging.getLogger() - log.setLevel(logging.DEBUG) - - print_handler = logging.StreamHandler(stream=sys.stdout) - print_handler.setLevel(logging.DEBUG) - log.addHandler(print_handler) - - if log_path: - log_handler = logging.FileHandler(log_path, mode="a") - - if log_verbosity == "messages": - log_handler.setLevel(logging.DEBUG) - elif log_verbosity == "errors": - log_handler.setLevel(logging.WARNING) - else: - log_handler.setLevel(logging.WARNING) - - format = "%(asctime)s -" if with_time else "" - format += "%(levelname)s - %(message)s" if with_level else "%(message)s" - log_handler.setFormatter(logging.Formatter(format)) - - log.addHandler(log_handler) - - -def handle_error_and_exit(error: typing.Union[str, Exception]): - logging.error(str(error)) - sys.exit(1) - - def main(): pyinstaller_path_fix() @@ -119,11 +88,15 @@ def main(): parser.add_argument( "--log-verbosity", choices=[ - "errors", - "messages", + "DEBUG", + "INFO", + "WARNING", + "ERROR", + "FATAL", + "CRITICAL", ], - default="messages", - help="Log messages, not only errors.", + default="DEBUG", + help="Set level of logging into log-file.", ) args = parser.parse_args() diff --git a/log_functions.py b/log_functions.py new file mode 100644 index 0000000..35cbfdf --- /dev/null +++ b/log_functions.py @@ -0,0 +1,54 @@ +import logging +import pathlib +import sys +import typing + + +def filter_below_error(record): + """Only lets through log messages with log level below ERROR .""" + return record.levelno < logging.ERROR + + +def setup_logger( + log_path: pathlib.Path = None, log_verbosity: str = "DEBUG", with_time=True, with_level=True +) -> logging.Logger: + log = logging.getLogger() + log.setLevel(logging.DEBUG) + + print_handler = logging.StreamHandler(stream=sys.stdout) + print_handler.setLevel(logging.DEBUG) + print_handler.addFilter(filter_below_error) + log.addHandler(print_handler) + + print_error_handler = logging.StreamHandler(stream=sys.stderr) + print_error_handler.setLevel(logging.ERROR) + log.addHandler(print_error_handler) + + if log_path: + log_handler = logging.FileHandler(log_path, mode="a") + + log_handler.setLevel(log_verbosity_to_logging(log_verbosity)) + + format = "%(asctime)s -" if with_time else "" + format += "%(levelname)s - %(message)s" if with_level else "%(message)s" + log_handler.setFormatter(logging.Formatter(format)) + + log.addHandler(log_handler) + + +def log_verbosity_to_logging(verbosity: str): + data = { + "DEBUG": logging.DEBUG, + "INFO": logging.INFO, + "WARNING": logging.WARNING, + "ERROR": logging.ERROR, + "FATAL": logging.FATAL, + "CRITICAL": logging.CRITICAL, + } + + return data[verbosity] + + +def handle_error_and_exit(error: typing.Union[str, Exception]): + logging.error(str(error)) + sys.exit(1) From f4ed9ad4d7288b61635c062e04ad7e3e80c49af0 Mon Sep 17 00:00:00 2001 From: Jan Caha Date: Tue, 23 May 2023 11:54:04 +0200 Subject: [PATCH 3/4] copy new file to docker --- Dockerfile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 7fbbc3b..4362772 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,7 +13,7 @@ RUN apt-get update && apt-get install -y \ libsqlite3-dev \ python3-pip \ python3-psycopg2 \ - && rm -rf /var/lib/apt/lists/* + && rm -rf /var/lib/apt/lists/* # Python Mergin client RUN python3 -m pip install --upgrade pip @@ -39,6 +39,7 @@ COPY version.py . COPY config.py . COPY dbsync.py . COPY dbsync_daemon.py . +COPY log_functions.py . # base DB sync config file (the rest is configured with env variables) COPY config-dockerfile.yaml ./config.yaml From db293cb4c1fc729927b10e3078090dc2a0e7142f Mon Sep 17 00:00:00 2001 From: Jan Caha Date: Tue, 23 May 2023 12:34:13 +0200 Subject: [PATCH 4/4] all log messages go to stderr --- log_functions.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/log_functions.py b/log_functions.py index 35cbfdf..40125d7 100644 --- a/log_functions.py +++ b/log_functions.py @@ -15,13 +15,8 @@ def setup_logger( log = logging.getLogger() log.setLevel(logging.DEBUG) - print_handler = logging.StreamHandler(stream=sys.stdout) - print_handler.setLevel(logging.DEBUG) - print_handler.addFilter(filter_below_error) - log.addHandler(print_handler) - print_error_handler = logging.StreamHandler(stream=sys.stderr) - print_error_handler.setLevel(logging.ERROR) + print_error_handler.setLevel(logging.DEBUG) log.addHandler(print_error_handler) if log_path: