Giter Site home page Giter Site logo

bink-logging-utils's Introduction

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" ]

bink-logging-utils's People

Contributors

lupusinfabula avatar cpressland avatar

Watchers

 avatar  avatar Kashim Aziz avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.