Skip to content

Handler Class

Renato Byrro edited this page Feb 16, 2021 · 13 revisions

This is the primary Class you'll be using on this library. Use it by instantiating and adding to your Logger object(s) using the addHandler() method.

Example:

import logging
from http_logging import AsyncHttpHandler

logger = logging.getLogger()
logger.addHandler(AsyncHttpHandler(host='your-domain.com'))

Use the Logger as you would normally do:

logger.info('Informational message...')
logger.error('Ooops, something went wrong!')

Log messages are cached locally[SQLite cache] and delivered in batches to your host in POST requests[Request anatomy].

API Reference: http_logging.AsyncHttpHandler

Attributes

Only the most relevant attributes are listed here. The AsyncHttpHandler Class inherits from logstash_async.AsyncronousLogstashHandler Class (understand more). For more info on all attributes supported, please refer to the Python LogStash Async docs ⧉

  • host (str) [required]

    Provide the hostname where log messages should be delivered. E.g. my-domain.com. Do not provide a path, such as my-domain.com/path

    Use localhost or 127.0.0.1 in local test/debug environments

  • port (int) [optional, defaults to None]

    Port number for the HTTP connection to host. If not provided, port number will be omited in the request URL.

  • path (str) [optional, defaults to None]

    URL path for the HTTP connection url. Will be appended to host. If your host is my-domain.com and path is app/logs, the URL will be my-domain.com/app/logs.

  • timeout (float, in seconds) [optional, defaults to 5.0]

    Timeout limit for the HTTP connection to host. Optionally configure with the ASYNC_LOG_TIMEOUT environment variable[Environment Variables].

  • database_path (str) [optional, defaults to 'logging-cache.db']

    Path for the local SQLite database file that stores log messages cache. Optionally configure with the ASYNC_LOG_DATABASE_PATH environment variable[Environment Variables].

  • transport (http_logging.AsyncHttpTransport) [optional, defaults to http_logging.AsyncHttpTransport(...)]

    HTTP Transport object. Usually you won't need to set this attribute. If not provided, the AsyncHttpTransport Class is instantiated with the arguments passed to the AsyncHttpHandler Class.

    For more details, please refer to the HTTP Transport Class documentation.

  • formatter (http_logging.HttpLogFormatter()) [optional, defaults to http_logging.HttpLogFormatter()]

    Customize how log messages are formatted before sending to the host. If not provided, the HttpLogFormatter Class is instantiated with its default attribute values.

    For more details, please refer to the HTTP Formatter Class documentation.

  • custom_headers (Callable) [optional, defaults to None]

    Pass in a function (or another Callable) that returns a Dict[str, str] object. The result from this function will be appended to the default HTTP headers in requests sent to the host.

    For more information, please refer to Adding custom HTTP headers to requests in the documentation.

  • ssl_enable (bool) [optional, defaults to True]

    Whether SSL should be enabled for the connection to your host.

  • ssl_verify (bool) [optional, defaults to True]

    Whether host server's SSL certificate should be verified on each connection.

  • event_ttl (int, in seconds) [optional, defaults to None]

    Time-to-live (TTL) for messages that are waiting to be published. If a message is beyond it's TTL, it will be deleted from the cache and will not be published to the host. [Python LogStash Async docs] ⧉

    If not supplied, log messages will never be deleted from cache.

  • enable (bool) [optional, defaults to True]

    Optionally set to False in order to disable HTTP logging in local testing/debugging tasks, for example.

Methods

  • flush (no args taken)

    Manually send cached log messages to the host.

    For more info, please refer to Trigger event flushing ⧉ in the Python LogStash Async documentation.

  • shutdown (no args taken)

    Terminate the HTTP logging background thread and close the requests session object[requests session].

    You usually will not need to call this method. The library tears down resources automatically when your program exits. Use this only if you need to terminate the HTTP logging before your program ends.


Footnotes

[SQLite cache] Refer to Local caching with SQLite in the documentation

[POST request anatomy] Refer to Anatomy of POST requests sent to the HTTP host in the documentation

[Environment Variables]: Refer to Configuration Constants and Environment Variables in the documentation

[requests session] Async HTTP Logging uses a Session object from the Python requests library to handle HTTP connections

Clone this wiki locally