From 958d6b9f9bb067f44132ec982125ed028a80dae1 Mon Sep 17 00:00:00 2001 From: Dimitris Zlatanidis Date: Wed, 5 Apr 2023 22:03:16 +0300 Subject: [PATCH] Added logging config --- slpkg/configs.py | 13 ++++++------- slpkg/logging_config.py | 10 ++++++++++ slpkg/upgrade.py | 7 +++---- slpkg/utilities.py | 10 +++++----- 4 files changed, 24 insertions(+), 16 deletions(-) create mode 100644 slpkg/logging_config.py diff --git a/slpkg/configs.py b/slpkg/configs.py index 4ab8d105..f867f1d1 100644 --- a/slpkg/configs.py +++ b/slpkg/configs.py @@ -6,6 +6,7 @@ import tomli import platform from pathlib import Path from dataclasses import dataclass +from slpkg.logging_config import LoggingConfig class Load: @@ -46,8 +47,6 @@ class Configs: } prog_name: str = 'slpkg' - root_slpkg: Path = Path(Path.home(), f'.{prog_name}') - log_path: Path = Path(root_slpkg, 'logs') os_arch: str = platform.machine() tmp_path: str = '/tmp/' tmp_slpkg: Path = Path(tmp_path, prog_name) @@ -115,13 +114,13 @@ class Configs: # Creating the paths if not exists paths = [ - tmp_slpkg, - log_path, - build_path, - download_only_path, + db_path, lib_path, etc_path, - db_path, + build_path, + tmp_slpkg, + download_only_path, + LoggingConfig.log_path ] for path in paths: diff --git a/slpkg/logging_config.py b/slpkg/logging_config.py new file mode 100644 index 00000000..79d8cd9e --- /dev/null +++ b/slpkg/logging_config.py @@ -0,0 +1,10 @@ +#!/usr/bin/python3 +# -*- coding: utf-8 -*- + +from pathlib import Path + + +class LoggingConfig: + root_slpkg: Path = Path(Path.home(), '.slpkg') + log_path: Path = Path(root_slpkg, 'logs') + log_file: Path = Path(log_path, 'errors_slpkg.log') diff --git a/slpkg/upgrade.py b/slpkg/upgrade.py index 5a7c215e..b00bec7d 100644 --- a/slpkg/upgrade.py +++ b/slpkg/upgrade.py @@ -2,7 +2,6 @@ # -*- coding: utf-8 -*- import logging -from pathlib import Path from typing import Generator from packaging.version import parse, InvalidVersion @@ -10,6 +9,7 @@ from slpkg.configs import Configs from slpkg.utilities import Utilities from slpkg.sbos.queries import SBoQueries from slpkg.binaries.queries import BinQueries +from slpkg.logging_config import LoggingConfig class Upgrade(Configs): @@ -26,8 +26,7 @@ class Upgrade(Configs): self.all_installed: list = self.utils.installed_package_names - log_file: Path = Path(self.log_path, 'error_upgrade.log') - logging.basicConfig(filename=log_file, + logging.basicConfig(filename=LoggingConfig.log_file, encoding='utf-8', level=logging.DEBUG) @@ -72,6 +71,6 @@ class Upgrade(Configs): if parse(repo_version) == parse(inst_version) and parse(repo_build) > parse(inst_build): return True except InvalidVersion as err: - logging.error(err) + logging.error(f'{self.__class__.__name__}: {Upgrade.is_package_upgradeable.__name__}: {err}') return False diff --git a/slpkg/utilities.py b/slpkg/utilities.py index af6c87dc..579733a9 100644 --- a/slpkg/utilities.py +++ b/slpkg/utilities.py @@ -14,6 +14,7 @@ from slpkg.blacklist import Blacklist from slpkg.sbos.queries import SBoQueries from slpkg.repositories import Repositories from slpkg.binaries.queries import BinQueries +from slpkg.logging_config import LoggingConfig class Utilities: @@ -36,8 +37,7 @@ class Utilities: self.installed_packages: list = list(self.all_installed()) self.installed_package_names: list = list(self.all_installed_names()) - log_file: Path = Path(self.configs.log_path, 'error_utilities.log') - logging.basicConfig(filename=log_file, + logging.basicConfig(filename=LoggingConfig.log_file, encoding='utf-8', level=logging.DEBUG) @@ -61,8 +61,8 @@ class Utilities: if not name.startswith('.') and name not in self.black.packages(): yield file.name - except ValueError: - pass + except ValueError as err: + logging.error(f'{self.__class__.__name__}: {Utilities.all_installed.__name__}: {err}') def all_installed_names(self) -> Generator: """ Return all installed packages names from /val/log/packages folder. """ @@ -75,7 +75,7 @@ class Utilities: if not name.startswith('.') and name not in self.black.packages(): yield name except ValueError as err: - logging.error(f'all_installed_names: {err}') + logging.error(f'{self.__class__.__name__}: {Utilities.all_installed_names.__name__}: {err}') @staticmethod def remove_file_if_exists(path: Path, file: str) -> None: