diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 8ae77b2..29500c5 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -143,6 +143,7 @@ repos: files: \.pyi?$ args: ["--skip=B101"] # skip assert-use warning + # - repo: https://github.com/RobertCraigie/pyright-python # rev: v1.1.404 # hooks: @@ -150,13 +151,13 @@ repos: # name: "🐍 pyright - Static type checker" # files: \.pyi?$ - - repo: local - hooks: - - id: pyright - name: "🐍 pyright (local) - Static type checker" - language: system - entry: "python -m pyright" - files: \.pyi?$ + # - repo: local + # hooks: + # - id: pyright + # name: "🐍 pyright (local) - Static type checker" + # language: system + # entry: "python -m pyright" + # files: \.pyi?$ # --- Tests --- # - repo: local diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a43349..c8e8702 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,5 +10,3 @@ **Full Changelog**: https://github.com/bybatkhuu/module-python-logging/compare/v6.0.0...v6.0.1 - - diff --git a/README.md b/README.md index e8cadb5..a755ea3 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ It is a `Loguru` based custom logging package for python projects. [OPTIONAL] For **DEVELOPMENT** environment: - Install [**git**](https://git-scm.com/downloads) -- Setup an [**SSH key**](https://docs.github.com/en/github/authenticating-to-github/connecting-to-github-with-ssh) ([video tutorial](https://www.youtube.com/watch?v=snCP3c7wXw0)) +- Setup an [**SSH key**](https://docs.github.com/en/github/authenticating-to-github/connecting-to-github-with-ssh) ### 2. 📥 Download or clone the repository @@ -151,7 +151,7 @@ logger.info("Logging info.") ### **Simple** -[**`configs/logger.yml`**](https://github.com/bybatkhuu/module-python-logging/blob/main/examples/simple/configs/logger.yml): +[**`configs/logger.yml`**](./examples/simple/configs/logger.yml): ```yml logger: @@ -164,9 +164,11 @@ logger: enabled: true ``` -[**`main.py`**](https://github.com/bybatkhuu/module-python-logging/blob/main/examples/simple/main.py): +[**`main.py`**](./examples/simple/main.py): ```python +#!/usr/bin/env python + from beans_logging.auto import logger @@ -178,10 +180,12 @@ logger.warning("Warning something.") logger.error("Error occured.") logger.critical("CRITICAL ERROR.") + def divide(a, b): _result = a / b return _result + def nested(c): try: divide(5, c) @@ -189,13 +193,14 @@ def nested(c): logger.error(err) raise + try: nested(0) -except Exception as err: +except Exception: logger.exception("Show me, what value is wrong:") ``` -Run the [**`examples/simple`**](https://github.com/bybatkhuu/module-python-logging/tree/main/examples/simple): +Run the [**`examples/simple`**](./examples/simple): ```sh cd ./examples/simple @@ -241,7 +246,7 @@ ZeroDivisionError: division by zero ## ⚙️ Configuration -[**`templates/configs/config.yml`**](./templates/configs/config.yml): +[**`templates/configs/logger.yml`**](./templates/configs/logger.yml): ```yaml logger: diff --git a/docs/getting-started/configuration.md b/docs/getting-started/configuration.md index ed2b78c..96bfc76 100644 --- a/docs/getting-started/configuration.md +++ b/docs/getting-started/configuration.md @@ -4,10 +4,10 @@ title: Configuration # ⚙️ Configuration -[**`templates/configs/config.yml`**](https://github.com/bybatkhuu/module-python-logging/blob/main/templates/configs/config.yml): +[**`templates/configs/logger.yml`**](https://github.com/bybatkhuu/module-python-logging/blob/main/templates/configs/logger.yml): ```yaml ---8<-- "./templates/configs/config.yml" +--8<-- "./templates/configs/logger.yml" ``` ## 🌎 Environment Variables diff --git a/docs/getting-started/prerequisites.md b/docs/getting-started/prerequisites.md index 95739c9..dad0a5b 100644 --- a/docs/getting-started/prerequisites.md +++ b/docs/getting-started/prerequisites.md @@ -12,4 +12,4 @@ title: Prerequisites [OPTIONAL] For **DEVELOPMENT** environment: - Install [**git**](https://git-scm.com/downloads) -- Setup an [**SSH key**](https://docs.github.com/en/github/authenticating-to-github/connecting-to-github-with-ssh) ([video tutorial](https://www.youtube.com/watch?v=snCP3c7wXw0)) +- Setup an [**SSH key**](https://docs.github.com/en/github/authenticating-to-github/connecting-to-github-with-ssh) diff --git a/docs/release-notes.md b/docs/release-notes.md index 7eb463b..7311218 100644 --- a/docs/release-notes.md +++ b/docs/release-notes.md @@ -16,5 +16,3 @@ hide: **Full Changelog**: https://github.com/bybatkhuu/module-python-logging/compare/v6.0.0...v6.0.1 - - diff --git a/mkdocs.yml b/mkdocs.yml index 8e1557a..e0da874 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -1,5 +1,5 @@ site_name: Python Logging (beans-logging) -site_description: "'beans-logging' is a python package for simple logger and easily managing logging." +site_description: "'beans-logging' is a python package for simple logger and easily managing logs." site_url: https://pylogging-docs.bybatkhuu.dev repo_name: bybatkhuu/module-python-logging repo_url: https://github.com/bybatkhuu/module-python-logging diff --git a/pyproject.toml b/pyproject.toml index 8896b14..44b7e30 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -5,7 +5,7 @@ build-backend = "setuptools.build_meta" [project] name = "beans_logging" authors = [{ name = "Batkhuu Byambajav", email = "batkhuu10@gmail.com" }] -description = "'beans-logging' is a python package for simple logger and easily managing logging." +description = "'beans-logging' is a python package for simple logger and easily managing logs." readme = "README.md" requires-python = ">=3.10,<4.0" keywords = [ diff --git a/src/beans_logging/__init__.py b/src/beans_logging/__init__.py index 19905f8..c6f5dbe 100644 --- a/src/beans_logging/__init__.py +++ b/src/beans_logging/__init__.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - from ._base import Logger, logger, LoggerLoader from .schemas import LoggerConfigPM from ._consts import WarnEnum diff --git a/src/beans_logging/_base.py b/src/beans_logging/_base.py index 128f198..1a1f27c 100644 --- a/src/beans_logging/_base.py +++ b/src/beans_logging/_base.py @@ -1,13 +1,11 @@ -# -*- coding: utf-8 -*- - -## Standard libraries +# Standard libraries import os import copy import json import logging -from typing import Union, Dict, Any +from typing import Any -## Third-party libraries +# Third-party libraries import yaml from loguru import logger from loguru._logger import Logger @@ -18,7 +16,7 @@ else: from pydantic import validate_arguments as validate_call -## Internal modules +# Internal modules from ._utils import create_dir, deep_merge from ._handlers import InterceptHandler from .rotation import RotationChecker @@ -67,7 +65,7 @@ class LoggerLoader: @validate_call def __init__( self, - config: Union[LoggerConfigPM, Dict[str, Any], None] = None, + config: LoggerConfigPM | dict[str, Any] | None = None, config_file_path: str = _CONFIG_FILE_PATH, auto_config_file: bool = True, auto_load: bool = False, @@ -75,12 +73,14 @@ def __init__( """LoggerLoader constructor method. Args: - config (Union[LoggerConfigPM, - dict, - None ], optional): New logger config to update loaded config. Defaults to None. - config_file_path (str , optional): Logger config file path. Defaults to `LoggerLoader._CONFIG_FILE_PATH`. - auto_config_file (bool , optional): Indicates whether to load logger config file or not. Defaults to True. - auto_load (bool , optional): Indicates whether to load logger handlers or not. Defaults to False. + config (LoggerConfigPM | dict | None], optional): New logger config to update loaded config. + Defaults to None. + config_file_path (str , optional): Logger config file path. Defaults to + `LoggerLoader._CONFIG_FILE_PATH`. + auto_config_file (bool , optional): Indicates whether to load logger config + file or not. Defaults to True. + auto_load (bool , optional): Indicates whether to load logger + handlers or not. Defaults to False. """ self.handlers_map = {"default": 0} @@ -125,9 +125,7 @@ def load(self) -> Logger: return logger @validate_call - def remove_handler( - self, handler: Union[str, None] = None, handler_type: str = "NAME" - ): + def remove_handler(self, handler: str | None = None, handler_type: str = "NAME"): """Remove all handlers or specific handler by name or id from logger. Raises: @@ -162,7 +160,7 @@ def remove_handler( self.handlers_map.clear() @validate_call - def update_config(self, config: Union[LoggerConfigPM, Dict[str, Any]]): + def update_config(self, config: LoggerConfigPM | dict[str, Any]): """Update logger config with new config. Args: @@ -213,13 +211,11 @@ def _load_config_file(self): # elif self.config_file_path.lower().endswith(".toml"): # _file_format = "TOML" - ## Loading config from file, if it's exits: + # Loading config from file, if it's exits: if os.path.isfile(self.config_file_path): if _file_format == "YAML": try: - with open( - self.config_file_path, "r", encoding="utf-8" - ) as _config_file: + with open(self.config_file_path, encoding="utf-8") as _config_file: _new_config_dict = yaml.safe_load(_config_file) or {} if "logger" not in _new_config_dict: logger.warning( @@ -242,9 +238,7 @@ def _load_config_file(self): raise elif _file_format == "JSON": try: - with open( - self.config_file_path, "r", encoding="utf-8" - ) as _config_file: + with open(self.config_file_path, encoding="utf-8") as _config_file: _new_config_dict = json.load(_config_file) or {} if "logger" not in _new_config_dict: logger.warning( @@ -296,7 +290,7 @@ def _load_config_file(self): def _check_env(self): """Check environment variables for logger config.""" - ## Checking environment for DEBUG option: + # Checking environment for DEBUG option: _is_debug = False _ENV = str(os.getenv("ENV")).strip().lower() _DEBUG = str(os.getenv("DEBUG")).strip().lower() @@ -314,7 +308,7 @@ def _check_env(self): self.config.file.logs_dir = os.getenv("BEANS_LOGGING_LOGS_DIR") # if self.config.stream.use_color: - # ## Checking terminal could support xterm colors: + # # Checking terminal could support xterm colors: # _TERM = str(os.getenv("TERM")).strip() # if not "xterm" in _TERM: # self.config.stream.use_color = False @@ -536,7 +530,7 @@ def _load_intercept_handlers(self): _intercept_handler = InterceptHandler() - ## Intercepting all logs from standard (root logger) logging: + # Intercepting all logs from standard (root logger) logging: logging.basicConfig(handlers=[_intercept_handler], level=0, force=True) _intercepted_modules = set() @@ -579,10 +573,10 @@ def _load_intercept_handlers(self): f"Intercepted modules: {list(_intercepted_modules)}; Muted modules: {list(_muted_modules)};" ) - ### ATTRIBUTES ### - ## handlers_map ## + # ATTRIBUTES # + # handlers_map @property - def handlers_map(self) -> Dict[str, int]: + def handlers_map(self) -> dict[str, int]: try: return self.__handlers_map except AttributeError: @@ -591,7 +585,7 @@ def handlers_map(self) -> Dict[str, int]: return self.__handlers_map @handlers_map.setter - def handlers_map(self, handlers_map: Dict[str, int]): + def handlers_map(self, handlers_map: dict[str, int]): if not isinstance(handlers_map, dict): raise TypeError( f"`handlers_map` attribute type {type(handlers_map)} is invalid, must be !." @@ -599,9 +593,9 @@ def handlers_map(self, handlers_map: Dict[str, int]): self.__handlers_map = copy.deepcopy(handlers_map) - ## handlers_map ## + # handlers_map - ## config ## + # config @property def config(self) -> LoggerConfigPM: try: @@ -620,9 +614,9 @@ def config(self, config: LoggerConfigPM): self.__config = copy.deepcopy(config) - ## config ## + # config - ## config_file_path ## + # config_file_path @property def config_file_path(self) -> str: try: @@ -648,11 +642,13 @@ def config_file_path(self, config_file_path: str): ): if not config_file_path.lower().endswith(".toml"): raise NotImplementedError( - f"`config_file_path` attribute value '{config_file_path}' is invalid, TOML file format is not supported yet!" + f"`config_file_path` attribute value '{config_file_path}' is invalid, " + f"TOML file format is not supported yet!" ) raise ValueError( - f"`config_file_path` attribute value '{config_file_path}' is invalid, file must be '.yml', '.yaml' or '.json' format!" + f"`config_file_path` attribute value '{config_file_path}' is invalid, " + f"file must be '.yml', '.yaml' or '.json' format!" ) if not os.path.isabs(config_file_path): @@ -660,5 +656,5 @@ def config_file_path(self, config_file_path: str): self.__config_file_path = config_file_path - ## config_file_path ## - ### ATTRIBUTES ### + # config_file_path + # ATTRIBUTES # diff --git a/src/beans_logging/_consts.py b/src/beans_logging/_consts.py index 20a72c6..49f7251 100644 --- a/src/beans_logging/_consts.py +++ b/src/beans_logging/_consts.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - from enum import Enum diff --git a/src/beans_logging/_handlers.py b/src/beans_logging/_handlers.py index 1363ea9..5d3f03e 100644 --- a/src/beans_logging/_handlers.py +++ b/src/beans_logging/_handlers.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - import sys import logging from logging import LogRecord @@ -25,13 +23,13 @@ def emit(self, record: LogRecord): record (LogRecord, required): Log needs to be handled. """ - ## Get corresponding Loguru level if it exists + # Get corresponding Loguru level if it exists try: _level = logger.level(record.levelname).name except ValueError: _level = record.levelno - ## Find caller from where originated the logged message + # Find caller from where originated the logged message _frame, _depth = sys._getframe(6), 6 while _frame and _frame.f_code.co_filename == logging.__file__: _frame = _frame.f_back diff --git a/src/beans_logging/_utils.py b/src/beans_logging/_utils.py index fc2c17c..131d0cd 100644 --- a/src/beans_logging/_utils.py +++ b/src/beans_logging/_utils.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - import os import sys import copy @@ -22,7 +20,8 @@ def create_dir(create_dir: str, warn_mode: WarnEnum = WarnEnum.DEBUG): Args: create_dir (str, required): Create directory path. - warn_mode (str, optional): Warning message mode, for example: 'ERROR', 'ALWAYS', 'DEBUG', 'IGNORE'. Defaults to "DEBUG". + warn_mode (str, optional): Warning message mode, for example: 'ERROR', 'ALWAYS', 'DEBUG', 'IGNORE'. + Defaults to "DEBUG". """ if not os.path.isdir(create_dir): diff --git a/src/beans_logging/auto.py b/src/beans_logging/auto.py index 9ee71a4..8105998 100644 --- a/src/beans_logging/auto.py +++ b/src/beans_logging/auto.py @@ -1,11 +1,10 @@ -# -*- coding: utf-8 -*- +# flake8: noqa import os -from typing import Union from . import * -logger_loader: Union[LoggerLoader, None] = None +logger_loader: LoggerLoader | None = None _DISABLE_DEFAULT_LOGGER = ( str(os.getenv("BEANS_LOGGING_DISABLE_DEFAULT")).strip().lower() ) diff --git a/src/beans_logging/filters.py b/src/beans_logging/filters.py index f8ea56f..0e82420 100644 --- a/src/beans_logging/filters.py +++ b/src/beans_logging/filters.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- - - def add_level_short(record: dict) -> dict: """Filter for adding short level name to log record. diff --git a/src/beans_logging/formats.py b/src/beans_logging/formats.py index a195bc4..7501bca 100644 --- a/src/beans_logging/formats.py +++ b/src/beans_logging/formats.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - import json import traceback diff --git a/src/beans_logging/rotation.py b/src/beans_logging/rotation.py index a2f1585..d4f7291 100644 --- a/src/beans_logging/rotation.py +++ b/src/beans_logging/rotation.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - import datetime from typing import TextIO @@ -35,8 +33,8 @@ def __init__(self, *, rotate_size: int, rotate_time: datetime.time): ) if _current_dtime >= self._dtime_limit: - ## The current time is already past the target time so it would rotate already. - ## Add one day to prevent an immediate rotation. + # The current time is already past the target time so it would rotate already. + # Add one day to prevent an immediate rotation. self._dtime_limit += datetime.timedelta(days=1) def should_rotate(self, message: Message, file: TextIO) -> bool: diff --git a/src/beans_logging/schemas.py b/src/beans_logging/schemas.py index e99061c..9661b5b 100644 --- a/src/beans_logging/schemas.py +++ b/src/beans_logging/schemas.py @@ -1,7 +1,4 @@ -# -*- coding: utf-8 -*- - import datetime -from typing import List import pydantic @@ -34,7 +31,10 @@ class StreamPM(ExtraBaseModel): use_color: bool = Field(default=True) use_icon: bool = Field(default=False) format_str: constr(strip_whitespace=True) = Field( - default="[{time:YYYY-MM-DD HH:mm:ss.SSS Z} | {level_short:<5} | {name}:{line}]: {message}", + default=( + "[{time:YYYY-MM-DD HH:mm:ss.SSS Z} | {level_short:<5} | {name}:{line}]: " + "{message}" + ), min_length=3, max_length=511, ) @@ -145,13 +145,13 @@ def _check_rotate_time(cls, val): class AutoLoadPM(ExtraBaseModel): enabled: bool = Field(default=True) only_base: bool = Field(default=False) - ignore_modules: List[str] = Field(default=[]) + ignore_modules: list[str] = Field(default=[]) class InterceptPM(ExtraBaseModel): auto_load: AutoLoadPM = Field(default_factory=AutoLoadPM) - include_modules: List[str] = Field(default=[]) - mute_modules: List[str] = Field(default=[]) + include_modules: list[str] = Field(default=[]) + mute_modules: list[str] = Field(default=[]) class ExtraPM(ExtraBaseModel): diff --git a/src/beans_logging/sinks.py b/src/beans_logging/sinks.py index 6e136ea..7dfd5fd 100644 --- a/src/beans_logging/sinks.py +++ b/src/beans_logging/sinks.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - import sys from loguru._handler import Message