No description
This repository has been archived on 2026-04-20. You can view files and clone it, but you cannot make any changes to its state, such as pushing and creating new issues, pull requests or comments.
  • Python 89.5%
  • Shell 10.5%
Find a file
Chris Pressland 0b1a91241b
Some checks failed
ci / tests (push) Successful in 50s
ci / release (push) Failing after 6m21s
Add build support for Azure DevOps
2023-12-19 12:41:29 +00:00
.github/workflows Add build support for Azure DevOps 2023-12-19 12:41:29 +00:00
bink_logging_utils change x_azure_ref to x-azure-ref and update sink patcher logic 2023-07-24 17:23:48 +03:00
.gitignore update gitignore 2023-06-22 15:33:13 +03:00
LICENSE CI Tweaks and Fixes 2023-06-22 13:18:49 +01:00
poetry.lock Add build support for Azure DevOps 2023-12-19 12:41:29 +00:00
pyproject.toml fix loguru extra, refactor code, update README 2023-07-18 16:36:04 +03:00
README.md fix indentation 2023-07-18 16:40:20 +03:00
style.sh fix style.sh path 2023-07-24 17:26:08 +03:00

Bink Logging Utils

Helper functions to setup loguru as the output for all the loggers in a project.

using loguru directly

From the bink_logging_utils module import init_loguru_root_sink.

Calling this will initialise the default loguru sink.

eg

from bink_logging_utils import init_loguru_root_sink

# ------- env vars -------- #
JSON_LOGGING: bool = ...
ROOT_LOG_LEVEL: str | int = ...
# ------------------------- #

init_loguru_root_sink(json_logging=JSON_LOGGING, sink_log_level=ROOT_LOG_LEVEL, show_pid=True)

funnelling existing logging calls into loguru

From the bink_logging_utils.handlers module import loguru_intercept_handler_factory.

This function will return an InterecptHandler class that can be set as the default logging handler, this will let loguru handle all logs output.

eg

import logging

from bink_logging_utils.handlers import loguru_intercept_handler_factory


InterceptHandler = loguru_intercept_handler_factory()

# then

logging.basicConfig(handlers=[InterceptHandler()], ...)

# or

logging.config.dictConfig(
    {
        ...
        "handlers": {
            "console": {"()": "path.to.InterceptHandler"},
        },
        ...
    }
)

gunicorn

when installing this library specify the gunicorn extra eg: poetry add bink-logging-utils[gunicorn]

From the bink_logging_utils.gunicorn module import gunicorn_logger_factory.

This function will return a custom Glogger class that funnels the output of gunicorn logs into loguru and, by default, filters out any gunicorn.access requests to the /healthz, /livez, /readyz, and /metrics endpoints.

Update the Dockerfile gunicorn run command with the flag --logger-class=path.to.CustomGunicornLogger.

eg

pyproject.toml

[[tool.poetry.source]]
name = "bink-pypi"
url = "https://pypi.gb.bink.com/simple"
default = false
secondary = false

[tool.poetry.dependencies]
...
bink-logging-utils = {version = "^1.1.0", source = "bink-pypi", extras=["gunicorn"]}
...

python module

from bink_logging_utils.handlers import loguru_intercept_handler_factory
from bink_logging_utils.gunicorn import gunicorn_logger_factory


InterceptHandler = loguru_intercept_handler_factory()

CustomGunicornLogger = gunicorn_logger_factory(intercept_handler_class=InterceptHandler)

Dockerfile

...

CMD [ "gunicorn", "--workers=2", "--error-logfile=-", "--access-logfile=-", \
      "--bind=0.0.0.0:9000", "--logger-class=path.to.CustomGunicornLogger", \
      "app.wsgi" ]