This repository has been archived by the owner on Sep 11, 2023. It is now read-only.
/
ink.ex
270 lines (218 loc) · 7.48 KB
/
ink.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
defmodule Ink do
@moduledoc """
A backend for the Elixir `Logger` that logs JSON and filters your secrets.
## Usage
To use `Ink` for your logging, just configure it as a backend:
config :logger, backends: [Ink]
# optional additional configuration
config :logger, Ink,
name: "your app",
level: :info
### Options
In total, the following options are supported by `Ink`:
- `:name` the name of your app that will be added to all logs
- `:io_device` the IO device the logs are written to (default: `:stdio`)
- `:level` the minimum log level for outputting messages (default: `:debug`)
- `:status_mapping` the mapping to use for log statuses (default: `:bunyan`)
- `:filtered_strings` secret strings that should not be printed in logs
(default: `[]`)
- `:filtered_uri_credentials` URIs that contain credentials for filtering
(default: `[]`)
- `:metadata` the metadata keys that should be included in the logs (default:
all)
- `:exclude_hostname` exclude local `hostname` from the log (default:
false)
- `:log_encoding_error` whether to log errors that happen during JSON encoding
(default: true)
### Filtering secrets
`Ink` can be configured to filter secrets out of your logs:
config :logger, Ink,
filtered_strings: ["password"]
Sometimes, you configure a connection using a URL. For example, a RabbitMQ
connection could be configured with the URL
`"amqp://user:password@localhost:5672"`. Filtering the whole URL from your
logs doesn't do you any good. Therefore, `Ink` has a separate option to pass
secret URLs:
config :logger, Ink,
filtered_uri_credentials: ["amqp://user:password@localhost:5672"]
This code will parse the URL and only filter `"password"` from your logs.
#### Preventing reports on the terminal
When processes crash - which is a normal thing to happen in Elixir - OTP sends
reports to the handlers of the `:error_logger`. In some environments, there is
a default handler that prints these to the terminal. Since it includes the
state of the crashed process, this can include secrets from your application.
`Ink` is unable to filter these reports, because they are not printed using
the `Logger`.
You can disable printing of these reports with the following line in your
config:
config :sasl, sasl_error_logger: false
### Metadata
If you don't configure any specific metadata, `Ink` will include all metadata
as separate fields in the logged JSON. If you only want to include specific
metadata in your logs, you need to configure the included fields.
config :logger, Ink,
metadata: [:pid, :my_field]
*Note*: Since the term PID is also prevalent in the UNIX world, services like
LogStash expect an integer if they encounter a field named `pid`. Therefore,
`Ink` will log the PID as `erlang_pid`.
"""
@behaviour :gen_event
def init(__MODULE__) do
{:ok, configure(Application.get_env(:logger, Ink, []), default_options())}
end
def handle_call({:configure, options}, state) do
{:ok, :ok, configure(options, state)}
end
def handle_event({_, gl, {Logger, _, _, _}}, state) when node(gl) != node() do
{:ok, state}
end
def handle_event(:flush, state) do
{:ok, state}
end
def handle_event({level, _, {Logger, message, timestamp, metadata}}, state) do
log_message(message, level, timestamp, metadata, state)
{:ok, state}
end
def handle_info(_msg, state) do
{:ok, state}
end
def terminate(_reason, _state) do
:ok
end
def code_change(_old, state, _extra) do
{:ok, state}
end
defp configure(options, state) do
state
|> Map.merge(Enum.into(options, %{}))
|> update_secret_strings
end
defp log_message(message, level, timestamp, metadata, config) do
if Logger.compare_levels(level, config.level) != :lt do
message
|> base_map(timestamp, level, config)
|> Map.merge(process_metadata(metadata, config))
|> Ink.Encoder.encode()
|> log_json(config)
end
end
defp process_metadata(metadata, config) do
metadata
|> filter_metadata(config)
|> rename_metadata_fields
|> Enum.into(%{})
|> Map.delete(:time)
end
defp filter_metadata(metadata, %{metadata: nil}), do: metadata
defp filter_metadata(metadata, config) do
metadata |> Enum.filter(fn {key, _} -> key in config.metadata end)
end
defp rename_metadata_fields(metadata) do
metadata
|> Enum.map(fn
{:pid, value} -> {:erlang_pid, value}
other -> other
end)
end
defp log_json({:ok, json}, config) do
json
|> filter_secret_strings(config.secret_strings)
|> log_to_device(config.io_device)
end
defp log_json(other, config) do
case config.log_encoding_error do
true -> log_to_device(inspect(other), config.io_device)
_ -> :ok
end
end
defp log_to_device(msg, io_device), do: IO.puts(io_device, msg)
defp base_map(message, timestamp, level, %{exclude_hostname: true} = config)
when is_binary(message) do
%{
name: name(),
pid: System.pid() |> String.to_integer(),
msg: message,
time: formatted_timestamp(timestamp),
level: level(level, config.status_mapping),
v: 0
}
end
defp base_map(message, timestamp, level, config) when is_binary(message) do
%{
name: name(),
pid: System.pid() |> String.to_integer(),
hostname: hostname(),
msg: message,
time: formatted_timestamp(timestamp),
level: level(level, config.status_mapping),
v: 0
}
end
defp base_map(message, timestamp, level, config) when is_list(message) do
base_map(IO.iodata_to_binary(message), timestamp, level, config)
end
defp formatted_timestamp({date, {hours, minutes, seconds, milliseconds}}) do
{date, {hours, minutes, seconds}}
|> NaiveDateTime.from_erl!({milliseconds * 1000, 3})
|> DateTime.from_naive!("Etc/UTC")
|> DateTime.to_iso8601()
end
defp update_secret_strings(config) do
secret_strings =
config.filtered_strings
|> Kernel.++(uri_credentials(config.filtered_uri_credentials))
|> Enum.reject(fn s -> s == "" || is_nil(s) end)
Map.put(config, :secret_strings, secret_strings)
end
defp uri_credentials(uris) do
uris
|> Enum.reject(&is_nil/1)
|> Enum.map(fn uri -> uri |> URI.parse() |> Map.get(:userinfo) end)
|> Enum.reject(&is_nil/1)
|> Enum.map(fn userinfo -> userinfo |> String.split(":") |> List.last() end)
end
defp filter_secret_strings(message, secret_strings) do
Enum.reduce(secret_strings, message, fn secret, msg ->
String.replace(msg, secret, "[FILTERED]")
end)
end
defp default_options do
%{
level: :debug,
status_mapping: :bunyan,
filtered_strings: [],
filtered_uri_credentials: [],
secret_strings: [],
io_device: :stdio,
metadata: nil,
exclude_hostname: false,
log_encoding_error: true
}
end
# https://github.com/trentm/node-bunyan#levels
defp level(level, :bunyan) do
case level do
:debug -> 20
:info -> 30
:warn -> 40
:error -> 50
end
end
# http://erlang.org/documentation/doc-10.0/lib/kernel-6.0/doc/html/logger_chapter.html#log-level
defp level(level, :rfc5424) do
case level do
:debug -> 7
:info -> 6
:warn -> 4
:error -> 3
end
end
defp hostname do
with {:ok, hostname} <- :inet.gethostname(), do: List.to_string(hostname)
end
defp name do
:logger
|> Application.get_env(Ink)
|> Keyword.fetch!(:name)
end
end