Better Stack Erlang logging

Start logging in 3 steps

Collect logs from your Erlang application.

1. Install

Add betterstack_logger to your project dependencies using Rebar or Mix.

rebar.config mix.exs
{deps, [betterstack_logger]}.
def deps do
  [
    {:betterstack_logger, "~> 1.0"}
  ]
end

2. Setup

Configure betterstack_logger in your application's configuration file,or add it programmatically via Logger API:

sys.config call logger:add_handler in your start module
[
  {betterstack_logger, [
      {logger, [
          {handler, betterstack_logs, betterstack_logger, #{
              level => info,
              config => #{
                  http_pool_options => [
                      {timeout, 15000},
                      {max_connections, 10}
                  ],
                  betterstack_source_token => <<"$SOURCE_TOKEN">>,
                  extra_fields => [
                      {<<"_env">>, <<"prod">>}
                  ],
                  upload_batch_max_size => 50,
                  upload_batch_inteval_ms => 5000,
                  upload_failed_retry_count => 3,
                  upload_failed_retry_delay_ms => 1000
              },
              formatter => {
                  logger_formatter, #{
                      single_line => true,
                      template => [pid, " ", mfa,":",line, " => ", msg],
                      time_offset => "Z"
                  }
              },
              filters => [
                  %{remote_group_leader, {fun logger_filters:remote_gl/2, stop}},
                  %{progress, {fun logger_filters:progress/2, stop}},
                  %{sasl, {fun logger_filters:domain/2, {stop, sub, [otp, sasl]}}}
              ]
          }}
      ]}
  ]}
].
logger:add_handler(my_handler, betterstack_logs, #{
    level => info,
    config => #{
        http_pool_options => [
            {timeout, 15000},
            {max_connections, 10}
        ],
        betterstack_source_token => <<"$SOURCE_TOKEN">>,
        extra_fields => [
            {<<"_env">>, <<"prod">>}
        ],
        upload_batch_max_size => 50,
        upload_batch_inteval_ms => 5000,
        upload_failed_retry_count => 3,
        upload_failed_retry_delay_ms => 1000
    },
    formatter => {
        logger_formatter, #{
            single_line => true,
            template => [pid, " ", mfa,":",line, " => ", msg],
            time_offset => "Z"
        }
    },
    filters => [
        %{remote_group_leader, {fun logger_filters:remote_gl/2, stop}},
        %{progress, {fun logger_filters:progress/2, stop}},
        %{sasl, {fun logger_filters:domain/2, {stop, sub, [otp, sasl]}}}
    ]
}).

Using the logger as default handler is not supported.

To avoid cyclic dependency, it's not currently possible to set betterstack_logger as default handler via sys.config.

3. Start logging 🎉

Use the Erlang logger module as usual. Your log events will be sent to Better Stack automatically.

Example usage
logger:info("Application started successfully."),
logger:error("Failed to connect to database."),
logger:debug("Debugging value: ~p", [Value]).

You should see your logs in Better Stack → Live tail.


Huge thanks to Silviu Caragea who maintains the betterstack_logger library as an MIT-licenced open-source! ❤️

Need help?

Please reach out to us at hello@betterstack.com.
We're happy to help! 🙏

Additional information

Ensure your application terminates gracefully, giving the logger enough time to finish sending logs to BetterStack.

Use init:stop() to end your application properly.

Avoid using erlang:halt(), as it terminates all processes immediately and may drop your logs.