Skip to content

Commit

Permalink
Use StyledStrings for Logging
Browse files Browse the repository at this point in the history
Transition from printstyled to the new approach to styling provided by
StyledStrings.

This both makes it possible for the styling used to be customised, and
allows for other styled content to inherit/re-use the logging styles.
  • Loading branch information
tecosaur committed Jan 25, 2024
1 parent 3da897b commit a88ac9e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 22 deletions.
6 changes: 5 additions & 1 deletion doc/Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ uuid = "94ce4f54-9a6c-5748-9c1c-f9c7231a4531"
version = "1.17.0+0"

[[deps.Logging]]
deps = ["StyledStrings"]
uuid = "56ddb016-857b-54e1-b83d-db4d58db5568"
version = "1.11.0"

Expand Down Expand Up @@ -205,7 +206,7 @@ uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7"
version = "1.11.0"

[[deps.REPL]]
deps = ["InteractiveUtils", "Markdown", "Sockets", "Unicode"]
deps = ["InteractiveUtils", "Markdown", "Sockets", "StyledStrings", "Unicode"]
uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"
version = "1.11.0"

Expand Down Expand Up @@ -242,6 +243,9 @@ deps = ["ArgTools", "SHA"]
uuid = "a4e569a6-e804-4fa4-b0f3-eef7a1d5b13e"
version = "1.10.0"

[[deps.StyledStrings]]
uuid = "f489334b-da3d-4c2e-b8f0-e476e12c162b"

[[deps.Test]]
deps = ["InteractiveUtils", "Logging", "Random", "Serialization"]
uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Expand Down
3 changes: 3 additions & 0 deletions stdlib/Logging/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ name = "Logging"
uuid = "56ddb016-857b-54e1-b83d-db4d58db5568"
version = "1.11.0"

[deps]
StyledStrings = "f489334b-da3d-4c2e-b8f0-e476e12c162b"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

Expand Down
33 changes: 18 additions & 15 deletions stdlib/Logging/src/ConsoleLogger.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ 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,
file and line location.
`(level, _module, group, id, file, line)` and returns a face name (used in
the constructed [`AnnotatedString`](@ref Base.AnnotatedString)), prefix 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
formatting.
Expand Down Expand Up @@ -59,10 +59,10 @@ showvalue(io, ex::Exception) = showerror(io, ex)

function default_logcolor(level::LogLevel)
level in keys(custom_log_levels) ? custom_log_levels[level][2] :
level < Info ? Base.debug_color() :
level < Warn ? Base.info_color() :
level < Error ? Base.warn_color() :
Base.error_color()
level < Info ? :log_debug :
level < Warn ? :log_info :
level < Error ? :log_warn :
:log_error
end

function default_metafmt(level::LogLevel, _module, group, id, file, line)
Expand Down Expand Up @@ -104,6 +104,8 @@ function termlength(str)
return N
end

termlength(str::Base.AnnotatedString) = textwidth(str)

function handle_message(logger::ConsoleLogger, level::LogLevel, message, _module, group, id,
filepath, line; kwargs...)
@nospecialize
Expand Down Expand Up @@ -146,6 +148,7 @@ function handle_message(logger::ConsoleLogger, level::LogLevel, message, _module
# 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)::Tuple{Union{Symbol,Int},String,String}
color = Face(foreground=StyledStrings.Legacy.legacy_color(color))
minsuffixpad = 2
buf = IOBuffer()
iob = IOContext(buf, stream)
Expand All @@ -159,19 +162,19 @@ function handle_message(logger::ConsoleLogger, level::LogLevel, message, _module
nonpadwidth = 2 + length(suffix)
end
for (i, (indent, msg)) in enumerate(msglines)
boxstr = length(msglines) == 1 ? "[ " :
i == 1 ? " " :
i < length(msglines) ? " " :
" "
printstyled(iob, boxstr, bold=true, color=color)
boxstr = length(msglines) == 1 ? "[" :
i == 1 ? "" :
i < length(msglines) ? "" :
""
print(iob, styled"{$color,bold:$boxstr} ")
if i == 1 && !isempty(prefix)
printstyled(iob, prefix, " ", bold=true, color=color)
print(iob, styled"{$color,bold:$prefix} ")
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)
print(iob, styled"{shadow:$suffix}")
end
println(iob)
end
Expand Down
12 changes: 6 additions & 6 deletions stdlib/Logging/src/Logging.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ and available by default.
"""
module Logging

using StyledStrings

# Import the CoreLogging implementation into Logging as new const bindings.
# Doing it this way (rather than with import) makes these symbols accessible to
# tab completion.
Expand All @@ -31,13 +33,11 @@ for sym in [
end

"""
@create_log_macro(name::Symbol, level::Int, color::Union{Int,Symbol})
Creates a custom log macro like `@info`, `@warn` etc. with a given `name`, `level` and
`color`. The macro created is named with the lowercase form of `name` but the given form
is used for the printing.
@create_log_macro(name::Symbol, level::Int, face::Union{Symbol, StyledStrings.Face})
The available color keys can be seen by typing `Base.text_colors` in the help mode of the REPL
Creates a custom log macro like `@info`, `@warn` etc. with a given `name`,
`level` to be displayed with `face`. The macro created is named with the
lowercase form of `name` but the given form is used for the printing.
```julia-repl
julia> @create_log_macro(:MyLog, 200, :magenta)
Expand Down

0 comments on commit a88ac9e

Please sign in to comment.