View Source LoggerTelegramBackend (logger_telegram_backend v3.0.0-rc.2)
A logger backend for Telegram.
Installation
Add :logger_telegram_backend and :finch to your list of dependencies in mix.exs:
def deps do
[
{:logger_telegram_backend, "~> 3.0-rc"},
{:finch, "~> 0.16"},
]
endIn your Application.start/2 callback, add the LoggerTelegramBackend backend:
@impl true
def start(_type, _args) do
LoggerTelegramBackend)
# ...
endConfiguration
First you need to create a Telegram Bot. Follow the instructions here to create one and get the token for the bot. You will need to send a message first as bots are not allowed to contact users. Then 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"Options
The available options are:
:level- the level to be logged by this backend (either:debug,:info,:warningor:error).Note that messages are first filtered by the general
:levelconfiguration for the:loggerapplication.Default:
nil(all levels are logged):metadata- the metadata to be included in the message. Setting:metadatato:allwill get all metadata. Default:[:line, :function, :module, :application, :file].:metadata_filter- the metadata which is required in order for a message to be logged.Default:
nil:client- If you need different functionality for the HTTP client, you can define your own module that implements theLoggerTelegramBackend.HTTPClientbehaviour and setclientto that module.Default:
LoggerTelegramBackend.HTTPClient.Finch(requires:finch):client_pool_opts- The options passed to configure the pool.See Finch.start_link/1.
Default:
[]:client_request_opts- The options passed on every request.See Finch.request/3.
Default:
[]
Examples
Custom HTTP client
Add a module that implements the
LoggerTelegramBackend.HTTPClientbehaviourPass your client module to the
:clientoption:config :logger, LoggerTelegramBackend, client: MyClient, # ...
Hackney Client
A client based on :hackney could look like this:
defmodule HackneyClient do
@behaviour LoggerTelegramBackend.HTTPClient
@hackney_pool_name :logger_telegram_backend_pool
@impl true
def child_spec do
:hackney_pool.child_spec(@hackney_pool_name, opts)
end
@impl true
def request(method, url, headers, body) do
opts = [{:pool, @hackney_pool_name} :with_body]
case :hackney.request(method, url, headers, body, opts) do
{:ok, _status, _headers, _body} = result -> result
{:error, _reason} = error -> error
end
end
endMetadata filter
config :logger, LoggerTelegramBackend,
chat_id: "$chatId",
token: "$botToken",
level: :info,
metadata: :all,
metadata_filter: [application: :ui]HTTP proxy
config :logger, LoggerTelegramBackend,
chat_id: "$chatId",
token: "$botToken",
client_pool_opts: [conn_opts: [{: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 toLoggerare processed before the backend is added
Applies runtime configuration.
See the module doc for more information.
Removes the LoggerTelegramBackend backend.
Options
:flush- whentrue, guarantees all messages currently sent toLoggerare processed before the backend is removed