LoggerExporter

Export your logs to the service of your choice.

I created this library because I wanted to export logs to a different service in Heroku. There is no simple way to export your logs.

Supported exporters:

Implement your own exporter using LoggerExporter.Exporters.Exporter behaviour.

Supported formatters:

Implement your own formatter using LoggerExporter.Formatters.Formatter behaviour.

Plug Logger

A plug for logging request information. It will log the method, path, params, status and duration.

You can add it to your MyApp.Endpoint:

  plug LoggerExporter.Loggers.Plug

Installation

Add logger_exporter to your list of dependencies in mix.exs:

def deps do
  [
    {:logger_exporter, "~> 0.1.0"}
  ]
end

Configuration

By default, the timestamp sent for each log to the external service is in utc: System.os_time(:nanosecond)

HTPP Auth

Supported authentication methods:

Usage in Phoenix

  1. Add the following to deps section of your mix.exs: {:logger_exporter, "~> 0.1.0"} and then mix deps.get

  2. Add LoggerExporter.Backend to your logger’s backends configuration

    config :logger,
      backends: [:console, LoggerExporter.Backend]
  3. Add config related to the exporter and other fields. ie. for LokiExporter

    config :logger, LoggerExporter,
      host: "http://localhost:3100",
      app_name: "my_app",
      environment_name: Mix.env(),
      http_auth: {:basic, System.fetch_env!("LOKI_USER"), System.fetch_env!("LOKI_PASSWORD")},
      metadata: [:request_id]
  4. Start the LoggerExporter GenServer in the supervised children list. In application.ex add to the children list:

    children [
      ...
      LoggerExporter
    ]
  5. (Optional) Add custom Plug logger. In MyApp.Endpoint add the plug after Plug.Parsers. If you see duplicate logs, remove Plug.Telemetry from your endpoint.

     plug Plug.Parsers,
       ...
    
     plug LoggerExporter.Loggers.Plug

JSON Formatter

If you want to log in JSON format, you can use the formatter of another library: logger_json

You can configure it like this:

config :logger, LoggerExporter,
  formatter: LoggerJSON.Formatters.BasicLogger

And it will work out of the box :)

Telemetry

Telemetry integration for logging and error reporting.

There is a default logger for you to attach. It logs when the status is :error

In your application.ex

:ok = LoggerExporter.Telemetry.attach_default_logger()

HTTP Post batch events

LoggerExporter emits the following events for processing each batch (sending it through http)

The following chart shows which metadata you can expect for each event:

event measures metadata
:start:system_time:events
:stop:duration:events, :status, :response
:exception:duration:events, :kind, :reason, :stacktrace

Metadata