-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow logging meta-formatter to set suffix color #29397
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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 = Base.stackframe_lineinfo_color() | ||
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,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. | ||
color,prefix,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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AFAIK this is an unexported API function and thus this change can go into Julia 1.1 or the next minor bump of the Logging package. This should also have an explicit deprecation message for the 3 value version, so developers can plan for this change appropriately There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It depends how you define public API. The docstring for |
||
prefix_color, prefix, suffix = format | ||
suffix_color = :light_black | ||
end | ||
minsuffixpad = 2 | ||
buf = IOBuffer() | ||
iob = IOContext(buf, logger.stream) | ||
|
@@ -145,15 +153,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 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea with
light_black
was to not distract people too much with the logging metadata and I'd rather like to preserve that. Butstackframe_lineinfo_color
is:bold
which is the opposite.I'm also not sure why we'd choose
stackframe_lineinfo_color
here, as it's not clearly related to logging. We could just make a new color category and matching environment variable and set it default to:light_black
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was on the opinion that they were the same notional thing, both are line info.
My proposal was more generally that we should have a single
print_styled_lineinfo
, used by all 3 of logging andmethods
and error printing.To my mind the line number shown in the log is a mini-stack-trace.
Like a
@warn
is a softer alternative to an exception.But I do see your point. About at one point it being considered important and the other not.
However, it is also worth noting that on many (most?) terminals
:bold
white is more or less indistinguishable from:white
(I actually find that it is harder find the line info section in the stack trace where it is:bold
white than in the warning info where is is:light_black
)Having 2 variables for more configurability is not unreasonable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I originally set the default
suffix_color
to:light_black
, which I thought was preferable. But other people wanted to emphasize the similarities between the mini stacktrace and a full stacktrace.