From d5a397545d4e2ee0090cf0e5a9761f7b7649be5d Mon Sep 17 00:00:00 2001 From: Bastian Fredriksson Date: Fri, 18 Oct 2024 16:02:36 +0200 Subject: [PATCH] fix: only log a message once New handlers were registered every time the get_logger function was called, which caused the same log line to be printed multiple times in the log. Initialise the logger once and just let get_logger return a reference instead. --- pyproject.toml | 2 +- src/fish_ai/engine.py | 43 ++++++++++++++++++++++--------------------- 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 260132a..5542ec7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "fish_ai" -version = "0.13.1" +version = "0.13.2" authors = [{ name = "Bastian Fredriksson", email = "realiserad@gmail.com" }] description = "Provides core functionality for fish-ai, an AI plugin for the fish shell." readme = "README.md" diff --git a/src/fish_ai/engine.py b/src/fish_ai/engine.py index f4e86d6..148dd47 100644 --- a/src/fish_ai/engine.py +++ b/src/fish_ai/engine.py @@ -25,34 +25,35 @@ from os import access, R_OK from re import match +logger = logging.getLogger() -def get_args(): - return list.copy(sys.argv[1:]) +if path.exists('/dev/log'): + # Syslog on Linux + handler = SysLogHandler(address='/dev/log') + logger.addHandler(handler) +elif path.exists('/var/run/syslog'): + # Syslog on macOS + handler = SysLogHandler(address='/var/run/syslog') + logger.addHandler(handler) + +if get_config('log'): + handler = RotatingFileHandler(path.expanduser(get_config('log')), + backupCount=0, + maxBytes=1024*1024) + logger.addHandler(handler) + +if get_config('debug') == 'True': + logger.setLevel(logging.DEBUG) def get_logger(): - logger = logging.getLogger() - - if path.exists('/dev/log'): - # Syslog on Linux - handler = SysLogHandler(address='/dev/log') - logger.addHandler(handler) - elif path.exists('/var/run/syslog'): - # Syslog on macOS - handler = SysLogHandler(address='/var/run/syslog') - logger.addHandler(handler) - - if get_config('log'): - handler = RotatingFileHandler(path.expanduser(get_config('log')), - backupCount=0, - maxBytes=1024*1024) - logger.addHandler(handler) - - if get_config('debug') == 'True': - logger.setLevel(logging.DEBUG) return logger +def get_args(): + return list.copy(sys.argv[1:]) + + def get_os(): if platform.system() == 'Linux': if isfile('/etc/os-release'):