thinlog.handlers.json_http module#

Handler that sends JSON log records via HTTP POST.

class thinlog.handlers.json_http.JsonHTTPHandler(settings: JsonHttpHandlerSettings | dict[str, Any])[source]#

Bases: Handler

Send formatted log records as JSON via HTTP POST using httpx.

The HTTP client is lazily initialised on first use. Per-record overrides (URL, headers, disable) can be provided via a handlers_context.json_http dict on the log record.

Parameters:

settings – A JsonHttpHandlerSettings or a dict of its fields.

property client: Client#

Lazily initialised httpx.Client.

close() None[source]#

Close the underlying HTTP client.

emit(record: LogRecord) None[source]#

Format and POST the record as JSON.

Supports per-record overrides via record.handlers_context["json_http"]:

  • disable – skip this record entirely.

  • url – override the target URL.

  • headers – replace the default headers.

  • append_headers – merge additional headers.

json_request(url: str, data: str | dict[str, Any], **kwargs: Any) Any[source]#

POST data as JSON (parsing it first if it is a string).

request(url: str, **kwargs: Any) Any[source]#

Send an HTTP POST and return the parsed JSON response.

class thinlog.handlers.json_http.JsonHttpHandlerSettings(url: str, headers: dict[str, str], level: str | int = 0, timeout: int = 5, proxy: str | None = None)[source]#

Bases: object

Settings for JsonHTTPHandler.

The first instance created is stored as a global singleton accessible via global_instance().

Parameters:
  • url – Target URL for HTTP POST requests.

  • headers – Default HTTP headers sent with every request.

  • level – Minimum log level (name or int).

  • timeout – HTTP request timeout in seconds.

  • proxy – Optional HTTP proxy URL.

classmethod global_instance() JsonHttpHandlerSettings | None[source]#

Return the first-created settings instance, or None.

headers: dict[str, str]#
level: str | int = 0#
proxy: str | None = None#
timeout: int = 5#
url: str#