Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
115 changes: 10 additions & 105 deletions dbsync_daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import logging
import os
import pathlib
import typing

import dbsync
from version import (
Expand All @@ -24,6 +23,8 @@
update_config_path,
)

from log_functions import setup_logger, handle_error_and_exit


def is_pyinstaller() -> bool:
if (
Expand All @@ -48,107 +49,7 @@ 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:
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()

parser = argparse.ArgumentParser(
Expand Down Expand Up @@ -187,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()
Expand Down
49 changes: 49 additions & 0 deletions log_functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
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_error_handler = logging.StreamHandler(stream=sys.stderr)
print_error_handler.setLevel(logging.DEBUG)
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)