forked from bleacherreport/plug_logger_json
/
logger_json.ex
255 lines (212 loc) · 7.13 KB
/
logger_json.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
defmodule Plug.LoggerJSON do
@moduledoc """
A plug for logging basic request information in the format:
```json
{
"api_version": "N/A"
"client_ip": "23.235.46.37"
"client_version": "ios/1.6.7",
"date_time": "2016-05-31T18:00:13Z",
"duration": 4.670,
"handler": "fronts#index"
"log_type": "http",
"method": "POST",
"params": {
"user":"jkelly",
"password":"[FILTERED]"
},
"path": "/",
"request_id": "d90jcl66vp09r8tke3utjsd1pjrg4ln8",
"status": "200"
}
```
To use it, just plug it into the desired module.
plug Plug.LoggerJSON, log: :debug
## Options
* `:log` - The log level at which this plug should log its request info.
Default is `:info`.
* `:extra_attributes_fn` - Function to call with `conn` to add additional
fields to the requests. Default is `nil`. Please see "Extra Fields" section
for more information.
## Extra Fields
Additional data can be logged alongside the request by specifying a function
to call which returns a map:
def extra_attributes(conn) do
map = %{
"user_id" => get_in(conn.assigns, [:user, :user_id]),
"other_id" => get_in(conn.private, [:private_resource, :id]),
"should_not_appear" => conn.private[:does_not_exist]
}
map
|> Enum.filter(&(&1 !== nil))
|> Enum.into(%{})
end
plug Plug.LoggerJSON, log: Logger.level,
extra_attributes_fn: &MyPlug.extra_attributes/1
In this example, the `:user_id` is retrieved from `conn.assigns.user.user_id`
and added to the log if it exists. In the example, any values that are `nil`
are filtered from the map. It is a requirement that the value is
serialiazable as JSON by the Poison library, otherwise an error will be raised
when attempting to encode the value.
"""
alias Plug.Conn
@behaviour Plug
require Logger
@typedoc """
Type for a plug option
"""
@type opts :: binary | tuple | atom | integer | float | [opts] | %{opts => opts}
@typedoc """
Type for time
"""
@type time :: {non_neg_integer(), non_neg_integer(), non_neg_integer()}
@spec init(opts) :: opts
def init(opts), do: opts
@spec call(Plug.Conn.t(), opts) :: Plug.Conn.t()
def call(conn, level_or_opts) when is_atom(level_or_opts) do
call(conn, level: level_or_opts)
end
def call(conn, opts) do
level = Keyword.get(opts, :log, :info)
start = :os.timestamp()
Conn.register_before_send(conn, fn conn ->
:ok = log(conn, level, start, opts)
conn
end)
end
@spec log(Plug.Conn.t(), atom(), time(), opts) :: atom() | no_return()
def log(conn, level, start, opts \\ [])
def log(conn, :error, start, opts), do: log(conn, :info, start, opts)
def log(conn, :info, start, opts), do: log_message(conn, :info, start, opts)
def log(conn, :warn, start, opts), do: log(conn, :debug, start, opts)
def log(conn, :debug, start, opts) do
log_message(conn, :info, start, Keyword.put_new(opts, :include_debug_logging, true))
end
@spec log_error(atom(), map(), list()) :: atom()
def log_error(kind, reason, stacktrace) do
_ =
Logger.log(:error, fn ->
%{
"log_type" => "error",
"message" => Exception.format(kind, reason, stacktrace),
"request_id" => Logger.metadata()[:request_id]
}
|> Poison.encode!()
end)
end
@spec log_message(Plug.Conn.t(), atom(), time(), opts) :: atom()
defp log_message(conn, level, start, opts) do
Logger.log(level, fn ->
conn
|> basic_logging(start)
|> Map.merge(debug_logging(conn, opts))
|> Map.merge(phoenix_attributes(conn))
|> Map.merge(extra_attributes(conn, opts))
|> Poison.encode!()
end)
end
defp basic_logging(conn, start) do
stop = :os.timestamp()
duration = :timer.now_diff(stop, start)
req_id = Logger.metadata()[:request_id]
req_headers = format_map_list(conn.req_headers)
log_json = %{
"api_version" => Map.get(req_headers, "accept", "N/A"),
"date_time" => iso8601(:calendar.now_to_datetime(:os.timestamp())),
"duration" => Float.round(duration / 1000, 3),
"log_type" => "http",
"method" => conn.method,
"path" => conn.request_path,
"request_id" => req_id,
"status" => conn.status
}
Map.drop(log_json, Application.get_env(:plug_logger_json, :suppressed_keys, []))
end
defp extra_attributes(conn, opts) do
case Keyword.get(opts, :extra_attributes_fn) do
fun when is_function(fun) -> fun.(conn)
_ -> %{}
end
end
@spec client_version(%{String.t() => String.t()}) :: String.t()
defp client_version(headers) do
headers
|> Map.get("x-client-version", "N/A")
|> case do
"N/A" ->
Map.get(headers, "user-agent", "N/A")
accept_value ->
accept_value
end
end
@spec debug_logging(Plug.Conn.t(), opts) :: map()
defp debug_logging(conn, opts) do
case Keyword.get(opts, :include_debug_logging) do
true ->
req_headers = format_map_list(conn.req_headers)
%{
"client_ip" => format_ip(Map.get(req_headers, "x-forwarded-for", "N/A")),
"client_version" => client_version(req_headers),
"params" => format_map_list(conn.params)
}
_ ->
%{}
end
end
@spec filter_values({String.t(), String.t()}) :: map()
defp filter_values({k, v}) when is_map(v) do
Map.put(%{}, k, filter_values(v))
end
defp filter_values(kv) when is_map(kv) do
Enum.map(kv, &filter_values/1)
|> Enum.reduce(%{}, &Map.merge(&2, &1))
end
defp filter_values({k, v}) do
filtered_keys = Application.get_env(:plug_logger_json, :filtered_keys, [])
if Enum.member?(filtered_keys, k) do
%{k => "[FILTERED]"}
else
%{k => format_value(v)}
end
end
@spec format_ip(String.t()) :: String.t()
defp format_ip("N/A") do
"N/A"
end
defp format_ip(x_forwarded_for) do
hd(String.split(x_forwarded_for, ", "))
end
@spec format_map_list([%{String.t() => String.t()}]) :: map()
defp format_map_list(list) do
list
|> Enum.take(20)
|> Enum.map(&filter_values/1)
|> Enum.reduce(%{}, &Map.merge(&2, &1))
end
defp format_value(value) when is_binary(value) do
String.slice(value, 0..500)
end
defp format_value(value) do
value
end
defp iso8601({{year, month, day}, {hour, minute, second}}) do
zero_pad(year, 4) <>
"-" <>
zero_pad(month, 2) <>
"-" <>
zero_pad(day, 2) <>
"T" <> zero_pad(hour, 2) <> ":" <> zero_pad(minute, 2) <> ":" <> zero_pad(second, 2) <> "Z"
end
@spec phoenix_attributes(map()) :: map()
defp phoenix_attributes(%{private: %{phoenix_controller: controller, phoenix_action: action}}) do
%{"handler" => "#{controller}##{action}"}
end
defp phoenix_attributes(_) do
%{"handler" => "N/A"}
end
@spec zero_pad(1..3_000, non_neg_integer()) :: String.t()
defp zero_pad(val, count) do
num = Integer.to_string(val)
:binary.copy("0", count - byte_size(num)) <> num
end
end