View Source LoggerTelegramBackend (logger_telegram_backend v3.0.0)

A logger backend for Telegram.

Usage

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

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

  # ...
end

Add the following to your configuration:

config :logger, LoggerTelegramBackend,
  chat_id: "your_chat_id",
  token: "yout_bot_token"

To create a Telegram bot, see the next section.

Creating a Telegram bot

To create a Telegram bot, follow the instructions here and get the token for the bot.

Then send a message to the bot and get your chat_id:

 TOKEN="..."
 curl https://api.telegram.org/bot$TOKEN/getUpdates

Configuration

You can configure LoggerTelegramBackend through the application environment. Configure the following options under the LoggerTelegramBackend key of the :logger application. For example, you can do this in config/runtime.exs:

# config/runtime.exs
config :logger, LoggerTelegramBackend,
  chat_id: System.fetch_env!("TELEGRAM_CHAT_ID"),
  token: System.fetch_env!("TELEGRAM_TOKEN"),
  level: :warning,
  # ...

The basic configuration options are:

  • :level (Logger.level/0) - the level to be logged by this backend. Note that messages are first filtered by the general :level configuration for the :logger application. Defaults to nil (all levels are logged).

  • :metadata (list of atom/0 or :all) - the metadata to be included in the message. :all will get all metadata. Defaults to[:line, :function, :module, :application, :file].

  • :metadata_filter (keyword/0, may also include atom/0) - the key-value pairs or keys that is must be present in the metadata for a message to be sent. Defaults to []. See the Filtering Messages section below.

To customize the behaviour of the HTTP client used by LoggerTelegramBackend, you can use these options:

Filtering messages

If you would like to prevent LoggerTelegramBackend from sending certain messages, you can use the :metadata_filter configuration option. It must be configured to be a list of key-value pairs or keys.

Examples

  • metadata_filter: [application: :core] - The metadata must contain application: :core
  • metadata_filter: [:user] - The metadata must contain the key :user, but it can be set to any value
  • metadata_filter: [{:application, :core}, :user] - The metadata must contain application: :core AND must include the key :user

Using a proxy

An HTTP proxy can be configured via the :client_pool_opts option:

config :logger, LoggerTelegramBackend,
  # ...
  client_pool_opts: [conn_opts: [proxy: {:http, "127.0.0.1", 8888, []}]]

See the Pool Configuration Options for further information.

Summary

Functions

Adds the LoggerTelegramBackend backend.

Applies runtime configuration.

Removes the LoggerTelegramBackend backend.

Functions

Link to this function

attach(opts \\ [])

View Source (since 3.0.0)
@spec attach(keyword()) :: Supervisor.on_start_child()

Adds the LoggerTelegramBackend backend.

Options

  • :flush - when true, guarantees all messages currently sent to Logger are processed before the backend is added

Example

iex> LoggerTelegramBackend.attach()
:ok
Link to this function

configure(opts)

View Source (since 3.0.0)
@spec configure(keyword()) :: term()

Applies runtime configuration.

See the module doc for more information.

Example

iex> LoggerTelegramBackend.configure(level: :error)
:ok
Link to this function

detach(opts \\ [])

View Source (since 3.0.0)
@spec detach(keyword()) :: :ok | {:error, term()}

Removes the LoggerTelegramBackend backend.

Options

  • :flush - when true, guarantees all messages currently sent to Logger are processed before the backend is removed

Example

iex> LoggerTelegramBackend.detach()
:ok