Skip to content

Commit

Permalink
Handle nothing in logging (#28097)
Browse files Browse the repository at this point in the history
* Logging: handle nothing for _module, _file and _line kwargs

* Logging: don't print nothing for _module, _line and _file
in ConsoleLogger. Format UnitRanges as `somefile.jl:1-4`
instead of `somefile.jl:1:4` when printing line numbers.
  • Loading branch information
fredrikekre committed Jul 17, 2018
1 parent f8093bb commit 435f0a2
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 9 deletions.
10 changes: 5 additions & 5 deletions base/logging.jl
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ _log_record_ids = Set{Symbol}()
# versions of the originating module, provided the log generating statement
# itself doesn't change.
function log_record_id(_module, level, message_ex)
modname = join(fullname(_module), "_")
modname = _module === nothing ? "" : join(fullname(_module), "_")
# Use (1<<31) to fit well within an (arbitriraly chosen) eight hex digits,
# as we increment h to resolve any collisions.
h = hash(string(modname, level, message_ex)) % (1<<31)
Expand Down Expand Up @@ -289,8 +289,8 @@ function logmsg_code(_module, file, line, level, message, exs...)
end
end
# Note that it may be necessary to set `id` and `group` manually during bootstrap
id !== nothing || (id = :(log_record_id(_module, level, $exs)))
group !== nothing || (group = :(Symbol(splitext(basename($file))[1])))
id = something(id, :(log_record_id(_module, level, $exs)))
group = something(group, :(Symbol(splitext(basename(something($file, "")))[1])))
quote
level = $level
std_level = convert(LogLevel, level)
Expand Down Expand Up @@ -529,8 +529,8 @@ function handle_message(logger::SimpleLogger, level, message, _module, group, id
for (key, val) in kwargs
println(iob, "", key, " = ", val)
end
mod = _module === nothing ? "" : "$(_module) "
println(iob, "└ @ ", mod, filepath, ":", line)
println(iob, "└ @ ", something(_module, "nothing"), " ",
something(filepath, "nothing"), ":", something(line, "nothing"))
write(logger.stream, take!(buf))
nothing
end
Expand Down
15 changes: 12 additions & 3 deletions stdlib/Logging/src/ConsoleLogger.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,18 @@ end
function default_metafmt(level, _module, group, id, file, line)
color = default_logcolor(level)
prefix = (level == Warn ? "Warning" : string(level))*':'
mod = _module === nothing ? "" : "$(_module) "
suffix = (Info <= level < Warn) ? "" : "@ $mod$(Base.contractuser(file)):$line"
color,prefix,suffix
suffix = ""
Info <= level < Warn && return color, prefix, suffix
_module !== nothing && (suffix *= "$(_module)")
if file !== nothing
_module !== nothing && (suffix *= " ")
suffix *= Base.contractuser(file)
if line !== nothing
suffix *= ":$(isa(line, UnitRange) ? "$(first(line))-$(last(line))" : line)"
end
end
!isempty(suffix) && (suffix = "@ " * suffix)
return color, prefix, suffix
end

# Length of a string as it will appear in the terminal (after ANSI color codes
Expand Down
11 changes: 11 additions & 0 deletions stdlib/Logging/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ import Logging: min_enabled_level, shouldlog, handle_message
(: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)
Expand Down
20 changes: 19 additions & 1 deletion test/logging.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This file is a part of Julia. License is MIT: https://julialang.org/license

using Base.CoreLogging
using Test, Base.CoreLogging
import Base.CoreLogging: BelowMinLevel, Debug, Info, Warn, Error,
handle_message, shouldlog, min_enabled_level, catch_exceptions

Expand Down Expand Up @@ -109,6 +109,17 @@ end
@test record._module == logger.shouldlog_args[2]
@test record.group == logger.shouldlog_args[3]
@test record.id == logger.shouldlog_args[4]

# handling of nothing
logger = TestLogger()
with_logger(logger) do
@info "foo" _module = nothing _file = nothing _line = nothing
end
@test length(logger.logs) == 1
record = logger.logs[1]
@test record._module == nothing
@test record.file == nothing
@test record.line == nothing
end


Expand Down Expand Up @@ -292,6 +303,13 @@ end
│ b = asdf
└ @ Base other.jl:101
"""

# nothing values
@test genmsg(Warn, "msg", nothing, nothing, nothing) ==
"""
┌ Warning: msg
└ @ nothing nothing:nothing
"""
end

# Issue #26273
Expand Down

0 comments on commit 435f0a2

Please sign in to comment.