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
4 changes: 2 additions & 2 deletions .github/workflows/actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-22.04
strategy:
matrix:
python-version: [ "3.9", "3.10", "3.11", "3.12", "3.13" ]
python-version: [ "3.10", "3.11", "3.12", "3.13" ]

steps:
- name: Setup dependencies
Expand All @@ -24,7 +24,7 @@ jobs:
runs-on: ubuntu-22.04
strategy:
matrix:
python-version: [ "3.9", "3.10", "3.11", "3.12", "3.13" ]
python-version: [ "3.10", "3.11", "3.12", "3.13" ]

steps:
- name: Setup dependencies
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

_Shows logs when you need it_

## Requirements

- Python 3.10+


## Проблема/решение

Expand Down
2,099 changes: 1,017 additions & 1,082 deletions poetry.lock

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions pybotx_smart_logger/contextvars.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
"""Contextvars for smart_logger."""

from contextvars import ContextVar
from typing import Optional

from pybotx_smart_logger.schemas import AccumulatedLogs

_accumulated_logs_var: ContextVar[Optional[AccumulatedLogs]] = ContextVar(
_accumulated_logs_var: ContextVar[AccumulatedLogs | None] = ContextVar(
"accumulated_logs",
default=None,
)
Expand Down
2 changes: 1 addition & 1 deletion pybotx_smart_logger/log_levels.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

TRACE = "TRACE"
DEBUG = "DEBUG"
INFO = "INFO" # noqa: WPS110
INFO = "INFO"
SUCCESS = "SUCCESS"
WARNING = "WARNING"
ERROR = "ERROR"
Expand Down
2 changes: 1 addition & 1 deletion pybotx_smart_logger/logger.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"""Prints logs only if exception was raised while processing message."""

import inspect
from datetime import datetime
from typing import Any

from loguru import logger
from loguru._datetime import datetime # noqa: WPS436

from pybotx_smart_logger import log_levels
from pybotx_smart_logger.contextvars import (
Expand Down
3 changes: 1 addition & 2 deletions pybotx_smart_logger/schemas.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
"""Schemas for smart logger internals."""

from dataclasses import dataclass
from datetime import datetime
from typing import Any

from loguru._datetime import datetime # noqa: WPS436


@dataclass
class LogEntry:
Expand Down
2 changes: 1 addition & 1 deletion pybotx_smart_logger/wrapper.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from collections.abc import AsyncGenerator, Callable
from contextlib import asynccontextmanager
from typing import AsyncGenerator, Callable

from loguru import logger

Expand Down
83 changes: 63 additions & 20 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,69 @@ authors = ["Alexander Samoylenko <alexandr.samojlenko@ccsteam.ru>"]
readme = "README.md"

[tool.poetry.dependencies]
python = ">=3.9, <3.14"

loguru = ">=0.6.0,<0.7.0"

[tool.poetry.dev-dependencies]
add-trailing-comma = "3.1.0"
autoflake = "2.3.1"
black = "25.1.0"
isort = "6.0.1"
mypy = "1.15.0"
wemake-python-styleguide = "0.19.2"
bandit = "1.8.3"

pytest = "^8.3.5"
pytest-asyncio = "^0.26.0"
pytest-cov = "^6.1.1"
requests = "^2.32.3"

fastapi = ">=0.70.0,<0.116.0"
pybotx = ">=0.75.0,<1.0.0"
python = ">=3.10, <3.14"

loguru = ">=0.7.3,<0.8.0"

[tool.poetry.group.dev.dependencies]
mypy = "^1.19.1"
bandit = "^1.9.3"

pytest = "^9.0.2"
pytest-asyncio = "^1.3.0"
pytest-cov = "^7.0.0"
pytest-xdist = "^3.8.0"
pytest-mock = "^3.15.1"
freezegun = "^1.5.5"
factory-boy = "^3.3.3"
faker = "^40.4.0"
requests = "^2.32.5"
ruff = "^0.15.1"

fastapi = "^0.129.0"
pybotx = ">=0.76.0,<1.0.0"
pybotx-smartapp-rpc = "^0.13.0"

[tool.ruff]
line-length = 88
target-version = "py310"

[tool.ruff.lint]
select = [
"E",
"F",
"I",
"B",
"UP",
]

[tool.ruff.lint.per-file-ignores]
"tests/*" = ["S101"]

[tool.pytest.ini_options]
testpaths = ["tests"]
asyncio_mode = "auto"
asyncio_default_fixture_loop_scope = "function"
addopts = """
--strict-markers
--tb=short
--cov=pybotx_smart_logger
--cov-report=term-missing
--cov-branch
--no-cov-on-fail
--cov-fail-under=100
"""

[tool.mypy]
plugins = ["pydantic.mypy"]
disallow_untyped_defs = true
warn_redundant_casts = true
warn_unused_ignores = true
show_error_codes = true

[[tool.mypy.overrides]]
module = ["pytest.*"]
ignore_missing_imports = true

[build-system]
requires = ["poetry>=1.2.0"]
Expand Down
5 changes: 2 additions & 3 deletions scripts/docs-lint
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ mkdir "${snippets_dir_path}"

python scripts/collect_snippets.py

isort --profile black --check-only "${snippets_dir_path}"
black --check --diff "${snippets_dir_path}"
ruff format --check "${snippets_dir_path}"
ruff check --ignore-noqa --select E,F,I,B "${snippets_dir_path}"
mypy "${snippets_dir_path}"
flake8 "${snippets_dir_path}"

rm -rf "${snippets_dir_path}"
19 changes: 2 additions & 17 deletions scripts/format
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,5 @@

set -ex

autoflake --recursive --in-place \
--remove-all-unused-imports \
--ignore-init-module-imports \
pybotx_smart_logger tests
isort --profile black pybotx_smart_logger tests
black pybotx_smart_logger tests

find pybotx_smart_logger -type f -name "*.py" | xargs add-trailing-comma --py36-plus --exit-zero-even-if-changed
find tests -type f -name "*.py" | xargs add-trailing-comma --py36-plus --exit-zero-even-if-changed

# This `black` is needed again in order to transfer parameters/arguments to new lines
# after inserting commas.
# The first `black` won't be able to transfer parameters/arguments to new lines because
# there is no comma at the end of the line.
# Inserting commas must be after the first `black`, so that there is one new line break,
# if the line is out of max-line-length.
black pybotx_smart_logger tests > /dev/null
ruff check --fix pybotx_smart_logger tests
ruff format pybotx_smart_logger tests
6 changes: 2 additions & 4 deletions scripts/lint
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

set -ex

black --check --diff pybotx_smart_logger tests
isort --profile black --check-only pybotx_smart_logger tests

ruff format --check pybotx_smart_logger tests
ruff check pybotx_smart_logger tests
mypy pybotx_smart_logger tests
flake8 pybotx_smart_logger tests
2 changes: 1 addition & 1 deletion scripts/test
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

set -ex

pytest ${@}
pytest -n auto "$@"
79 changes: 0 additions & 79 deletions setup.cfg

This file was deleted.

28 changes: 23 additions & 5 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
from typing import Any, Callable, Generator, Optional
from collections.abc import Callable, Generator
from typing import Any
from uuid import UUID, uuid4

import pytest
Expand All @@ -14,6 +15,12 @@
UserSender,
)

from pybotx_smart_logger.contextvars import (
clear_accumulated_logs,
set_debug_enabled,
set_grouping_enabled,
)


@pytest.fixture
def bot_id() -> UUID:
Expand All @@ -35,6 +42,17 @@ def host() -> str:
return "cts.example.com"


@pytest.fixture(autouse=True)
def reset_smart_logger_context() -> Generator[None, None, None]:
clear_accumulated_logs()
set_debug_enabled(False)
set_grouping_enabled(False)
yield
clear_accumulated_logs()
set_debug_enabled(False)
set_grouping_enabled(False)


@pytest.fixture
def bot_account(host: str, bot_id: UUID) -> BotAccountWithSecret:
return BotAccountWithSecret(
Expand All @@ -54,9 +72,9 @@ def incoming_message_factory(
def factory(
*,
body: str = "",
raw_command: Optional[dict[str, Any]] = None,
ad_login: Optional[str] = None,
ad_domain: Optional[str] = None,
raw_command: dict[str, Any] | None = None,
ad_login: str | None = None,
ad_domain: str | None = None,
) -> IncomingMessage:
return IncomingMessage(
bot=BotAccount(
Expand Down Expand Up @@ -105,7 +123,7 @@ def loguru_caplog(
) -> Generator[pytest.LogCaptureFixture, None, None]:
# https://github.com/Delgan/loguru/issues/59

class PropogateHandler(logging.Handler): # noqa: WPS431
class PropogateHandler(logging.Handler):
def emit(self, record: logging.LogRecord) -> None:
logging.getLogger(record.name).handle(record)

Expand Down
18 changes: 18 additions & 0 deletions tests/factories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from datetime import datetime

import factory

from pybotx_smart_logger.schemas import LogEntry


class LogEntryFactory(factory.Factory):
class Meta:
model = LogEntry

log_message = factory.Faker("sentence")
log_args = factory.LazyFunction(tuple)
log_kwargs = factory.LazyFunction(dict)
module = factory.Faker("slug")
function = factory.Faker("word")
line_number = factory.Faker("pyint", min_value=1, max_value=1000)
time = factory.LazyFunction(datetime.now)
Loading
Loading