View Source LoggerTelegramBackend (logger_telegram_backend v3.0.0-rc.0)

A logger backend for Telegram.

Installation

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

def deps do
  [
    {:logger_telegram_backend, "~> 3.0"},
    {:logger_backends, "~> 1.0"},
    {:hackney, "~> 1.18"},
  ]
end

In your Application.start/2 callback, add the LoggerTelegramBackend backend:

@impl true
def start(_type, _args) do
  LoggerBackends.add(LoggerTelegramBackend)

  # ...
end

Configuration

First of all you need to create a Telegram bot. Follow the instructions here to create one and get the token for the bot. Since bots are not allowed to contact users, you need to send a message first. Afterwards, retrieve your chat_id with $ curl -X GET https://api.telegram.org/botYOUR_TOKEN/getUpdates.

Then configure the telegram chat_id and bot token:

config :logger, LoggerTelegramBackend,
  chat_id: "$chatId",
  token: "$botToken"

The logger configuration is read at runtime from the application environment so that you can provide it via distillerys dynamic configuration with environment variables.

Options

The following options are available:

  • :level - the level to be logged by this backend (either :debug, :info, :warning or :error). Note that messages are filtered by the general :level configuration for the :logger application first. If not explicitly configured all levels are logged.
  • :metadata - the metadata to be included in the telegram message. Defaults to [:line, :function, :module, :application, :file]. Setting :metadata to :all gets all metadata.
  • :metadata_filter - the metadata which is required in order for a message to be logged. Example: metadata_filter: [application: :ui].
  • :client - If you need different functionality for the HTTP client, you can define your own module that implements the LoggerTelegramBackend.HTTPClient behaviour and set client to that module (default: LoggerTelegramBackend.HackneyClient)
  • :hackney_opts - (default: [pool: :logger_telegram_backend_pool])
  • :hackney_pool_max_connections (default: 50)
  • :hackney_pool_timeout - (default: 5000)

Examples

Metadata filter
config :logger, LoggerTelegramBackend,
  chat_id: "$chatId",
  token: "$botToken",
  level: :info,
  metadata: :all
  metadata_filter: [application: :ui]
Finch client
config :logger, LoggerTelegramBackend,
  chat_id: "$chatId",
  token: "$botToken",
  client: MyFinchClient

You'll need to add Finch instead of :hackney to your list of dependencies:

  {:finch, "~> 0.16"}
defmodule MyFinchClient do
  @behaviour LoggerTelegramBackend.HTTPClient

  @finch_pool_name MyApp.Finch

  @impl true
  def child_spec do
    Finch.child_spec(name: @finch_pool_name)
  end

  @impl true
  def request(method, url, headers, body) do
    req = Finch.build(method, url, headers, body)

    case Finch.request(req, @finch_pool_name) do
      {:ok, %Finch.Response{status: status, headers: headers, body: body}} ->
        {:ok, status, headers, body}

      {:error, reason} ->
        {:error, reason}
    end
  end
end
SOCKS5 proxy
config :logger, LoggerTelegramBackend,
  chat_id: "$chatId",
  token: "$botToken",
  hackney_opts: [
    ssl: [verify: :verify_none],
    hackney: [insecure: true],
    proxy: {:socks5, ~c"127.0.0.1", 9050}
  ]

See the hackney docs for further information.