Skip to content

Commit

Permalink
some logging things
Browse files Browse the repository at this point in the history
 - use user customizable colors in logging
 - 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
[ci skip]
  • Loading branch information
fredrikekre committed Dec 15, 2017
1 parent 5e1418b commit 1b9f434
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
39 changes: 25 additions & 14 deletions base/logging.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module CoreLogging

import Base: isless, +, -, convert, show
using Base: info_color, warn_color, error_color

export
AbstractLogger,
Expand Down Expand Up @@ -341,8 +342,10 @@ 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"
str = join(" " .* split(sprint(showerror, err), "\n"), "\n")
msg *= str
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 @@ -482,19 +485,27 @@ function handle_message(logger::SimpleLogger, level, message, _module, group, id
end
levelstr = string(level)
color = level < Info ? :blue :
level < Warn ? :cyan :
level < Error ? :yellow : :red
level < Warn ? info_color() :
level < Error ? warn_color() : error_color()
buf = IOBuffer()
print_with_color(color, buf, first(levelstr), "- ", bold=true)
msglines = split(string(message), '\n')
for i in 1:length(msglines)-1
println(buf, msglines[i])
print_with_color(color, buf, "| ", bold=true)
end
println(buf, msglines[end], " -", levelstr, ":", _module, ":", basename(filepath), ":", line)
for (key,val) in pairs(kwargs)
print_with_color(color, buf, "| ", bold=true)
println(buf, key, " = ", val)
msglines = split(chomp(string(message)), '\n')
if length(msglines) + length(kwargs) == 1
print_with_color(color, buf, first(levelstr), ": ", bold=true)
# print_with_color(color, buf, "- ")
println(buf, msglines[1], " - ", _module, ":", basename(filepath), ":", line)
else
print_with_color(color, buf, first(levelstr), "", bold=true)
println(buf, msglines[1])
for i in 2:length(msglines)
print_with_color(color, buf, "", bold=true)
println(buf, msglines[i])
end
for (key,val) in pairs(kwargs)
print_with_color(color, buf, "", bold=true)
println(buf, key, " = ", val)
end
print_with_color(color, buf, "", bold=true)
println(buf, _module, ":", basename(filepath), ":", line)
end
write(logger.stream, take!(buf))
nothing
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

0 comments on commit 1b9f434

Please sign in to comment.