Skip to content

Commit e67389b

Browse files
allow custom log level names & colors
1 parent 641f717 commit e67389b

File tree

5 files changed

+38
-9
lines changed

5 files changed

+38
-9
lines changed

base/logging.jl

+12-7
Original file line numberDiff line numberDiff line change
@@ -162,14 +162,19 @@ const AboveMaxLevel = LogLevel( 1000001)
162162
# Global log limiting mechanism for super fast but inflexible global log limiting.
163163
const _min_enabled_level = Ref{LogLevel}(Debug)
164164

165+
# add to this to dict to introduce a log level for printing
166+
# i.e. custom_log_levels[LogLevel(-500)] = ("MyLog", :magenta)
167+
const custom_log_levels = Dict{LogLevel,Tuple{String,Symbol}}()
168+
165169
function show(io::IO, level::LogLevel)
166-
if level == BelowMinLevel print(io, "BelowMinLevel")
167-
elseif level == Debug print(io, "Debug")
168-
elseif level == Info print(io, "Info")
169-
elseif level == Warn print(io, "Warn")
170-
elseif level == Error print(io, "Error")
171-
elseif level == AboveMaxLevel print(io, "AboveMaxLevel")
172-
else print(io, "LogLevel($(level.level))")
170+
if level in keys(custom_log_levels) print(io, custom_log_levels[level][1]::String)
171+
elseif level == BelowMinLevel print(io, "BelowMinLevel")
172+
elseif level == Debug print(io, "Debug")
173+
elseif level == Info print(io, "Info")
174+
elseif level == Warn print(io, "Warn")
175+
elseif level == Error print(io, "Error")
176+
elseif level == AboveMaxLevel print(io, "AboveMaxLevel")
177+
else print(io, "LogLevel($(level.level))")
173178
end
174179
end
175180

stdlib/Logging/docs/src/index.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ automatically extracted. Let's examine the user-defined data first:
5858
* The *log level* is a broad category for the message that is used for early
5959
filtering. There are several standard levels of type [`LogLevel`](@ref);
6060
user-defined levels are also possible.
61-
Each is distinct in purpose:
61+
Each built-in is distinct in purpose:
6262
- [`Logging.Debug`](@ref) (log level -1000) is information intended for the developer of
6363
the program. These events are disabled by default.
6464
- [`Logging.Info`](@ref) (log level 0) is for general information to the user.
@@ -70,6 +70,15 @@ automatically extracted. Let's examine the user-defined data first:
7070
Often this log-level is unneeded as throwing an exception can convey
7171
all the required information.
7272

73+
You can also asign printing styles for custom log levels. For instance:
74+
```
75+
MyLog = LogLevel(-500)
76+
custom_log_levels[MyLog] = ("MyLog", :magenta)
77+
macro mylog(exs...) Base.CoreLogging.logmsg_code((Base.CoreLogging.@_sourceinfo)..., MyLog, exs...) end
78+
79+
@mylog "foo"
80+
```
81+
7382
* The *message* is an object describing the event. By convention
7483
`AbstractString`s passed as messages are assumed to be in markdown format.
7584
Other types will be displayed using `print(io, obj)` or `string(obj)` for
@@ -298,6 +307,7 @@ Logging.Debug
298307
Logging.Info
299308
Logging.Warn
300309
Logging.Error
310+
Logging.custom_log_levels
301311
```
302312

303313
### [Processing events with AbstractLogger](@id AbstractLogger-interface)

stdlib/Logging/src/ConsoleLogger.jl

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ end
5858
showvalue(io, ex::Exception) = showerror(io, ex)
5959

6060
function default_logcolor(level::LogLevel)
61+
level in keys(custom_log_levels) ? custom_log_levels[level][2]::Symbol :
6162
level < Info ? Base.debug_color() :
6263
level < Warn ? Base.info_color() :
6364
level < Error ? Base.warn_color() :

stdlib/Logging/src/Logging.jl

+12
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,18 @@ Alias for [`LogLevel(2000)`](@ref LogLevel).
5555
"""
5656
const Error = Base.CoreLogging.Error
5757

58+
"""
59+
custom_log_levels::Dict{LogLevel,Tuple{String,Symbol}}
60+
61+
Add to this dict to introduce a print style for a custom log.
62+
63+
For instance:
64+
```
65+
custom_log_levels[LogLevel(-500)] = ("MyLog", :magenta)
66+
```
67+
"""
68+
const custom_log_levels = Base.CoreLogging.custom_log_levels
69+
5870
using Base.CoreLogging:
5971
closed_stream
6072

stdlib/Logging/test/runtests.jl

+2-1
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ end
280280
end
281281

282282
@testset "custom log macro" begin
283+
Logging.custom_log_levels[CustomLog] = ("CustomLog", :magenta)
283284
@test_logs (CustomLog, "a") min_level=CustomLog @customlog "a"
284285

285286
buf = IOBuffer()
@@ -289,7 +290,7 @@ end
289290
with_logger(logger) do
290291
@customlog "a"
291292
end
292-
@test occursin("LogLevel(-500): a", String(take!(buf)))
293+
@test occursin("CustomLog: a", String(take!(buf)))
293294
end
294295

295296
end

0 commit comments

Comments
 (0)