Skip to content

Commit

Permalink
some logging things
Browse files Browse the repository at this point in the history
 - use user customizable colors
 - use showerror to print exceptions rather than their string representation
 - nice box drawing chars
 - print line info after key value pairs
 - always put the line info on its own line for multiline messages
 - indent key-value pair
 - print line info in grey
  • Loading branch information
fredrikekre committed Dec 22, 2017
1 parent eec85b2 commit b552bba
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 22 deletions.
2 changes: 2 additions & 0 deletions base/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ have_color = false
default_color_warn = :yellow
default_color_error = :light_red
default_color_info = :cyan
default_color_debug = :blue
default_color_input = :normal
default_color_answer = :normal
color_normal = text_colors[:normal]
Expand All @@ -83,6 +84,7 @@ end
error_color() = repl_color("JULIA_ERROR_COLOR", default_color_error)
warn_color() = repl_color("JULIA_WARN_COLOR" , default_color_warn)
info_color() = repl_color("JULIA_INFO_COLOR" , default_color_info)
debug_color() = repl_color("JULIA_DEBUG_COLOR" , default_color_debug)

input_color() = text_colors[repl_color("JULIA_INPUT_COLOR", default_color_input)]
answer_color() = text_colors[repl_color("JULIA_ANSWER_COLOR", default_color_answer)]
Expand Down
40 changes: 24 additions & 16 deletions base/logging.jl
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,9 @@ end
# Try really hard to get the message to the logger, with
# progressively less information.
try
msg = "Exception while generating log record in module $_module at $filepath:$line"
handle_message(logger, Error, msg, _module, group, id, filepath, line; exception=err)
msg = "Exception while generating log record:\n"
msg *= join(" " .* split(sprint(showerror, err), "\n"), "\n")
handle_message(logger, Error, msg, _module, group, id, filepath, line)
catch err2
try
# Give up and write to STDERR, in three independent calls to
Expand Down Expand Up @@ -483,23 +484,30 @@ function handle_message(logger::SimpleLogger, level, message, _module, group, id
logger.message_limits[id] = remaining - 1
remaining > 0 || return
end
levelstr = string(level)
color = level < Info ? :blue :
level < Warn ? :cyan :
level < Error ? :yellow : :red
levelstr, color = level < Info ? ("Debug", Base.debug_color()) :
level < Warn ? ("Info", Base.info_color()) :
level < Error ? ("Warning", Base.warn_color()) :
("Error", Base.error_color())
buf = IOBuffer()
iob = IOContext(buf, logger.stream)
print_with_color(color, iob, first(levelstr), "- ", bold=true)
msglines = split(string(message), '\n')
for i in 1:length(msglines)-1
println(iob, msglines[i])
print_with_color(color, iob, "| ", bold=true)
end
println(iob, msglines[end], " -", levelstr, ":", _module, ":", basename(filepath), ":", line)
for (key,val) in pairs(kwargs)
print_with_color(color, iob, "| ", bold=true)
println(iob, key, " = ", val)
msglines = split(chomp(string(message)), '\n')
if length(msglines) + length(kwargs) == 1
print_with_color(color, iob, "[ ", levelstr, ": ", bold=true)
print(iob, msglines[1], " ")
else
print_with_color(color, iob, "", levelstr, ": ", bold=true)
println(iob, msglines[1])
for i in 2:length(msglines)
print_with_color(color, iob, "", bold=true)
println(iob, msglines[i])
end
for (key,val) in pairs(kwargs)
print_with_color(color, iob, "", bold=true)
println(iob, " ", key, " = ", val)
end
print_with_color(color, iob, "", bold=true)
end
print_with_color(:light_black, iob, "@ ", _module, " ", basename(filepath), ":", line, "\n")
write(logger.stream, take!(buf))
nothing
end
Expand Down
1 change: 1 addition & 0 deletions base/replutil.jl
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ function showerror(io::IO, ex::DomainError, bt; backtrace=true)
backtrace && show_backtrace(io, bt)
nothing
end
showerror(io::IO, ex::DomainError) = showerror(io, ex, [])

function showerror(io::IO, ex::SystemError)
if ex.extrainfo === nothing
Expand Down
14 changes: 8 additions & 6 deletions test/logging.jl
Original file line number Diff line number Diff line change
Expand Up @@ -249,22 +249,24 @@ end
# Simple
@test genmsg(Info, "msg", Main, "some/path.jl", 101) ==
"""
I- msg -Info:Main:path.jl:101
[ Info: msg @ Main path.jl:101
"""

# Multiline message
@test genmsg(Warn, "line1\nline2", Main, "some/path.jl", 101) ==
"""
W- line1
| line2 -Warn:Main:path.jl:101
┌ Warning: line1
│ line2
└ @ Main path.jl:101
"""

# Keywords
@test genmsg(Error, "msg", Base, "other.jl", 101, a=1, b="asdf") ==
"""
E- msg -Error:Base:other.jl:101
| a = 1
| b = asdf
┌ Error: msg
│ a = 1
│ b = asdf
└ @ Base other.jl:101
"""
end

Expand Down

0 comments on commit b552bba

Please sign in to comment.