Logging From Elixir
This documentation outlines how to configure logging with the AppSignal for Elixir integration.
Configure Logging
You do not need to create a log source to send logs from the AppSignal for Elixir integration. An "application" log source will be created automatically.
Logger Handler
You can configure the Erlang :logger
to use the Appsignal.Logger.Handler
module as a handler, by calling the Appsignal.Logger.Handler.add/2
function on your Application.start/2
callback:
@impl true def start(_type, _args) do Appsignal.Logger.Handler.add("phoenix")
The given argument is the group name that describes where you are logging from. This group name can be used in AppSignal to filter your logs.
Logger Backend
Instead of using an Elixir logger handler, you can configure the Elixir logger backend to use the Appsignal.Logger.Backend
module as a backend by placing the code below in the config/runtime.exs
file:
config :logger, backends: [:console, {Appsignal.Logger.Backend, [group: "phoenix"]}]
The group
option describes where you are logging from. This group name can be used in AppSignal to filter your logs.
You can also add the backend using the add_backend/2
function on your Application.start/2
callback:
@impl true def start(_type, _args) do Logger.add_backend(Appsignal.Logger.Backend, group: "phoenix")
Elixir Logger
To send logs to AppSignal without using the Elixir Logger backend, you can use one of Appsignal.Logger
's logging functions and provide it with a groupname
that defines where you are logging from. For example, if we were logging from a helper for invoicing clients:
Appsignal.Logger.info( "invoice_helper", "Generating invoice for customer 129" )
Usage
Sending Logs
Like the Elixir/Phoenix logger class, you can define the severity level of your logs by using fatal
, error
, warning
, info
, and debug
:
Appsignal.Logger.warning( "app", "Something's gone terribly wrong here" )
You can pass custom attributes as the last argument, to send log information that can be used when filtering and querying logs:
Appsignal.Logger.info( "invoice_helper", "Generated invoice for customer #{customer.id}", %{ customer_id: customer.id, invoice_id: invoice.id } )
You can query and filter on message contents and attribute values from within the Log Management tool.
Once configured, the desired attributes will be sent to AppSignal as log_tags
, and be queryable in the AppSignal logging interface.
Logfmt
The AppSignal Logger can also parse attributes out of a Logfmt-formatted line. To do so, pass :logfmt
as the last argument:
Appsignal.Logger.info( "invoice_helper", "Generated invoice invoice_id=A145 for customer customer_id=123", :logfmt )
JSON
The AppSignal Logger can also parse attributes out of a JSON-formatted line. To do so, pass :json
as the last argument:
Appsignal.Logger.info( "invoice_helper", ~s({"message": "Generated invoice for customer", "invoice_id": "A145", "customer_id": "123"}), :json )
Only primitive non-nested values (numbers, strings, booleans and null
) will be shown as log attributes. All other attributes will be discarded.
Filtering Logs
You can use the ignore_logs
configuration option to ignore log lines based on their message. See our Ignore Logs guide to learn more.
Need Help?
After configuring your Elixir application to send logs, logs should appear in AppSignal. If you are unsure about this step or AppSignal is not receiving any logs, you can always reach out for assistance. We'll help get you back on track!