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 tonil
(all levels are logged).:metadata
(list ofatom/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 includeatom/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:
:client
(module/0
) - A module that implements theLoggerTelegramBackend.HTTPClient
behaviour. Defaults toLoggerTelegramBackend.HTTPClient.Finch
(requires:finch
).:client_pool_opts
(keyword/0
) - Options to configure the HTTP client pool. SeeFinch.start_link/1
. Defaults to[]
.:client_request_opts
(keyword/0
) - Options passed to theLoggerTelegramBackend.HTTPClient.request/5
callback. SeeFinch.request/3
. Defaults to[]
.
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 containapplication: :core
metadata_filter: [:user]
- The metadata must contain the key:user
, but it can be set to any valuemetadata_filter: [{:application, :core}, :user]
- The metadata must containapplication: :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
@spec attach(keyword()) :: Supervisor.on_start_child()
Adds the LoggerTelegramBackend backend.
Options
:flush
- whentrue
, guarantees all messages currently sent toLogger
are processed before the backend is added
Example
iex> LoggerTelegramBackend.attach()
:ok
Applies runtime configuration.
See the module doc for more information.
Example
iex> LoggerTelegramBackend.configure(level: :error)
:ok
Removes the LoggerTelegramBackend backend.
Options
:flush
- whentrue
, guarantees all messages currently sent toLogger
are processed before the backend is removed
Example
iex> LoggerTelegramBackend.detach()
:ok