-
Notifications
You must be signed in to change notification settings - Fork 14
/
handlers.jl
381 lines (318 loc) · 12.1 KB
/
handlers.jl
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
@testset "Handlers" begin
FMT_STR = "[{level}]:{name} - {msg}"
LEVELS = Dict(
"not_set" => 0,
"debug" => 10,
"info" => 20,
"warn" => 30,
"error" => 40,
"fubar" => 50
)
@testset "Custom Handlers" begin
@testset "SimplestHandler" begin
io = IOBuffer()
try
handler = SimplestHandler(io)
logger = Logger(
"SimplestHandler",
Dict("Buffer" => handler),
"info",
LEVELS,
DefaultRecord,
true
)
@test getlevel(handler) == "not_set"
@test isempty(getfilters(handler))
msg = "It works!"
Memento.info(logger, msg)
@test occursin(msg, String(take!(io)))
Memento.debug(logger, "This shouldn't get logged")
@test isempty(String(take!(io)))
finally
close(io)
end
end
@testset "FilterHandler" begin
io = IOBuffer()
try
handler = FilterHandler(io)
handler_filter = Memento.Filter(handler)
push!(handler, handler_filter)
logger = Logger(
"FilterHandler",
Dict("Buffer" => handler),
"info",
LEVELS,
DefaultRecord,
true
)
@test getlevel(handler) == "not_set"
@test getfilters(handler) == [handler_filter]
msg = "It works!"
Memento.info(logger, msg)
@test occursin(msg, String(take!(io)))
Memento.debug(logger, "This shouldn't get logged")
@test isempty(String(take!(io)))
finally
close(io)
end
end
@testset "AsyncHandler" begin
io = IOBuffer()
function wait_for_empty(c::Channel)
while isready(c)
sleep(1)
end
end
try
handler = AsyncHandler(io)
logger = Logger(
"AsyncHandler",
Dict("Buffer" => handler),
"info",
LEVELS,
DefaultRecord,
true
)
@test getlevel(handler) == "not_set"
@test isempty(getfilters(handler))
msg = "It works!"
Memento.info(logger, msg)
wait_for_empty(handler.channel)
@test occursin(msg, String(take!(io)))
Memento.debug(logger, "This shouldn't get logged")
wait_for_empty(handler.channel)
@test isempty(String(take!(io)))
# Asynchronous handlers can cause issues when loggers are used across
# multiple processes. We'll emulate the following issue:
#=
using Distributed, Memento
const LOGGER = getlogger()
addprocs(1)
@everywhere using Memento
# Note: Define AsyncHandler on all processes
@everywhere push!(getlogger(), AsyncHandler(IOBuffer()))
pmap(1:3) do i
Memento.info(LOGGER, i) # ERROR: cannot serialize a running Task
end
=#
try
serialize(io, handler)
@test false
catch e
@test e isa ErrorException
@test e.msg == "cannot serialize a running Task"
end
@test_throws LoggerSerializationError serialize(io, logger)
finally
close(io)
end
end
end
@testset "DefaultHandler" begin
@testset "Sample Usage w/ IO" begin
io = IOBuffer()
try
handler = DefaultHandler(io, DefaultFormatter(FMT_STR))
logger = Logger(
"DefaultHandler.sample_io",
Dict("Buffer" => handler),
"info",
LEVELS,
DefaultRecord,
true
)
@test logger.name == "DefaultHandler.sample_io"
@test logger.level == "info"
msg = "It works!"
Memento.info(logger, msg)
@test occursin("[info]:$(logger.name) - $msg", String(take!(io)))
Memento.debug(logger, "This shouldn't get logged")
@test isempty(String(take!(io)))
msg = "Something went very wrong"
log(logger, "fubar", msg)
@test occursin("[fubar]:$(logger.name) - $msg", String(take!(io)))
finally
close(io)
end
end
@testset "Sample Usage w/ File" begin
filename = tempname()
@info("Path to log file: $filename")
handler = DefaultHandler(filename)
logger = Logger(
"DefaultHandler.sample_file",
Dict("File" => handler),
"info",
LEVELS,
DefaultRecord,
true
)
@test logger.name == "DefaultHandler.sample_file"
@test logger.level == "info"
Memento.info(logger, "It works!")
Memento.debug(logger, "This shouldn't get logged")
log(logger, "fubar", "Something went very wrong")
@test isfile(filename)
@test Sys.iswindows() ? true : success(`rm $filename`)
end
@testset "IO Construction" begin
io = IOBuffer()
try
handler1 = DefaultHandler(io)
@test handler1.fmt.fmt_str == Memento.DEFAULT_FMT_STRING
@test isempty(String(take!(handler1.io)))
@test !(handler1.opts[:is_colorized])
handler2 = DefaultHandler(
io,
DefaultFormatter(FMT_STR),
Dict{Symbol, Any}(:is_colorized => true)
)
@test handler2.fmt.fmt_str == FMT_STR
@test handler2.opts[:is_colorized]
@test haskey(handler2.opts, :colors)
@test haskey(handler2.opts[:colors], "debug")
@test handler2.opts[:colors]["debug"] == :blue
finally
close(io)
end
end
@testset "Filename Construction" begin
filename = tempname()
@info("Path to log file: $filename")
handler1 = DefaultHandler(filename)
@test handler1.fmt.fmt_str == Memento.DEFAULT_FMT_STRING
@test !(handler1.opts[:is_colorized])
handler2 = DefaultHandler(
filename,
DefaultFormatter(FMT_STR),
Dict{Symbol, Any}(:is_colorized => true)
)
@test handler2.fmt.fmt_str == FMT_STR
@test handler2.opts[:is_colorized]
@test haskey(handler2.opts, :colors)
@test haskey(handler2.opts[:colors], "debug")
@test handler2.opts[:colors]["debug"] == :blue
end
@testset "Colors" begin
io = IOBuffer()
try
handler = DefaultHandler(
io, DefaultFormatter(FMT_STR),
Dict{Symbol, Any}(
:is_colorized => true,
:colors => Dict{AbstractString, Symbol}(
"debug" => :black,
"info" => :blue,
"notice" => :green,
"warn" => :cyan,
"error" => :magenta,
"critical" => :red,
"alert" => :yellow,
"emergency" => :white,
)
)
)
logger = Logger(
"DefaultHandler.sample_io",
Dict("Buffer" => handler),
"info",
LEVELS,
DefaultRecord,
true
)
@test logger.name == "DefaultHandler.sample_io"
@test logger.level == "info"
msg = "It works!"
Memento.info(logger, msg)
@test occursin("[info]:$(logger.name) - $msg", String(take!(io)))
Memento.debug(logger, "This shouldn't get logged")
@test isempty(String(take!(io)))
msg = "Something went very wrong"
log(logger, "fubar", msg)
@test occursin("[fubar]:$(logger.name) - $msg", String(take!(io)))
finally
close(io)
end
end
@testset "Level Filter" begin
io = IOBuffer()
try
handler = DefaultHandler(io, DefaultFormatter(FMT_STR))
logger = Logger(
"DefaultHandler.sample_io",
Dict("Buffer" => handler),
"info",
LEVELS,
DefaultRecord,
true
)
@test logger.name == "DefaultHandler.sample_io"
@test logger.level == "info"
msg = "It works!"
Memento.info(logger, msg)
@test occursin("[info]:$(logger.name) - $msg", String(take!(io)))
# Filter out log messages < LEVELS["warn"]
setlevel!(handler, "warn")
# add_filter(
# handler,
# Filter((rec) -> rec.levelnum >= LEVELS["warn"])
# )
Memento.info(logger, "This shouldn't get logged")
@test isempty(String(take!(io)))
finally
close(io)
end
end
end
@testset "Escalator" begin
@testset "Basic" begin
# Using default behaviour to convert warnings to errors
handler = Escalator()
logger = Logger(
"Escalator.basic",
Dict("Escalator" => handler),
"info",
LEVELS,
DefaultRecord,
true,
)
# Info messages should work fine as no-ops
info(logger, "Hello World!")
# Test that we throw an escalation error for warnings
@test_throws Memento.EscalationError warn(logger, "Goodbye World!")
end
@testset "info" begin
# Using default behaviour to convert notice and warnings to errors
handler = Escalator()
setlevel!(handler, "info")
logger = Logger(
"Escalator.basic",
Dict("Escalator" => handler),
"info",
LEVELS,
DefaultRecord,
true,
)
# Test that we throw an escalation error for info messages
@test_throws Memento.EscalationError info(logger, "Goodbye World!")
# Test that we throw an escalation error for warnings
@test_throws Memento.EscalationError warn(logger, "Goodbye World!")
end
@testset "stdlib" begin
# Test escalating base logging records
orig_logger = Base.CoreLogging.global_logger()
base_logger = Memento.BaseLogger(min_enabled_level(global_logger()))
logger = getlogger(string(@__MODULE__))
try
global_logger(base_logger)
push!(logger, Escalator())
@test_throws Memento.EscalationError @warn("Goodbye World!")
finally
# Remove the logger and replace the core logging
delete!(Memento._loggers, string(@__MODULE__))
global_logger(orig_logger)
end
end
end
end