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.1.tar.gz (6.4 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.1-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fxc_logger-1.0.1.tar.gz
  • Upload date:
  • Size: 6.4 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.1.tar.gz
Algorithm Hash digest
SHA256 52a00161d80d92758fadd23a7012aa1a5dbd2430a24191027e4ecea7bd887d25
MD5 bbf8fcad9a0bba0b3ebec2d3c8a01baa
BLAKE2b-256 d3a5415a4d6e2ae8e67ab629410b66ce46f7ef28a54f310538d15d6ffa7ee786

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fxc_logger-1.0.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 82408dd9696c767c621456a6d5a7b197e333784614d4010c5fe6aaef891fda86
MD5 b2a846e9bc5c6a9e54ac9d25fc865643
BLAKE2b-256 214cab733d68388db598d8044aad107318b104f8c310895e36ce8a4fa4b1bb1e

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