Packages
phauxth
2.0.0-alpha.0
2.5.1
retired
2.5.0
2.4.0
2.3.2
2.3.1
2.3.0
2.2.0
2.1.1
2.1.0
2.0.1
2.0.0
2.0.0-rc.2
2.0.0-rc.1
2.0.0-rc.0
2.0.0-beta.1
2.0.0-beta.0
2.0.0-alpha.5
2.0.0-alpha.4
retired
2.0.0-alpha.3
retired
2.0.0-alpha.2
retired
2.0.0-alpha.1
retired
2.0.0-alpha.0
retired
1.2.9
1.2.8
1.2.7
1.2.6
1.2.5
1.2.4
1.2.3
1.2.1
1.2.0
1.1.3
1.1.2
1.1.1
1.1.0
1.0.3
1.0.2
1.0.1
1.0.0
0.17.1
0.17.0
0.16.0
0.15.1
0.15.0
0.14.0
0.13.0-rc.1
0.13.0-rc.0
0.12.1-rc.1
0.12.1-rc.0
0.12.0-rc
0.11.0
0.10.3
0.10.2
0.10.0
0.9.0
0.8.2
0.8.1
0.8.0
Authentication library for Phoenix, and other Plug-based, web applications
Retired package: Deprecated
Current section
Files
Jump to
Current section
Files
lib/phauxth/log.ex
defmodule Phauxth.Log do
@moduledoc """
Logging functions for Phauxth.
## Format
Phauxth uses logfmt to provide a standard logging format.
15:31:08.575 [warn] user=ray@example.com message="invalid password"
* `:user` - the user identifier (one of email, username, nil)
* `:message` - error / info message
* `:meta` - additional metadata that does not fit into any of the other categories
* this should be a keyword list
## Log levels
The available log levels are :info, :warn and false.
The level at which logging starts can be configured by changing
the `log_level` value in the config file.
The default log_level is :info, but if you only want warnings printed
out, add the following to the config file:
config :phauxth,
log_level: :warn
And if you do not want Phauxth to print out any logs, set the
log_level to false.
"""
require Logger
alias Phauxth.Config
defstruct user: "nil", message: "", meta: []
for level <- [:debug, :info, :warn, :error] do
@doc """
Returns the #{level} log message.
"""
def unquote(level)(%Phauxth.Log{user: user, message: message, meta: meta}) do
if Config.log_level() && Logger.compare_levels(unquote(level), Config.log_level()) != :lt do
Logger.log(unquote(level), fn ->
Enum.map_join([{"user", user}, {"message", message}] ++ meta, " ", &format/1)
end)
end
end
end
@doc """
Returns the id of the currently logged-in user, if present.
"""
@spec current_user_id(map | nil) :: String.t()
def current_user_id(%{current_user: %{id: id}}), do: "#{id}"
def current_user_id(_), do: "nil"
defp format({key, val}) when is_binary(val) do
if String.contains?(val, [" ", "="]) or val == "" do
~s(#{key}="#{val}")
else
~s(#{key}=#{val})
end
end
defp format({key, val}), do: format({key, to_string(val)})
end