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.

rebar.config
{deps, [betterstack_logger]}.

Alternatively, add it to your mix.exs if you're using Mix:

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

2. Setup

Configure betterstack_logger in your application's configuration file:

sys.config
[
  {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]}}}
              ]
          }}
      ]}
  ]}
].

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

Alternatively, you can add it programmatically via Logger API:

Call logger:add_handler in your start module
logger:add_handler(my_handler, betterstack_logs, #{...}).

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 the amazing devs who maintain 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.