Skip to content

Commit ab0f1cf

Browse files
committed
Use StyledStrings for Logging
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.
1 parent dd83530 commit ab0f1cf

File tree

7 files changed

+42
-34
lines changed

7 files changed

+42
-34
lines changed

doc/Manifest.toml

+1
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ uuid = "94ce4f54-9a6c-5748-9c1c-f9c7231a4531"
134134
version = "1.17.0+0"
135135

136136
[[deps.Logging]]
137+
deps = ["StyledStrings"]
137138
uuid = "56ddb016-857b-54e1-b83d-db4d58db5568"
138139
version = "1.11.0"
139140

pkgimage.mk

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ $(eval $(call stdlib_builder,Base64,))
7777
$(eval $(call stdlib_builder,CRC32c,))
7878
$(eval $(call stdlib_builder,FileWatching,))
7979
$(eval $(call stdlib_builder,Libdl,))
80-
$(eval $(call stdlib_builder,Logging,))
8180
$(eval $(call stdlib_builder,Mmap,))
8281
$(eval $(call stdlib_builder,NetworkOptions,))
8382
$(eval $(call stdlib_builder,SHA,))
@@ -106,6 +105,7 @@ $(eval $(call stdlib_builder,OpenBLAS_jll,Artifacts Libdl))
106105
$(eval $(call stdlib_builder,Markdown,Base64))
107106
$(eval $(call stdlib_builder,Printf,Unicode))
108107
$(eval $(call stdlib_builder,Random,SHA))
108+
$(eval $(call stdlib_builder,Logging,StyledStrings))
109109
$(eval $(call stdlib_builder,Tar,ArgTools,SHA))
110110
$(eval $(call stdlib_builder,DelimitedFiles,Mmap))
111111
$(eval $(call stdlib_builder,JuliaSyntaxHighlighting,StyledStrings))

stdlib/Logging/Project.toml

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ name = "Logging"
22
uuid = "56ddb016-857b-54e1-b83d-db4d58db5568"
33
version = "1.11.0"
44

5+
[deps]
6+
StyledStrings = "f489334b-da3d-4c2e-b8f0-e476e12c162b"
7+
58
[extras]
69
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
710

stdlib/Logging/src/ConsoleLogger.jl

+18-15
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ Log levels less than `min_level` are filtered out.
1212
Message formatting can be controlled by setting keyword arguments:
1313
1414
* `meta_formatter` is a function which takes the log event metadata
15-
`(level, _module, group, id, file, line)` and returns a color (as would be
16-
passed to printstyled), prefix and suffix for the log message. The
17-
default is to prefix with the log level and a suffix containing the module,
18-
file and line location.
15+
`(level, _module, group, id, file, line)` and returns a face name (used in
16+
the constructed [`AnnotatedString`](@ref Base.AnnotatedString)), prefix and
17+
suffix for the log message. The default is to prefix with the log level and
18+
a suffix containing the module, file and line location.
1919
* `show_limited` limits the printing of large data structures to something
2020
which can fit on the screen by setting the `:limit` `IOContext` key during
2121
formatting.
@@ -59,10 +59,10 @@ showvalue(io, ex::Exception) = showerror(io, ex)
5959

6060
function default_logcolor(level::LogLevel)
6161
level in keys(custom_log_levels) ? custom_log_levels[level][2] :
62-
level < Info ? Base.debug_color() :
63-
level < Warn ? Base.info_color() :
64-
level < Error ? Base.warn_color() :
65-
Base.error_color()
62+
level < Info ? :log_debug :
63+
level < Warn ? :log_info :
64+
level < Error ? :log_warn :
65+
:log_error
6666
end
6767

6868
function default_metafmt(level::LogLevel, _module, group, id, file, line)
@@ -104,6 +104,8 @@ function termlength(str)
104104
return N
105105
end
106106

107+
termlength(str::Base.AnnotatedString) = textwidth(str)
108+
107109
function handle_message(logger::ConsoleLogger, level::LogLevel, message, _module, group, id,
108110
filepath, line; kwargs...)
109111
@nospecialize
@@ -146,6 +148,7 @@ function handle_message(logger::ConsoleLogger, level::LogLevel, message, _module
146148
# Format lines as text with appropriate indentation and with a box
147149
# decoration on the left.
148150
color, prefix, suffix = logger.meta_formatter(level, _module, group, id, filepath, line)::Tuple{Union{Symbol,Int},String,String}
151+
color = StyledStrings.Face(foreground=StyledStrings.Legacy.legacy_color(color))
149152
minsuffixpad = 2
150153
buf = IOBuffer()
151154
iob = IOContext(buf, stream)
@@ -159,19 +162,19 @@ function handle_message(logger::ConsoleLogger, level::LogLevel, message, _module
159162
nonpadwidth = 2 + length(suffix)
160163
end
161164
for (i, (indent, msg)) in enumerate(msglines)
162-
boxstr = length(msglines) == 1 ? "[ " :
163-
i == 1 ? " " :
164-
i < length(msglines) ? " " :
165-
" "
166-
printstyled(iob, boxstr, bold=true, color=color)
165+
boxstr = length(msglines) == 1 ? "[" :
166+
i == 1 ? "" :
167+
i < length(msglines) ? "" :
168+
""
169+
print(iob, styled"{$color,bold:$boxstr} ")
167170
if i == 1 && !isempty(prefix)
168-
printstyled(iob, prefix, " ", bold=true, color=color)
171+
print(iob, styled"{$color,bold:$prefix} ")
169172
end
170173
print(iob, " "^indent, msg)
171174
if i == length(msglines) && !isempty(suffix)
172175
npad = max(0, justify_width - nonpadwidth) + minsuffixpad
173176
print(iob, " "^npad)
174-
printstyled(iob, suffix, color=:light_black)
177+
print(iob, styled"{shadow:$suffix}")
175178
end
176179
println(iob)
177180
end

stdlib/Logging/src/Logging.jl

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ and available by default.
88
"""
99
module Logging
1010

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

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

stdlib/Logging/test/runtests.jl

+12-12
Original file line numberDiff line numberDiff line change
@@ -54,24 +54,24 @@ end
5454

5555
@testset "Default metadata formatting" begin
5656
@test Logging.default_metafmt(Logging.Debug, Base, :g, :i, expanduser("~/somefile.jl"), 42) ==
57-
(:blue, "Debug:", "@ Base ~/somefile.jl:42")
57+
(:log_debug, "Debug:", "@ Base ~/somefile.jl:42")
5858
@test Logging.default_metafmt(Logging.Info, Main, :g, :i, "a.jl", 1) ==
59-
(:cyan, "Info:", "")
59+
(:log_info, "Info:", "")
6060
@test Logging.default_metafmt(Logging.Warn, Main, :g, :i, "b.jl", 2) ==
61-
(:yellow, "Warning:", "@ Main b.jl:2")
61+
(:log_warn, "Warning:", "@ Main b.jl:2")
6262
@test Logging.default_metafmt(Logging.Error, Main, :g, :i, "", 0) ==
63-
(:light_red, "Error:", "@ Main :0")
63+
(:log_error, "Error:", "@ Main :0")
6464
# formatting of nothing
6565
@test Logging.default_metafmt(Logging.Warn, nothing, :g, :i, "b.jl", 2) ==
66-
(:yellow, "Warning:", "@ b.jl:2")
66+
(:log_warn, "Warning:", "@ b.jl:2")
6767
@test Logging.default_metafmt(Logging.Warn, Main, :g, :i, nothing, 2) ==
68-
(:yellow, "Warning:", "@ Main")
68+
(:log_warn, "Warning:", "@ Main")
6969
@test Logging.default_metafmt(Logging.Warn, Main, :g, :i, "b.jl", nothing) ==
70-
(:yellow, "Warning:", "@ Main b.jl")
70+
(:log_warn, "Warning:", "@ Main b.jl")
7171
@test Logging.default_metafmt(Logging.Warn, nothing, :g, :i, nothing, 2) ==
72-
(:yellow, "Warning:", "")
72+
(:log_warn, "Warning:", "")
7373
@test Logging.default_metafmt(Logging.Warn, Main, :g, :i, "b.jl", 2:5) ==
74-
(:yellow, "Warning:", "@ Main b.jl:2-5")
74+
(:log_warn, "Warning:", "@ Main b.jl:2-5")
7575
end
7676

7777
function dummy_metafmt(level, _module, group, id, file, line)
@@ -256,9 +256,9 @@ end
256256
# Basic colorization test
257257
@test genmsg("line1\nline2", color=true) ==
258258
"""
259-
\e[36m\e[1m┌ \e[22m\e[39m\e[36m\e[1mPREFIX \e[22m\e[39mline1
260-
\e[36m\e[1m│ \e[22m\e[39mline2
261-
\e[36m\e[1m└ \e[22m\e[39m\e[90mSUFFIX\e[39m
259+
\e[36m\e[1m┌\e[39m\e[22m \e[36m\e[1mPREFIX\e[39m\e[22m line1
260+
\e[36m\e[1m│\e[39m\e[22m line2
261+
\e[36m\e[1m└\e[39m\e[22m \e[90mSUFFIX\e[39m
262262
"""
263263

264264
end

test/precompile.jl

+1
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,7 @@ precompile_test_harness(false) do dir
469469
# and their dependencies
470470
Dict(Base.PkgId(Base.root_module(Base, :SHA)) => Base.module_build_id(Base.root_module(Base, :SHA))),
471471
Dict(Base.PkgId(Base.root_module(Base, :Markdown)) => Base.module_build_id(Base.root_module(Base, :Markdown))),
472+
Dict(Base.PkgId(Base.root_module(Base, :StyledStrings)) => Base.module_build_id(Base.root_module(Base, :StyledStrings))),
472473
# and their dependencies
473474
Dict(Base.PkgId(Base.root_module(Base, :Base64)) => Base.module_build_id(Base.root_module(Base, :Base64))),
474475
)

0 commit comments

Comments
 (0)