-
Notifications
You must be signed in to change notification settings - Fork 57
/
test_websocketlogger.jl
345 lines (319 loc) · 13.4 KB
/
test_websocketlogger.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
# included in runtests.jl
# The first test set is an close adaption of /stdlib/Logging/test/runtests.jl
# The second test set checks additional functionality
using Test
using WebSockets
import WebSockets: Logging
import Logging: min_enabled_level, shouldlog, handle_message, with_logger
import Base.CoreLogging: Info,
Debug,
Warn,
Error,
LogLevel
@noinline func1() = backtrace()
@testset "WebSocketLogger" begin
# First pass log limiting
@test min_enabled_level(WebSocketLogger(devnull, Debug)) == Debug
@test min_enabled_level(WebSocketLogger(devnull, Error)) == Error
# Second pass log limiting
logger = WebSocketLogger(devnull)
@test shouldlog(logger, Info, Base, :group, :asdf) === true
handle_message(logger, Info, "msg", Base, :group, :asdf, "somefile", 1, maxlog=2)
@test shouldlog(logger, Info, Base, :group, :asdf) === true
handle_message(logger, Info, "msg", Base, :group, :asdf, "somefile", 1, maxlog=2)
@test shouldlog(logger, Info, Base, :group, :asdf) === false
# Check that maxlog works without an explicit ID (#28786)
buf = IOBuffer()
io = IOContext(buf, :displaysize=>(30,80), :color=>false)
logger = WebSocketLogger(io)
# This covers an issue (#28786) in stdlib/Julia v 0.7, where log_record_id is not defined
# in the expanded macro. Fixed Nov 2018, labelled for backporting to v1.0.0
with_logger(logger) do
for i in 1:2
@info "test" maxlog=1
end
end
@test String(take!(buf)) ==
"""
[ Info: test
"""
with_logger(logger) do
for i in 1:2
@info "test" maxlog=0
end
end
@test String(take!(buf)) == ""
@testset "Default metadata formatting" begin
@test Logging.default_metafmt(Debug, Base, :g, :i, expanduser("~/somefile.jl"), 42) ==
(:blue, "Debug:", "@ Base ~/somefile.jl:42")
@test Logging.default_metafmt(Info, Main, :g, :i, "a.jl", 1) ==
(:cyan, "Info:", "")
@test Logging.default_metafmt(Warn, Main, :g, :i, "b.jl", 2) ==
(:yellow, "Warning:", "@ Main b.jl:2")
@test Logging.default_metafmt(Error, Main, :g, :i, "", 0) ==
(:light_red, "Error:", "@ Main :0")
# formatting of nothing
@test Logging.default_metafmt(Warn, nothing, :g, :i, "b.jl", 2) ==
(:yellow, "Warning:", "@ b.jl:2")
@test Logging.default_metafmt(Warn, Main, :g, :i, nothing, 2) ==
(:yellow, "Warning:", "@ Main")
@test Logging.default_metafmt(Warn, Main, :g, :i, "b.jl", nothing) ==
(:yellow, "Warning:", "@ Main b.jl")
@test Logging.default_metafmt(Warn, nothing, :g, :i, nothing, 2) ==
(:yellow, "Warning:", "")
@test Logging.default_metafmt(Warn, Main, :g, :i, "b.jl", 2:5) ==
(:yellow, "Warning:", "@ Main b.jl:2-5")
end
function dummy_metafmt(level, _module, group, id, file, line)
:cyan,"PREFIX","SUFFIX"
end
# Log formatting
function genmsg(message; level=Info, _module=Main,
file="some/path.jl", line=101, color=false, width=75,
meta_formatter=dummy_metafmt, show_limited=true,
right_justify=0, kws...)
buf = IOBuffer()
io = IOContext(buf, :displaysize=>(30,width), :color=>color)
logger = WebSocketLogger(io, Debug,
meta_formatter=meta_formatter,
show_limited=show_limited,
right_justify=right_justify)
Logging.handle_message(logger, level, message, _module, :a_group, :an_id,
file, line; kws...)
String(take!(buf))
end
# Basic tests for the default setup
@test genmsg("msg", level=Info, meta_formatter=Logging.default_metafmt) ==
"""
[ Info: msg
"""
@test genmsg("line1\nline2", level=Warn, _module=Base,
file="other.jl", line=42, meta_formatter=Logging.default_metafmt) ==
"""
┌ Warning: line1
│ line2
└ @ Base other.jl:42
"""
# Full metadata formatting
@test genmsg("msg", level=Debug,
meta_formatter=(level, _module, group, id, file, line)->
(:white,"Foo!", "$level $_module $group $id $file $line")) ==
"""
┌ Foo! msg
└ Debug Main a_group an_id some/path.jl 101
"""
@testset "Prefix and suffix layout" begin
@test genmsg("") ==
replace("""
┌ PREFIX EOL
└ SUFFIX
""", "EOL"=>"")
@test genmsg("msg") ==
"""
┌ PREFIX msg
└ SUFFIX
"""
# Behavior with empty prefix / suffix
@test genmsg("msg", meta_formatter=(args...)->(:white, "PREFIX", "")) ==
"""
[ PREFIX msg
"""
@test genmsg("msg", meta_formatter=(args...)->(:white, "", "SUFFIX")) ==
"""
┌ msg
└ SUFFIX
"""
end
@testset "Metadata suffix, right justification" begin
@test genmsg("xxx", width=20, right_justify=200) ==
"""
[ PREFIX xxx SUFFIX
"""
@test genmsg("xxx\nxxx", width=20, right_justify=200) ==
"""
┌ PREFIX xxx
└ xxx SUFFIX
"""
# When adding the suffix would overflow the display width, add it on
# the next line:
@test genmsg("xxxx", width=20, right_justify=200) ==
"""
┌ PREFIX xxxx
└ SUFFIX
"""
# Same for multiline messages
@test genmsg("""xxx
xxxxxxxxxx""", width=20, right_justify=200) ==
"""
┌ PREFIX xxx
└ xxxxxxxxxx SUFFIX
"""
@test genmsg("""xxx
xxxxxxxxxxx""", width=20, right_justify=200) ==
"""
┌ PREFIX xxx
│ xxxxxxxxxxx
└ SUFFIX
"""
# min(right_justify,width) is used
@test genmsg("xxx", width=200, right_justify=20) ==
"""
[ PREFIX xxx SUFFIX
"""
@test genmsg("xxxx", width=200, right_justify=20) ==
"""
┌ PREFIX xxxx
└ SUFFIX
"""
end
# Keywords
@test genmsg("msg", a=1, b="asdf") ==
"""
┌ PREFIX msg
│ a = 1
│ b = "asdf"
└ SUFFIX
"""
# Exceptions shown with showerror
@test genmsg("msg", exception=DivideError()) ==
"""
┌ PREFIX msg
│ exception = DivideError: integer division error
└ SUFFIX
"""
# Attaching backtraces
bt = func1()
@test startswith(genmsg("msg", exception=(DivideError(),bt)),
"""
┌ PREFIX msg
│ exception =
│ DivideError: integer division error
│ Stacktrace:
│ [1] func1()""")
@testset "Limiting large data structures" begin
@test genmsg("msg", a=fill(1.00001, 100,100), b=fill(2.00002, 10,10)) ==
"""
┌ PREFIX msg
│ a =
│ 100×100 $(Matrix{Float64}):
│ 1.00001 1.00001 1.00001 1.00001 … 1.00001 1.00001 1.00001
│ 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001
│ 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001
│ ⋮ ⋱
│ 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001
│ 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001
│ b =
│ 10×10 $(Matrix{Float64}):
│ 2.00002 2.00002 2.00002 2.00002 … 2.00002 2.00002 2.00002
│ 2.00002 2.00002 2.00002 2.00002 2.00002 2.00002 2.00002
│ 2.00002 2.00002 2.00002 2.00002 2.00002 2.00002 2.00002
│ ⋮ ⋱
│ 2.00002 2.00002 2.00002 2.00002 2.00002 2.00002 2.00002
│ 2.00002 2.00002 2.00002 2.00002 2.00002 2.00002 2.00002
└ SUFFIX
"""
# Limiting the amount which is printed
@test genmsg("msg", a=fill(1.00001, 10,10), show_limited=false) ==
"""
┌ PREFIX msg
│ a =
│ 10×10 $(Matrix{Float64}):
│ 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001
│ 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001
│ 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001
│ 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001
│ 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001
│ 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001
│ 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001
│ 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001
│ 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001
│ 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001
└ SUFFIX
"""
end
# Basic colorization test
@test genmsg("line1\nline2", color=true) ==
"""
\e[36m\e[1m┌ \e[22m\e[39m\e[36m\e[1mPREFIX \e[22m\e[39mline1
\e[36m\e[1m│ \e[22m\e[39mline2
\e[36m\e[1m└ \e[22m\e[39m\e[90mSUFFIX\e[39m
"""
end
@testset "WebSocketLogger_special" begin
# Log formatting
function spec_msg(message; level=Info,
color = false,
shouldlog = WebSockets.shouldlog_default)
buf = IOBuffer()
io = IOContext(buf, :color => color)
logger = WebSocketLogger(io, Debug,
shouldlog = shouldlog)
with_logger(logger) do
if level == Info
@info message
elseif level == Wslog
@wslog message
elseif level == Warn
@warn message
elseif level == Debug
@debug message
else
@error message
end
end
String(take!(buf))
end
# Normal info
@test spec_msg("---") == "[ Info: ---\n"
# Debug, own format
msg = spec_msg("---", level = Debug)
@test startswith(msg, "┌ Debug: ---\n└ ")
@test '@' in msg
# Loglevel Wslog, own format
# This covers an issue related to #28786 in stdlib/Julia v 0.7, where log_record_id is not defined
# in the expanded macro. Why it is triggered here, but not in the LogLevel Info test is unknown.
msg = spec_msg("---", level = Wslog)
@test startswith(msg, "[ Wslog ")
@test endswith(msg, ": ---\n")
# Blocking all log levels except Wslog.
spec_shouldlog(logger, level, _module, group, id) = level == Wslog
# Issue 28786
msg = spec_msg("---", level = Wslog, shouldlog = spec_shouldlog)
@test startswith(msg, "[ Wslog ")
@test endswith(msg, ": ---\n")
#
@test spec_msg("---", shouldlog = spec_shouldlog) == ""
@test spec_msg("---", level = Debug, shouldlog = spec_shouldlog) == ""
@test spec_msg("---", level = Warn, shouldlog = spec_shouldlog) == ""
@test spec_msg("---", level = Error, shouldlog = spec_shouldlog) == ""
# Check two lines output for all except log level Info and Wslog
@test startswith(spec_msg("---", level = Debug), "┌ Debug: ---\n└ ")
@test startswith(spec_msg("---", level = Warn), "┌ Warn: ---\n└ ")
@test startswith(spec_msg("---", level = Error), "┌ Error: ---\n└ ")
# Check colors in first argument show methods are preserved
ws = WebSocket(IOBuffer(), true)
@test spec_msg(ws) == "[ Info: WebSocket{GenericIOBuffer}(server, CONNECTED)\n"
@test spec_msg(ws, color= true) == "\e[36m\e[1m[ \e[22m\e[39m\e[36m\e[1mInfo: \e[22m\e[39mWebSocket{GenericIOBuffer}(server, \e[32mCONNECTED\e[39m)\n"
# Check tuples in first argument, resembling println(arg1, arg2)
@test spec_msg(("Now testing ", ws), color=true) == "\e[36m\e[1m[ \e[22m\e[39m\e[36m\e[1mInfo: \e[22m\e[39mNow testing WebSocket{GenericIOBuffer}(server, \e[32mCONNECTED\e[39m)\n"
# Check tuples in first argument, resembling println(arg1, arg2)
@test spec_msg(("Now testing ", ws), color=true) == "\e[36m\e[1m[ \e[22m\e[39m\e[36m\e[1mInfo: \e[22m\e[39mNow testing WebSocket{GenericIOBuffer}(server, \e[32mCONNECTED\e[39m)\n"
# Check default shouldlog filters away WebSockets. HTTP.Servers, i.e. HTTP.Servers
logger = WebSocketLogger()
@test shouldlog(logger, Info, WebSockets.HTTP.Servers, :group, :asdf) == false
@test shouldlog(logger, Info, Main, :group, :asdf) == true
# Check that error handling works with @wslog
# This covers an issue (#28786) in stdlib/Julia v 0.7, where log_record_id is not defined
# in the expanded macro. Fixed Nov 2018, labelled for backporting to v1.0.0
buf = IOBuffer()
io = IOContext(buf, :displaysize=>(30,80), :color=>true)
logger = WebSocketLogger(io)
with_logger(logger) do
@wslog sqrt(-2)
end
@test length(String(take!(buf))) > 1900
"""
[ Info: test
"""
end
nothing