Skip to main content

Python logging handler that sends log records to a HTTP server in a non-blocking way.

Project description

Non-Blocking-Http-Logging-Handler

License: MIT Python Versions

This library provides a non-blocking http logging handler for python 3.8+ that can be used to send logs to a logging service in a non-blocking way via http requests in a very simple way.

Installation

pip install non-blocking-http-logging-handler

Basic Usage

import logging

from non_blocking_http_handler.handler import NonBlockingHttpHandler

httpHandler = NonBlockingHttpHandler(
    url='http://localhost:5000/logs',
    max_workers=10,
    max_retries=3
)

log = logging.getLogger()
log.setLevel(logging.DEBUG)  # set level to DEBUG to see all logs
log.addHandler(httpHandler)

The complete example is available in the examples folder in the file basic_usage.py.

Parameters explanation

  • url: The url of the logging service.
  • max_workers: The maximum number of workers that will be used to send logs to the logging service.
  • max_retries: The maximum number of retries that will be done if the request fails.
  • extra: A dictionary with extra fields that will be added to the log (explained below).

Output format

The output format of the logs is a json with the following fields:

  • level: The level of the log.
  • message: The message of the log.
  • file: The file name of the log.
  • line: The line number of the log.
  • timestamp: The timestamp of the log.
  • the extra fields that you may have added to the log.

for example:

{
    "level": "INFO",
    "message": "This is a log",
    "file": "basic_usage.py",
    "line": 10,
    "timestamp": "2021-10-10 10:10:10"
}

Add extra fields to the log

In same cases you may want to add extra fields to the log, for example, if you are using a logging handler to send logs to a logging service, you may want to add the service name to the log or the ip.

For this purpose, you can use the extra parameter of the NonBlockingHttpHandler class.

import logging
import socket  # for get hostname amd ip

from non_blocking_http_handler.handler import NonBlockingHttpHandler

httpHandler = NonBlockingHttpHandler(
    url='http://localhost:5000/logs',
    max_workers=5,
    max_retries=3,
    extra={
        'hostname': socket.gethostname()
    }
)

log = logging.getLogger()
log.setLevel(logging.DEBUG)  # set level to DEBUG to see all logs
log.addHandler(httpHandler)

The complete example is available in the examples folder in the file extra_fields.py.

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

Supported by

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