Elixir app > Logger > StathamLogger Logger Backend > :stdout > Datadog Agent > Datadog
A backend for the Elixir Logger that:
- transforms logged metadata, by hiding sensitive values and trimming long strings
- outputs JSON string, structured according to Datadog attributest list
def deps do
[
{:statham_logger, github: "prosapient/statham_logger"}
]
end- Use
StathamLoggerLogger backend, instead of default Console backend inconfig.exs:
config :logger,
backends: [StathamLogger]- Add
StathamLogger.ExceptionCapturePlugtoendpoint.exbeforeuse Phoenix.Endpoint.
defmodule MyApp.Endpoint
use StathamLogger.ExceptionCapturePlug
use Phoenix.Endpoint, otp_app: :my_app
# ...
end- Configure
StathamLogger:
config :logger, StathamLogger,
metadata: :all,
sanitize_options: [
filter_keys: {:discard, ~w(password other_sensitive_key)},
max_string_size: 100
]Given this configuration, logged medatada will be sanitized:
passwordandother_sensitive_keyvalues replaced with"[FILTERED]"- string values truncated to 100 characters
iex> require Logger
iex> Logger.remove_backend(:console)
iex> Logger.add_backend(StathamLogger)
iex> Logger.configure_backend(StathamLogger, metadata: :all)iex> Logger.metadata(metadata_field_1: "metadata_value_1")
iex> Logger.debug("hello")
# Output
{"file":"/some_file.ex","function":"say_hello/0","line":67,"logger":{"thread_name":"#PID<0.222.0>","method_name":"HelloModule.say_hello/0"},"message":"hello","metadata_field_1":"metadata_value_1","mfa":["HelloModule","say_hello",0],"module":"HelloModule","pid":"#PID<0.222.0>","syslog":{"hostname":"mb","severity":"debug","timestamp":"2021-10-07T10:20:17.902Z"}}- Configure groups
config :logger, level: :info
config :logger, StathamLogger,
groups: %{
detailed_logs: [
level: :debug
]
}- Use group
Logger.metadata(statham_logger_group: :detailed_logs)
# message is logged, because :detailed_logs level (:debug) overwrites :logger level (:info)
Logger.debug("hello")- Store things like current user (
logger_context.user) and request details (logger_context.http) in Logger metadata underlogger_contextkey.StathamLoggerlooks inlogger_contextto set standard attributes values (seeStathamLogger.DatadogFormatterfor details).
See the StathamLogger.Loggable documentation.
- install Datadog agent https://docs.datadoghq.com/agent/
- start agent
datadog-agent start - start agent gui
datadog-agent launch-gui - see Status->Collector page of GUI to confirm
datadog-agentis working correctly - see Status->General->AgentInfo page of GUI for configuration paths (
Config File,Conf.d Pathetc.)
- Update Datadog
Config File, or in GUI Settings:
api_key: [develpment api key from https://app.datadoghq.com/organization-settings/api-keys]
apm_config:
enabled: true- In your app, make sure tracing is enabled
- Change
MyApp.Tracer.Adapter.adapter_opts()toMyApp.Tracer.Adapter.adapter_opts(verbose: true)inMyApp.Tracermodule - Run in iex:
1..10
|> Enum.map(fn i ->
Task.async(fn ->
MyApp.Tracer.start_trace("span_name#{i}", service: :my_app_dev, type: :custom)
Process.sleep(5_000)
MyApp.Tracer.finish_trace()
end)
end)
|> Enum.map(&Task.await(&1, :infinity))- Observe traces received by Datadog
In staging/production Datadog Agent is using output of :stdout.
In development writing/reading logs to/from file should be used instead.
- Enable logs in Datadog
Config File, or in GUI Settings:
api_key: [develpment api key from https://app.datadoghq.com/organization-settings/api-keys]
logs_enabled: true- Follow the custom log collection setup:
- in
Conf.d Pathdirectory, create<CUSTOM_LOG_SOURCE>.ddirectory - inside
<CUSTOM_LOG_SOURCE>.d, createconf.yamlfile:
logs:
- type: file
path: <CUSTOM_PATH>.log
service: custom-service
source: custom-source- Start your app that has
statham_loggerdependency, and is using StathamLogger Logger backend withmix phx.server > <CUSTOM_PATH>.log - Start Datadog Agent
datadog-agent start - Generate some Logs in your app
- Observe logs received by Datadog Agent:
datadog-agent stream-logs - Observe logs received by Datadog
- To see all logs, filter Live Tail by host, for example (https://app.datadoghq.com/logs/livetail?query=host%3Amb)
Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/statham_logger.