View Source LoggerHTTP.Adapter behaviour (logger_http v0.1.0)

A behaviour module for implementing a custom HTTP adapter for LoggerHTTP.

The default adapter is LoggerHTTP.Adapter.Req.

To configure a different adapter, implement the LoggerHTTP.Adapter behaviour and set the :adapter option in the logger handler configuration.

config :my_app, :logger, [
  {:handler, :my_http_handler, LoggerHTTP.Handler, %{
    config: %{
      url: "https://example.com/logs",
      adapter: MyApp.CustomAdapter
    }
  }}
]

Be careful about using Logger in the adapter

Since the adapter is called from within a logger handler, it can be very troublesome to use Logger in its callbacks. Doing so can lead to an infinite number of logs being created, the app slowing down, and finally the logger handler crashing and being detached. Try to avoid it, and be careful if you really need it.

Summary

Types

The options for the adapter.

Callbacks

Validate and cast the options given to the adapter.

Handle an error that occurred when sending the logs.

Send the logs to the given URL.

Types

@type options() :: keyword()

The options for the adapter.

The options are given in the logger handler configuration, then validated using the cast_options/1 callback if provided. They are then passed to the other callbacks to configure the adapter's behaviour.

Callbacks

Link to this callback

cast_options(options)

View Source (optional)
@callback cast_options(options()) :: options()

Validate and cast the options given to the adapter.

This callback is called directly when adding the logger. It may raise an exception if the passed options are invalid, which will lead to the logger handler failing to be added.

Link to this callback

handle_error(term, options)

View Source
@callback handle_error(term(), options()) :: term()

Handle an error that occurred when sending the logs.

The error can be anything returned by send_logs/3. When using the default adapter, the error is known to be of type Exception.t/0.

Link to this callback

send_logs(url, logs, options)

View Source
@callback send_logs(url :: String.t(), logs :: [iodata()], options()) ::
  :ok | {:error, term()}

Send the logs to the given URL.

This callback should not handle the error directly, but rather return {:error, reason}. handle_error/2 will then be called with reason as its first parameter.