Skip to content

Commit b419d8b

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 0be0b38 commit b419d8b

File tree

4 files changed

+28
-16
lines changed

4 files changed

+28
-16
lines changed

doc/Manifest.toml

+5-1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ version = "1.11.0+1"
6565
uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
6666

6767
[[deps.Logging]]
68+
deps = ["StyledStrings"]
6869
uuid = "56ddb016-857b-54e1-b83d-db4d58db5568"
6970

7071
[[deps.Markdown]]
@@ -94,7 +95,7 @@ deps = ["Unicode"]
9495
uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7"
9596

9697
[[deps.REPL]]
97-
deps = ["InteractiveUtils", "Markdown", "Sockets", "Unicode"]
98+
deps = ["InteractiveUtils", "Markdown", "Sockets", "StyledStrings", "Unicode"]
9899
uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"
99100

100101
[[deps.Random]]
@@ -111,6 +112,9 @@ uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
111112
[[deps.Sockets]]
112113
uuid = "6462fe0b-24de-5631-8697-dd941f90decc"
113114

115+
[[deps.StyledStrings]]
116+
uuid = "f489334b-da3d-4c2e-b8f0-e476e12c162b"
117+
114118
[[deps.Test]]
115119
deps = ["InteractiveUtils", "Logging", "Random", "Serialization"]
116120
uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

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.
@@ -58,10 +58,10 @@ end
5858
showvalue(io, ex::Exception) = showerror(io, ex)
5959

6060
function default_logcolor(level::LogLevel)
61-
level < Info ? Base.debug_color() :
62-
level < Warn ? Base.info_color() :
63-
level < Error ? Base.warn_color() :
64-
Base.error_color()
61+
level < Info ? :log_debug :
62+
level < Warn ? :log_info :
63+
level < Error ? :log_warn :
64+
:log_error
6565
end
6666

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

106+
termlength(str::Base.AnnotatedString) = textwidth(str)
107+
106108
function handle_message(logger::ConsoleLogger, level::LogLevel, message, _module, group, id,
107109
filepath, line; kwargs...)
108110
@nospecialize
@@ -145,6 +147,7 @@ function handle_message(logger::ConsoleLogger, level::LogLevel, message, _module
145147
# Format lines as text with appropriate indentation and with a box
146148
# decoration on the left.
147149
color, prefix, suffix = logger.meta_formatter(level, _module, group, id, filepath, line)::Tuple{Union{Symbol,Int},String,String}
150+
color = Face(foreground=StyledStrings.Legacy.legacy_color(color))
148151
minsuffixpad = 2
149152
buf = IOBuffer()
150153
iob = IOContext(buf, stream)
@@ -158,19 +161,19 @@ function handle_message(logger::ConsoleLogger, level::LogLevel, message, _module
158161
nonpadwidth = 2 + length(suffix)
159162
end
160163
for (i, (indent, msg)) in enumerate(msglines)
161-
boxstr = length(msglines) == 1 ? "[ " :
162-
i == 1 ? " " :
163-
i < length(msglines) ? " " :
164-
" "
165-
printstyled(iob, boxstr, bold=true, color=color)
164+
boxstr = length(msglines) == 1 ? "[" :
165+
i == 1 ? "" :
166+
i < length(msglines) ? "" :
167+
""
168+
print(iob, styled"{$color,bold:$boxstr} ")
166169
if i == 1 && !isempty(prefix)
167-
printstyled(iob, prefix, " ", bold=true, color=color)
170+
print(iob, styled"{$color,bold:$prefix} ")
168171
end
169172
print(iob, " "^indent, msg)
170173
if i == length(msglines) && !isempty(suffix)
171174
npad = max(0, justify_width - nonpadwidth) + minsuffixpad
172175
print(iob, " "^npad)
173-
printstyled(iob, suffix, color=:light_black)
176+
print(iob, styled"{shadow:$suffix}")
174177
end
175178
println(iob)
176179
end

stdlib/Logging/src/Logging.jl

+2
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.

0 commit comments

Comments
 (0)