View Source CozyLogger.Integrations.Phoenix (cozy_logger v2.0.1)
CozyLogger
integration for Phoenix
.
Phoenix uses the :telemetry
library for instrumentation. The following events
are published by Phoenix:
[:phoenix, :endpoint, :start]
[:phoenix, :endpoint, :stop]
[:phoenix, :router_dispatch, :start]
[:phoenix, :error_rendered]
[:phoenix, :socket_connected]
[:phoenix, :channel_joined]
[:phoenix, :channel_handled_in]
- ...
Phoenix.Logger
(the default logger) handles these events, and print logs accordingly.
Usage
Before using this custom logger, you need to disable the default logger:
config :phoenix, :logger, false
Then, you need to attach events handlers before starting the application:
defmodule Demo
use Application
def start(_type, _args) do
unless Application.fetch_env!(:phoenix, :logger) do
:ok = CozyLogger.Integrations.Phoenix.install()
end
children = [
# ...
]
Supervisor.start_link(children, strategy: :one_for_one, name: Demo.Supervisor)
end
end
Parameter filtering
When logging parameters, CozyLogger.Integrations.Phoenix can filter out sensitive parameters
such as passwords and tokens. Parameters to be filtered can be added via the
:filter_params
option:
CozyLogger.Integrations.Phoenix.install(filter_params: ["password", "secret"])
With the configuration above, CozyLogger.Integrations.Phoenix will filter any parameter that
contains the terms password
or secret
. The match is case sensitive.
The default value of :filter_params
option is ["password"]
.
CozyLogger.Integrations.Phoenix can also filter all parameters by default and selectively keep parameters. This can be configured like so:
CozyLogger.Integrations.Phoenix.install(filter_params: {:keep, ["id", "order"]})
With the configuration above, CozyLogger.Integrations.Phoenix will filter all parameters,
except those that match exactly id
or order
. If a kept parameter matches,
all parameters nested under that one will also be kept.
Dynamic log level
In some cases you may wish to set the log level dynamically on a per-request basis.
To do so, set the :log
option to a tuple, {Mod, Fun, Args}
. The Plug.Conn.t()
for the
request will be prepended to the provided list of arguments.
When invoked, your function must return a Logger.level()
or false
to disable logging for the request.
For example, in your Endpoint you might do something like this:
# lib/demo_web/endpoint.ex
plug Plug.Telemetry,
event_prefix: [:phoenix, :endpoint],
log: {__MODULE__, :log_level, []}
# Disables logging for routes like /status/*
def log_level(%{path_info: ["status" | _]}), do: false
def log_level(_), do: :info
TODO
Currently, only the handler for [:phoenix, :endpoint, :stop]
is implemented.
If you need handlers for other events, feel free to contribute.