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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392 | % %CopyrightBegin%
%
% | /¯\ /¯_ /¯_ |_¯ |¯\ |¯¯ | | ¯|¯ |_¯ |¯\ (`
% |__ \_/ \_/ \_/ |__ |¯\ ___ |¯ | |__ | |__ |¯\ \_) . E R L
%\
%%
%% Copyright Ericsson AB 2017-2018. All Rights Reserved.
%%
%%
%% Whitespace Beautified by ScriptCulture © JANUARY 2019
%% Sit Back · Feet Up · Learn Erlang ¯¯¯¯¯¯¯
%% For use as a reference only
%% www.scriptculture.com
%% Not check-summed
%% kernel-6.1.1
%%
%% Licensed under the Apache License,
%% Version 2.0 (the "License"); you may
%% not use this file except in compliance
%% with the License. You may obtain a copy
%% of the License at:
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by
%% applicable law or agreed to in writing, software
%% distributed under the License is distributed
%% on an "AS IS" BASIS, WITHOUT WARRANTIES
%% OR CONDITIONS OF ANY KIND, either
%% express or implied. See the
%% License for the specific
%% language governing
%% permissions and
%% limitations
%% under the
%% License.
%%%%%%%
%%%%
%%
%
%
%%
%%%
%% %CopyrightEnd%
-module(logger_filters).
-export([ domain/2 % Provides a way of filtering log events based on a domain field in Metadata.
, level/2 % Provides a way of filtering log events based on the log level.
, progress/2 % Stops or allows progress reports from supervisor and application_controller.
, remote_gl/2 % Stops or allows log events originating from a process that has its
]). % group leader on a remote node.
-include("logger_internal.hrl").
-define(IS_ACTION(A), (A==log orelse A==stop)).
%%·% DOMAIN / 2 %·%%
-spec domain(LogEvent,Extra) -> logger:filter_return()
when
LogEvent :: logger:log_event(),
Extra :: {Action,Compare,MatchDomain},
Action :: log
| stop , % If stop is returned, the log event is immediately discarded.
Compare :: super
| sub
| equal
| not_equal
| undefined ,
MatchDomain :: list( atom() ).
% This filter provides a way of filtering
% log events based on a domain field in Metadata.
% This field is optional, and the purpose of using
% it is to group log events from, for example, a specific functional area.
% This allows filtering or other specialized treatment in a Logger handler.
%
% A domain field must be a list of atoms,
% creating smaller and more specialized domains as the list grows longer.
% The greatest domain is [], which comprises all possible domains.
%
% For example, consider the following domains:
%
% D1 = [otp]
% D2 = [otp, sasl]
% D1 is the greatest of the two,
% and is said to be a super-domain of D2.
% D2 is a sub-domain D1. Both D1 and D2 are sub-domains of [].
%
% The above domains are used for logs originating from Erlang/OTP.
% D1 specifies that the log event comes from Erlang/OTP in general,
% & D2 indicates that the log event is a so called SASL report.
%
% The Extra parameter to the domain/2 function is specified when adding
% the filter via logger:add_primary_filter/2 or logger:add_handler_filter/3.
%
% The filter compares the value of the domain field in the log event's
% metadata (Domain)
% against MatchDomain.
% The filter matches if the value of Compare is:
%
% sub
% and Domain is equal to or a sub-domain of MatchDomain,
% that is, if MatchDomain is a prefix of Domain.
%
% super
% and Domain is equal to or a super-domain of MatchDomain,
% that is, if Domain is a prefix of MatchDomain.
%
% equal
% and Domain is equal to MatchDomain.
%
% not_equal
% and Domain differs from MatchDomain, or if there is no domain field in metadata.
%
% undefined
% and there is no domain field in metadata.
% In this case MatchDomain must be set to [].
%
% If the filter matches and Action is log, the log event is allowed.
% If the filter matches and Action is stop, the log event is stopped.
%
% If the filter does not match, it returns ignore,
% meaning that other filters, or the value of the
% configuration parameter filter_default,
% decide if the event is allowed or not.
%
% Log events that do not contain any domain field,
% match only when Compare is equal to undefined or not_equal.
%
% Example: stop all events with domain [otp, sasl | _]
%
% logger:set_handler_config(h1, filter_default, log). %--- this is the default
% Filter = {fun logger_filters:domain/2, {stop, sub, [otp, sasl]}}.
% logger:add_handler_filter(h1, no_sasl, Filter).
% ok
domain( #{ meta := Meta } = LogEvent
, {Action,Compare,MatchDomain}
)
when
?IS_ACTION(Action) andalso
(
Compare==super orelse
Compare==sub orelse
Compare==equal orelse
Compare==not_equal orelse
Compare==undefined
)
andalso
is_list(MatchDomain)
->
filter_domain( Compare
, Meta
, MatchDomain
,on_match(Action,LogEvent)
)
;domain(LogEvent,Extra)
->
erlang:error(badarg,[LogEvent,Extra]).
-spec level(LogEvent,Extra) -> logger:filter_return()
when
LogEvent :: logger:log_event(),
Extra :: {Action,Operator,MatchLevel},
Action :: log
| stop ,
Operator :: neq
| eq
| lt
| gt
| lteq
| gteq ,
MatchLevel :: logger:level().
% This filter provides a way of filtering log events based on the log level.
% It matches log events by comparing the log level with a specified MatchLevel
%
% The Extra parameter is specified when adding
% the filter via logger:add_primary_filter/2
% or logger:add_handler_filter/3.
%
% The filter compares the value of the event's
% log level (Level) to MatchLevel by calling: logger:compare_levels(Level, MatchLevel).
% The filter matches if the value of Operator is:
%
% neq
% and the compare function returns lt or gt.
%
% eq
% and the compare function returns eq.
%
% lt
% and the compare function returns lt.
%
% gt
% and the compare function returns gt.
%
% lteq
% and the compare function returns lt or eq.
%
% gteq
% and the compare function returns gt or eq.
%
% If the filter matches and Action is log , the log event is allowed.
% If the filter matches and Action is stop, the log event is stopped.
%
% If the filter does not match, it returns ignore,
% meaning that other filters, or the value of the configuration
% parameter filter_default, will decide if the event is allowed or not.
%
% Example: only allow debug level log events
%
% logger:set_handler_config(h1, filter_default, stop).
% Filter = {fun logger_filters:level/2, {log, eq, debug}}.
% logger:add_handler_filter(h1, debug_only, Filter).
% ok
level( #{level := L1} = LogEvent, {Action,Op,L2} )
when
?IS_ACTION(Action) andalso
(Op==neq orelse
Op==eq orelse
Op==lt orelse
Op==gt orelse
Op==lteq orelse
Op==gteq) andalso
?IS_LEVEL(L2) ->
filter_level(Op,L1,L2,on_match(Action,LogEvent))
% This filter matches all progress reports from
% supervisor and application_controller.
%
% If Extra is log , the progress reports are allowed.
% If Extra is stop, the progress reports are stopped.
%
% The filter returns ignore for all other log events.
;level( LogEvent, Extra )
->
erlang:error(badarg,[LogEvent, Extra ] ).
-spec progress(LogEvent,Extra) -> logger:filter_return()
when
LogEvent :: logger:log_event(),
Extra :: log
| stop.
% This filter matches all events originating from a
% process that has its group leader on a remote node.
%
% If Extra is log, the matching events are allowed.
% If Extra is stop, the matching events are stopped.
%
% The filter returns ignore for all other log events.
progress( LogEvent,Action)
when ?IS_ACTION(Action) ->
filter_progress(LogEvent,on_match(Action,LogEvent))
;progress(LogEvent,Action) ->
erlang:error(badarg,[LogEvent,Action]).
-spec remote_gl(LogEvent,Extra) -> logger:filter_return()
when
LogEvent :: logger:log_event(),
Extra :: log
| stop .
remote_gl( LogEvent,Action)
when ?IS_ACTION(Action) ->
filter_remote_gl(LogEvent,on_match(Action,LogEvent))
;remote_gl(LogEvent,Action) ->
erlang:error(badarg,[LogEvent,Action]).
%%%-----------------------------------------------------------------
%%% Internal
filter_domain( super ,#{domain:=Domain},MatchDomain,OnMatch)
->
is_prefix(Domain ,MatchDomain,OnMatch)
;filter_domain( sub ,#{domain:=Domain},MatchDomain,OnMatch)
->
is_prefix(MatchDomain,Domain,OnMatch)
;filter_domain( equal ,#{domain:=Domain}, Domain,OnMatch)
->
OnMatch
;filter_domain( not_equal,#{domain:=Domain},MatchDomain,OnMatch)
when Domain
=/= MatchDomain->
OnMatch
;filter_domain( Compare , Meta , _ ,OnMatch)
->
case
maps:is_key( domain, Meta)
of
false when
Compare==undefined
;Compare==not_equal -> OnMatch
; _ -> ignore
end
.
is_prefix(D1,D2,OnMatch)
when
is_list(D1)
,is_list(D2) ->
case
lists:prefix(D1,D2)
of
true -> OnMatch
;false -> ignore
end
;is_prefix(_,_,_) ->
ignore.
filter_level( Op , L1,L2 , OnMatch) ->
case
logger:compare_levels(L1,L2)
of
eq when Op==eq; Op==lteq; Op==gteq -> OnMatch;
lt when Op==lt; Op==lteq; Op== neq -> OnMatch;
gt when Op==gt; Op==gteq; Op== neq -> OnMatch;
_ -> ignore
end
.
filter_progress( #{msg := {report,#{ label:={_,progress} }}} , OnMatch)
->
OnMatch
;filter_progress(_,_)->
ignore.
filter_remote_gl( #{ meta := #{gl:=GL} }, OnMatch)
when node(GL)
=/= node() ->
OnMatch
;filter_remote_gl(_,_)->
ignore.
on_match( log ,LogEvent) -> LogEvent
;on_match(stop, _ ) -> stop
.
|