Handler Class - hacktlib/py-async-http-logging GitHub Wiki
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].
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 asmy-domain.com/pathUse
localhostor127.0.0.1in local test/debug environments -
port(int) [optional, defaults toNone]Port number for the HTTP connection to host. If not provided,
portnumber will be omited in the request URL. -
path(str) [optional, defaults toNone]URL path for the HTTP connection url. Will be appended to host. If your host is
my-domain.comandpathisapp/logs, the URL will bemy-domain.com/app/logs. -
timeout(float, in seconds) [optional, defaults to5.0]Timeout limit for the HTTP connection to host. Optionally configure with the
ASYNC_LOG_TIMEOUTenvironment 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_PATHenvironment variable[Environment Variables]. -
transport(http_logging.AsyncHttpTransport) [optional, defaults tohttp_logging.AsyncHttpTransport(...)]HTTP Transport object. Usually you won't need to set this attribute. If not provided, the
AsyncHttpTransportClass is instantiated with the arguments passed to theAsyncHttpHandlerClass.For more details, please refer to the HTTP Transport Class documentation.
-
formatter(http_logging.HttpLogFormatter()) [optional, defaults tohttp_logging.HttpLogFormatter()]Customize how log messages are formatted before sending to the host. If not provided, the
HttpLogFormatterClass is instantiated with its default attribute values.For more details, please refer to the HTTP Formatter Class documentation.
-
custom_headers(Callable) [optional, defaults toNone]Pass in a function (or another
Callable) that returns aDict[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 toTrue]Whether SSL should be enabled for the connection to your host.
-
ssl_verify(bool) [optional, defaults toTrue]Whether host server's SSL certificate should be verified on each connection.
-
event_ttl(int, in seconds) [optional, defaults toNone]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 toTrue]Optionally set to
Falsein order to disable HTTP logging in local testing/debugging tasks, for example.
-
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.
[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 ↩