Skip to content

Commit

Permalink
Structlog and more fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
xfenix committed Oct 9, 2023
1 parent 4c14cbb commit b3f52a8
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 47 deletions.
51 changes: 18 additions & 33 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ license = "MIT"
python = "^3.10"
gunicorn = "*"
uvicorn = "*"
loguru = "*"
pyenchant = "*"
toml = "*"
pylru = "*"
Expand All @@ -18,6 +17,7 @@ anyio = "*"
sentry-sdk = "*"
pydantic-settings = "*"
fastapi = "*"
structlog = "^23.2.0"

[tool.poetry.group.dev.dependencies]
httpx = "*"
Expand Down
9 changes: 5 additions & 4 deletions whole_app/dictionaries/__init__.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
from loguru import logger
import typing

import structlog

from . import dummy as dummy_storage
from . import file as file_storage
from . import protocol
from whole_app import misc_helpers
from whole_app.settings import SETTINGS, StorageProviders


misc_helpers.init_logger()
LOGGER_OBJ: typing.Final = structlog.get_logger()


def init_storage() -> None:
if SETTINGS.dictionaries_storage_provider == StorageProviders.FILE:
file_storage.init_storage()
elif SETTINGS.dictionaries_storage_provider == StorageProviders.DUMMY:
logger.warning(
LOGGER_OBJ.warning(
"Storage provider set to dummy mode. "
"Currently all user dictionary requests will be thrown away. We worn you.",
)
Expand Down
22 changes: 17 additions & 5 deletions whole_app/misc_helpers.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
import sys
import logging
import typing

from loguru import logger
import structlog

from .settings import SETTINGS
from whole_app.settings import SETTINGS


def init_logger() -> None:
logger.remove()
logger.add(sys.stdout, serialize=SETTINGS.structured_logging)
our_processors: typing.Final[typing.Any] = [
structlog.contextvars.merge_contextvars,
structlog.processors.add_log_level,
structlog.processors.format_exc_info,
structlog.processors.TimeStamper(fmt="iso", utc=True),
]
if SETTINGS.structured_logging:
our_processors.append(structlog.processors.JSONRenderer())
structlog.configure(
cache_logger_on_first_use=True,
wrapper_class=structlog.make_filtering_bound_logger(logging.INFO),
processors=our_processors,
)
9 changes: 5 additions & 4 deletions whole_app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import typing

import pydantic
import structlog
import toml
from loguru import logger
from pydantic_settings import BaseSettings


LOGGER_OBJ: typing.Final = structlog.get_logger()
PATH_TO_PYPROJECT: typing.Final = pathlib.Path(__file__).parent.parent / "pyproject.toml"
AvailableLanguagesType = typing.Literal[
"ru_RU",
Expand All @@ -24,7 +25,7 @@ def _warn_about_poor_lru_cache_size(
possible_value: int,
) -> int:
if int(possible_value) < 1:
logger.warning(
LOGGER_OBJ.warning(
(
"You set cache size less then 1. In this case, "
"the cache size will be unlimited and polute your memory."
Expand All @@ -38,7 +39,7 @@ def _warn_about_empty_api_key(
possible_value: str,
) -> str:
if not possible_value:
logger.warning("You set empty API key. This is not recommended.")
LOGGER_OBJ.warning("You set empty API key. This is not recommended.")
return possible_value


Expand All @@ -51,7 +52,7 @@ def _parse_version_from_local_file(
)
return pyproject_obj["tool"]["poetry"]["version"]
except (toml.TomlDecodeError, KeyError, FileNotFoundError) as exc:
logger.warning(f"Cant parse version from pyproject. Trouble {exc}")
LOGGER_OBJ.warning("Cant parse version from pyproject. Trouble %s", exc)
return default_value


Expand Down

0 comments on commit b3f52a8

Please sign in to comment.