From abfe771d7ba819024fbd49360ea572e91caeec91 Mon Sep 17 00:00:00 2001 From: Luke Adams Date: Thu, 27 Sep 2018 10:27:42 -0600 Subject: [PATCH 1/4] allow metaformatter to set suffix color --- stdlib/Logging/src/ConsoleLogger.jl | 16 +++++++++------- stdlib/Logging/test/runtests.jl | 26 +++++++++++++------------- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/stdlib/Logging/src/ConsoleLogger.jl b/stdlib/Logging/src/ConsoleLogger.jl index 1f8ecba69188b..a45fabc1aee9b 100644 --- a/stdlib/Logging/src/ConsoleLogger.jl +++ b/stdlib/Logging/src/ConsoleLogger.jl @@ -58,10 +58,11 @@ function default_logcolor(level) end function default_metafmt(level, _module, group, id, file, line) - color = default_logcolor(level) + prefix_color = default_logcolor(level) prefix = (level == Warn ? "Warning" : string(level))*':' suffix = "" - Info <= level < Warn && return color, prefix, suffix + suffix_color = :light_black + Info <= level < Warn && return prefix_color, prefix, suffix_color, suffix _module !== nothing && (suffix *= "$(_module)") if file !== nothing _module !== nothing && (suffix *= " ") @@ -71,7 +72,7 @@ function default_metafmt(level, _module, group, id, file, line) end end !isempty(suffix) && (suffix = "@ " * suffix) - return color, prefix, suffix + return prefix_color, prefix, suffix_color, suffix end # Length of a string as it will appear in the terminal (after ANSI color codes @@ -127,7 +128,8 @@ function handle_message(logger::ConsoleLogger, level, message, _module, group, i # Format lines as text with appropriate indentation and with a box # decoration on the left. - color,prefix,suffix = logger.meta_formatter(level, _module, group, id, filepath, line) + prefix_color, prefix, suffix_color ,suffix = + logger.meta_formatter(level, _module, group, id, filepath, line) minsuffixpad = 2 buf = IOBuffer() iob = IOContext(buf, logger.stream) @@ -145,15 +147,15 @@ function handle_message(logger::ConsoleLogger, level, message, _module, group, i i == 1 ? "┌ " : i < length(msglines) ? "│ " : "└ " - printstyled(iob, boxstr, bold=true, color=color) + printstyled(iob, boxstr, bold=true, color=prefix_color) if i == 1 && !isempty(prefix) - printstyled(iob, prefix, " ", bold=true, color=color) + printstyled(iob, prefix, " ", bold=true, color=prefix_color) end print(iob, " "^indent, msg) if i == length(msglines) && !isempty(suffix) npad = max(0, justify_width - nonpadwidth) + minsuffixpad print(iob, " "^npad) - printstyled(iob, suffix, color=:light_black) + printstyled(iob, suffix, color=suffix_color) end println(iob) end diff --git a/stdlib/Logging/test/runtests.jl b/stdlib/Logging/test/runtests.jl index 38c85b6d125f8..f9434ba4fdd3b 100644 --- a/stdlib/Logging/test/runtests.jl +++ b/stdlib/Logging/test/runtests.jl @@ -43,28 +43,28 @@ import Logging: min_enabled_level, shouldlog, handle_message @testset "Default metadata formatting" begin @test Logging.default_metafmt(Logging.Debug, Base, :g, :i, expanduser("~/somefile.jl"), 42) == - (:blue, "Debug:", "@ Base ~/somefile.jl:42") + (:blue, "Debug:", :light_black, "@ Base ~/somefile.jl:42") @test Logging.default_metafmt(Logging.Info, Main, :g, :i, "a.jl", 1) == - (:cyan, "Info:", "") + (:cyan, "Info:", :light_black, "") @test Logging.default_metafmt(Logging.Warn, Main, :g, :i, "b.jl", 2) == - (:yellow, "Warning:", "@ Main b.jl:2") + (:yellow, "Warning:", :light_black, "@ Main b.jl:2") @test Logging.default_metafmt(Logging.Error, Main, :g, :i, "", 0) == - (:light_red, "Error:", "@ Main :0") + (:light_red, "Error:", :light_black, "@ Main :0") # formatting of nothing @test Logging.default_metafmt(Logging.Warn, nothing, :g, :i, "b.jl", 2) == - (:yellow, "Warning:", "@ b.jl:2") + (:yellow, "Warning:", :light_black, "@ b.jl:2") @test Logging.default_metafmt(Logging.Warn, Main, :g, :i, nothing, 2) == - (:yellow, "Warning:", "@ Main") + (:yellow, "Warning:", :light_black, "@ Main") @test Logging.default_metafmt(Logging.Warn, Main, :g, :i, "b.jl", nothing) == - (:yellow, "Warning:", "@ Main b.jl") + (:yellow, "Warning:", :light_black, "@ Main b.jl") @test Logging.default_metafmt(Logging.Warn, nothing, :g, :i, nothing, 2) == - (:yellow, "Warning:", "") + (:yellow, "Warning:", :light_black, "") @test Logging.default_metafmt(Logging.Warn, Main, :g, :i, "b.jl", 2:5) == - (:yellow, "Warning:", "@ Main b.jl:2-5") + (:yellow, "Warning:", :light_black, "@ Main b.jl:2-5") end function dummy_metafmt(level, _module, group, id, file, line) - :cyan,"PREFIX","SUFFIX" + :cyan,"PREFIX",:light_black,"SUFFIX" end # Log formatting @@ -98,7 +98,7 @@ import Logging: min_enabled_level, shouldlog, handle_message # 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")) == + (:white,"Foo!",:red, "$level $_module $group $id $file $line")) == """ ┌ Foo! msg └ Debug Main a_group an_id some/path.jl 101 @@ -116,11 +116,11 @@ import Logging: min_enabled_level, shouldlog, handle_message └ SUFFIX """ # Behavior with empty prefix / suffix - @test genmsg("msg", meta_formatter=(args...)->(:white, "PREFIX", "")) == + @test genmsg("msg", meta_formatter=(args...)->(:white, "PREFIX", :red, "")) == """ [ PREFIX msg """ - @test genmsg("msg", meta_formatter=(args...)->(:white, "", "SUFFIX")) == + @test genmsg("msg", meta_formatter=(args...)->(:white, "", :red, "SUFFIX")) == """ ┌ msg └ SUFFIX From 4e378ba66d2a38fa976714b3d5e84d6e9bc08e2d Mon Sep 17 00:00:00 2001 From: Luke Adams Date: Thu, 27 Sep 2018 10:42:55 -0600 Subject: [PATCH 2/4] update docs --- stdlib/Logging/src/ConsoleLogger.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/stdlib/Logging/src/ConsoleLogger.jl b/stdlib/Logging/src/ConsoleLogger.jl index a45fabc1aee9b..819ded9f07b7f 100644 --- a/stdlib/Logging/src/ConsoleLogger.jl +++ b/stdlib/Logging/src/ConsoleLogger.jl @@ -12,9 +12,9 @@ Log levels less than `min_level` are filtered out. Message formatting can be controlled by setting keyword arguments: * `meta_formatter` is a function which takes the log event metadata - `(level, _module, group, id, file, line)` and returns a color (as would be - passed to printstyled), prefix and suffix for the log message. The - default is to prefix with the log level and a suffix containing the module, + `(level, _module, group, id, file, line)` and returns a prefix color (as would + be passed to printstyled), prefix, suffix color, and suffix for the log message. + The default is to prefix with the log level and a suffix containing the module, file and line location. * `show_limited` limits the printing of large data structures to something which can fit on the screen by setting the `:limit` `IOContext` key during From fdf02f50722ed8394a755b342301cf280748fba2 Mon Sep 17 00:00:00 2001 From: Luke Adams Date: Tue, 2 Oct 2018 16:31:16 -0600 Subject: [PATCH 3/4] make changes backwards compatiable Eventually, the three-value meta-formatter should be depreciated. --- stdlib/Logging/src/ConsoleLogger.jl | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/stdlib/Logging/src/ConsoleLogger.jl b/stdlib/Logging/src/ConsoleLogger.jl index 819ded9f07b7f..3c722964dc06d 100644 --- a/stdlib/Logging/src/ConsoleLogger.jl +++ b/stdlib/Logging/src/ConsoleLogger.jl @@ -128,8 +128,14 @@ function handle_message(logger::ConsoleLogger, level, message, _module, group, i # Format lines as text with appropriate indentation and with a box # decoration on the left. - prefix_color, prefix, suffix_color ,suffix = - logger.meta_formatter(level, _module, group, id, filepath, line) + format = logger.meta_formatter(level, _module, group, id, filepath, line) + if length(format) == 4 + prefix_color, prefix, suffix_color, suffix = format + else + # The three-value version of meta_formatter should be depreciated at some point + prefix_color, prefix, suffix = format + suffix_color = :light_black + end minsuffixpad = 2 buf = IOBuffer() iob = IOContext(buf, logger.stream) From ebbc7992abff3ae00bb73711df79ae4895cd1cd8 Mon Sep 17 00:00:00 2001 From: Luke Adams Date: Fri, 5 Oct 2018 12:00:49 -0600 Subject: [PATCH 4/4] Make ConsoleLogger colors consistant with Base --- stdlib/Logging/src/ConsoleLogger.jl | 2 +- stdlib/Logging/test/runtests.jl | 19 ++++++++++--------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/stdlib/Logging/src/ConsoleLogger.jl b/stdlib/Logging/src/ConsoleLogger.jl index 3c722964dc06d..838122fbebe7b 100644 --- a/stdlib/Logging/src/ConsoleLogger.jl +++ b/stdlib/Logging/src/ConsoleLogger.jl @@ -61,7 +61,7 @@ function default_metafmt(level, _module, group, id, file, line) prefix_color = default_logcolor(level) prefix = (level == Warn ? "Warning" : string(level))*':' suffix = "" - suffix_color = :light_black + suffix_color = Base.stackframe_lineinfo_color() Info <= level < Warn && return prefix_color, prefix, suffix_color, suffix _module !== nothing && (suffix *= "$(_module)") if file !== nothing diff --git a/stdlib/Logging/test/runtests.jl b/stdlib/Logging/test/runtests.jl index f9434ba4fdd3b..35261b9b0aaec 100644 --- a/stdlib/Logging/test/runtests.jl +++ b/stdlib/Logging/test/runtests.jl @@ -41,26 +41,27 @@ import Logging: min_enabled_level, shouldlog, handle_message end @test String(take!(buf)) == "" + suffix_color = Base.stackframe_lineinfo_color() @testset "Default metadata formatting" begin @test Logging.default_metafmt(Logging.Debug, Base, :g, :i, expanduser("~/somefile.jl"), 42) == - (:blue, "Debug:", :light_black, "@ Base ~/somefile.jl:42") + (:blue, "Debug:", suffix_color, "@ Base ~/somefile.jl:42") @test Logging.default_metafmt(Logging.Info, Main, :g, :i, "a.jl", 1) == - (:cyan, "Info:", :light_black, "") + (:cyan, "Info:", suffix_color, "") @test Logging.default_metafmt(Logging.Warn, Main, :g, :i, "b.jl", 2) == - (:yellow, "Warning:", :light_black, "@ Main b.jl:2") + (:yellow, "Warning:", suffix_color, "@ Main b.jl:2") @test Logging.default_metafmt(Logging.Error, Main, :g, :i, "", 0) == - (:light_red, "Error:", :light_black, "@ Main :0") + (:light_red, "Error:", suffix_color, "@ Main :0") # formatting of nothing @test Logging.default_metafmt(Logging.Warn, nothing, :g, :i, "b.jl", 2) == - (:yellow, "Warning:", :light_black, "@ b.jl:2") + (:yellow, "Warning:", suffix_color, "@ b.jl:2") @test Logging.default_metafmt(Logging.Warn, Main, :g, :i, nothing, 2) == - (:yellow, "Warning:", :light_black, "@ Main") + (:yellow, "Warning:", suffix_color, "@ Main") @test Logging.default_metafmt(Logging.Warn, Main, :g, :i, "b.jl", nothing) == - (:yellow, "Warning:", :light_black, "@ Main b.jl") + (:yellow, "Warning:", suffix_color, "@ Main b.jl") @test Logging.default_metafmt(Logging.Warn, nothing, :g, :i, nothing, 2) == - (:yellow, "Warning:", :light_black, "") + (:yellow, "Warning:", suffix_color, "") @test Logging.default_metafmt(Logging.Warn, Main, :g, :i, "b.jl", 2:5) == - (:yellow, "Warning:", :light_black, "@ Main b.jl:2-5") + (:yellow, "Warning:", suffix_color, "@ Main b.jl:2-5") end function dummy_metafmt(level, _module, group, id, file, line)