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
|
# This file is a part of Julia. License is MIT: https://julialang.org/license
using Test, Logging
import Logging: min_enabled_level, shouldlog, handle_message
@noinline func1() = backtrace()
@testset "Logging" begin
@testset "ConsoleLogger" begin
# First pass log limiting
@test min_enabled_level(ConsoleLogger(devnull, Logging.Debug)) == Logging.Debug
@test min_enabled_level(ConsoleLogger(devnull, Logging.Error)) == Logging.Error
# Second pass log limiting
logger = ConsoleLogger(devnull)
@test shouldlog(logger, Logging.Info, Base, :group, :asdf) === true
handle_message(logger, Logging.Info, "msg", Base, :group, :asdf, "somefile", 1, maxlog=2)
@test shouldlog(logger, Logging.Info, Base, :group, :asdf) === true
handle_message(logger, Logging.Info, "msg", Base, :group, :asdf, "somefile", 1, maxlog=2)
@test shouldlog(logger, Logging.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 = ConsoleLogger(io)
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(Logging.Debug, Base, :g, :i, expanduser("~/somefile.jl"), 42) ==
(:blue, "Debug:", "@ Base ~/somefile.jl:42")
@test Logging.default_metafmt(Logging.Info, Main, :g, :i, "a.jl", 1) ==
(:cyan, "Info:", "")
@test Logging.default_metafmt(Logging.Warn, Main, :g, :i, "b.jl", 2) ==
(:yellow, "Warning:", "@ Main b.jl:2")
@test Logging.default_metafmt(Logging.Error, Main, :g, :i, "", 0) ==
(:light_red, "Error:", "@ Main :0")
# formatting of nothing
@test Logging.default_metafmt(Logging.Warn, nothing, :g, :i, "b.jl", 2) ==
(:yellow, "Warning:", "@ b.jl:2")
@test Logging.default_metafmt(Logging.Warn, Main, :g, :i, nothing, 2) ==
(:yellow, "Warning:", "@ Main")
@test Logging.default_metafmt(Logging.Warn, Main, :g, :i, "b.jl", nothing) ==
(:yellow, "Warning:", "@ Main b.jl")
@test Logging.default_metafmt(Logging.Warn, nothing, :g, :i, nothing, 2) ==
(:yellow, "Warning:", "")
@test Logging.default_metafmt(Logging.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=Logging.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 = ConsoleLogger(io, Logging.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=Logging.Info, meta_formatter=Logging.default_metafmt) ==
"""
[ Info: msg
"""
@test genmsg("line1\nline2", level=Logging.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=Logging.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() at""")
@testset "Limiting large data structures" begin
@test genmsg("msg", a=fill(1.00001, 100,100), b=fill(2.00002, 10,10)) ==
replace("""
┌ PREFIX msg
│ a =
│ 100×100 Array{Float64,2}:
│ 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
│ ⋮ ⋱ EOL
│ 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 Array{Float64,2}:
│ 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
│ ⋮ ⋱ EOL
│ 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
""", "EOL"=>"") # EOL hack to work around git whitespace errors
# Limiting the amount which is printed
@test genmsg("msg", a=fill(1.00001, 10,10), show_limited=false) ==
"""
┌ PREFIX msg
│ a =
│ 10×10 Array{Float64,2}:
│ 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
end
|