Skip to content

Commit

Permalink
Implement syslog functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
antonplagemann committed Dec 27, 2021
1 parent fdd0dfd commit 4bf4ce0
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,9 @@ MONICA_LABELS_EXCLUDE=
DATABASE_FILE=data/syncState.db
GOOGLE_TOKEN_FILE=data/token.pickle
GOOGLE_CREDENTIALS_FILE=data/credentials.json

# Send messages to a syslog server
# An alternative to providing target host and port is providing only a target address, for example "/dev/log".
# In this case, a Unix domain socket is used to send the message to the syslog.
SYSLOG_TARGET=
SYSLOG_PORT=
19 changes: 16 additions & 3 deletions GMSync.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import argparse
import logging
import logging.handlers
import os
import sys
from os.path import abspath, join
from typing import Tuple, Union

from dotenv import dotenv_values, find_dotenv # type: ignore
from dotenv.main import set_key # type: ignore
Expand All @@ -14,7 +16,7 @@
from helpers.MonicaHelper import Monica
from helpers.SyncHelper import Sync

VERSION = "v4.0.0"
VERSION = "v4.1.0"
LOG_FOLDER = "logs"
LOG_FILENAME = "sync.log"
DEFAULT_CONFIG_FILEPATH = join("helpers", ".env.default")
Expand All @@ -39,6 +41,17 @@ def main(self) -> None:
# Load config
self.load_config()

# Create syslog handler
if self.conf.SYSLOG_TARGET:
address: Union[Tuple[str, int], str] = (
(self.conf.SYSLOG_TARGET, int(self.conf.SYSLOG_PORT))
if self.conf.SYSLOG_PORT
else self.conf.SYSLOG_TARGET
)
syslog_handler = logging.handlers.SysLogHandler(address=address)
syslog_handler.setFormatter(self.logging_format)
self.log.addHandler(syslog_handler)

# Create sync object
self.create_sync_helper()

Expand Down Expand Up @@ -94,11 +107,11 @@ def create_logger(self) -> None:
log = logging.getLogger("GMSync")
dotenv_log = logging.getLogger("dotenv.main")
log.setLevel(logging.INFO)
logging_format = logging.Formatter("%(asctime)s %(levelname)s %(message)s")
self.logging_format = logging.Formatter("%(asctime)s %(levelname)s %(message)s")
log_filepath = join(LOG_FOLDER, LOG_FILENAME)
handler = logging.FileHandler(filename=log_filepath, mode="a", encoding="utf8")
handler.setLevel(logging.INFO)
handler.setFormatter(logging_format)
handler.setFormatter(self.logging_format)
log.addHandler(handler)
dotenv_log.addHandler(handler)
self.log = log
Expand Down
6 changes: 6 additions & 0 deletions helpers/.env.default
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,9 @@ MONICA_LABELS_EXCLUDE=
DATABASE_FILE=data/syncState.db
GOOGLE_TOKEN_FILE=data/token.pickle
GOOGLE_CREDENTIALS_FILE=data/credentials.json

# Send messages to a syslog server
# An alternative to providing target host and port is providing only a target address, for example "/dev/log".
# In this case, a Unix domain socket is used to send the message to the syslog.
SYSLOG_TARGET=
SYSLOG_PORT=
2 changes: 2 additions & 0 deletions helpers/ConfigHelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ def __init__(self, log: Logger, raw_config: Dict[str, Union[str, None]]) -> None
self.DATABASE_FILE = abspath(self._values["DATABASE_FILE"])
self.GOOGLE_TOKEN_FILE = abspath(self._values["GOOGLE_TOKEN_FILE"])
self.GOOGLE_CREDENTIALS_FILE = abspath(self._values["GOOGLE_CREDENTIALS_FILE"])
self.SYSLOG_TARGET = self._values["SYSLOG_TARGET"]
self.SYSLOG_PORT = self._values["SYSLOG_PORT"]
except Exception as e:
raise ConfigError("Error parsing config, check syntax and required args!") from e

Expand Down
7 changes: 4 additions & 3 deletions test/SetupToken.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@

import requests
from bs4 import BeautifulSoup # type: ignore
from dotenv.main import load_dotenv, set_key # type: ignore
from dotenv.main import dotenv_values, set_key # type: ignore
from requests import ConnectionError, ConnectTimeout, ReadTimeout

LOG_FOLDER = "logs"
LOG_FILENAME = "setup.log"
try:
load_dotenv()
PROTOCOL, HOST, PORT = re.findall(r"(https?)://(.+?):(\d+)/api/?", os.getenv("BASE_URL", ""))[0]
config = dotenv_values(".env")
result = re.findall(r"(https?)://(.+?):(\d+)/api/?", str(config.get("BASE_URL", "")))
PROTOCOL, HOST, PORT = result[0]
except IndexError:
PROTOCOL, HOST, PORT = "http", "localhost", 8080
ENV_FILE = ".env"
Expand Down

0 comments on commit 4bf4ce0

Please sign in to comment.