Skip to main content

Python library for logging with colors and http status code.

Project description

fxc-logger

Install


pip install fxc-logger

Configure


# main.py
from fxc_logger import PyLogger

logger = PyLogger(appname="my_app")
logger.http(message="Uma mensagem http qualquer", status_code=200, data={"name": "Um nome qualquer", "age": 22})

2022-09-02 14:05:59 | [HTTP] | | 9fd8… | Uma mensagem http qualquer | [STATUSCODE 200] | {"name": "Um nome qualquer", "age": 22}

Correlation ID (Contexto)

Cada requisição ou mensagem pode ter um identificador único para facilitar filtros de log. A biblioteca gera um UUID v4 automaticamente no primeiro log, mas você pode controlar isso manualmente.

from fxc_logger import PyLogger, correlation_scope, with_new_correlation_id

logger = PyLogger(appname="consumer")

# 1) Definindo manualmente
auto_id = correlation_scope()      # context-manager
with correlation_scope():          # gera novo UUID
    logger.info("log dentro do bloco")

# 2) Decorando callback do RabbitMQ / Kafka
@with_new_correlation_id
def callback(ch, method, props, body):
    logger.info("mensagem recebida", data=body)

Todos os logs emitidos dentro do bloco ou função decorada carregam o mesmo Correlation ID, permitindo rastrear todo o fluxo.

Log Types


Tipo Description
error Informar sobre exceptions tratadas
warning Informar eventos ou estados potencialmente prejudicias ao programa
debug Acompanhar eventos ou estados do programa
info Descrever infos detalhadas sobre o estado do programa
http Informar dados de requests e responses feitas via http

Middlewares


django

# httpLogger.py
from fxc_logger import PyLogger
from django.utils.deprecation import MiddlewareMixin

class HTTPLoggerMiddleware(MiddlewareMixin):
    def process_response(self, request, response):
        logger = PyLogger(appname="my_django_app")
        logger.http(message=f"{request.META.get('REMOTE_ADDR')} {request.method} {request.META.get('PATH_INFO')}",
                    status_code=response.status_code)
        return response

settings.py:

MIDDLEWARE = [
    ...,
    'app.utils.httpLogger.HTTPLoggerMiddleware'
]

Flask

from flask import Flask, request, Response
from fxc_logger import PyLogger

app = Flask(__name__)
logger = PyLogger(appname="my_flask_app")

@app.after_request
def log_response(response: Response):
    logger.http(message=f"{request.remote_addr} {request.method} {request.path}",
                status_code=response.status_code)
    return response

Alert

from fxc_logger import PyLogger

logger = PyLogger(appname="my_app", group="warehouse")

# envia alerta (ENV=PRD)
logger.error("Falhou o checkout", status_code=500, data={"order_id": 123})

# não envia alerta
logger.error("Falhou o checkout de novo", status_code=500, alert=False)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fxc_logger-1.0.2.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fxc_logger-1.0.2-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file fxc_logger-1.0.2.tar.gz.

File metadata

  • Download URL: fxc_logger-1.0.2.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.0

File hashes

Hashes for fxc_logger-1.0.2.tar.gz
Algorithm Hash digest
SHA256 7ff9a4dd6ccc80c622473218bb38f3779424465d62f1a85b99fa20a832a750d2
MD5 2826bf1731bbc81d4ff78b678ba8ce1d
BLAKE2b-256 2b9eeeacd9bc8f5d1e237fe1c1430800ced0208f3b1979921376d2df677bf08d

See more details on using hashes here.

File details

Details for the file fxc_logger-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: fxc_logger-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.0

File hashes

Hashes for fxc_logger-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 7107c2e22f7d023c3473b0f0c31990c499d09f6f987e7a767d333d18654b2f62
MD5 4fc1fd58ca54279d044c4f627ac5169e
BLAKE2b-256 3a4564e9735813fdfca1a3338c9aec7465ff67dec87c5fddad600a7476a05755

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page