Skip to content

Commit

Permalink
allow custom log level names & colors
Browse files Browse the repository at this point in the history
  • Loading branch information
IanButterworth committed Dec 2, 2023
1 parent 641f717 commit e67389b
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 9 deletions.
19 changes: 12 additions & 7 deletions base/logging.jl
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,19 @@ const AboveMaxLevel = LogLevel( 1000001)
# Global log limiting mechanism for super fast but inflexible global log limiting.
const _min_enabled_level = Ref{LogLevel}(Debug)

# add to this to dict to introduce a log level for printing
# i.e. custom_log_levels[LogLevel(-500)] = ("MyLog", :magenta)
const custom_log_levels = Dict{LogLevel,Tuple{String,Symbol}}()

function show(io::IO, level::LogLevel)
if level == BelowMinLevel print(io, "BelowMinLevel")
elseif level == Debug print(io, "Debug")
elseif level == Info print(io, "Info")
elseif level == Warn print(io, "Warn")
elseif level == Error print(io, "Error")
elseif level == AboveMaxLevel print(io, "AboveMaxLevel")
else print(io, "LogLevel($(level.level))")
if level in keys(custom_log_levels) print(io, custom_log_levels[level][1]::String)
elseif level == BelowMinLevel print(io, "BelowMinLevel")
elseif level == Debug print(io, "Debug")
elseif level == Info print(io, "Info")
elseif level == Warn print(io, "Warn")
elseif level == Error print(io, "Error")
elseif level == AboveMaxLevel print(io, "AboveMaxLevel")
else print(io, "LogLevel($(level.level))")
end
end

Expand Down
12 changes: 11 additions & 1 deletion stdlib/Logging/docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ automatically extracted. Let's examine the user-defined data first:
* The *log level* is a broad category for the message that is used for early
filtering. There are several standard levels of type [`LogLevel`](@ref);
user-defined levels are also possible.
Each is distinct in purpose:
Each built-in is distinct in purpose:
- [`Logging.Debug`](@ref) (log level -1000) is information intended for the developer of
the program. These events are disabled by default.
- [`Logging.Info`](@ref) (log level 0) is for general information to the user.
Expand All @@ -70,6 +70,15 @@ automatically extracted. Let's examine the user-defined data first:
Often this log-level is unneeded as throwing an exception can convey
all the required information.

You can also asign printing styles for custom log levels. For instance:

Check warning on line 73 in stdlib/Logging/docs/src/index.md

View workflow job for this annotation

GitHub Actions / Check for new typos

perhaps "asign" should be "assign".
```
MyLog = LogLevel(-500)
custom_log_levels[MyLog] = ("MyLog", :magenta)
macro mylog(exs...) Base.CoreLogging.logmsg_code((Base.CoreLogging.@_sourceinfo)..., MyLog, exs...) end
@mylog "foo"
```

* The *message* is an object describing the event. By convention
`AbstractString`s passed as messages are assumed to be in markdown format.
Other types will be displayed using `print(io, obj)` or `string(obj)` for
Expand Down Expand Up @@ -298,6 +307,7 @@ Logging.Debug
Logging.Info
Logging.Warn
Logging.Error
Logging.custom_log_levels
```

### [Processing events with AbstractLogger](@id AbstractLogger-interface)
Expand Down
1 change: 1 addition & 0 deletions stdlib/Logging/src/ConsoleLogger.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ end
showvalue(io, ex::Exception) = showerror(io, ex)

function default_logcolor(level::LogLevel)
level in keys(custom_log_levels) ? custom_log_levels[level][2]::Symbol :
level < Info ? Base.debug_color() :
level < Warn ? Base.info_color() :
level < Error ? Base.warn_color() :
Expand Down
12 changes: 12 additions & 0 deletions stdlib/Logging/src/Logging.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ Alias for [`LogLevel(2000)`](@ref LogLevel).
"""
const Error = Base.CoreLogging.Error

"""
custom_log_levels::Dict{LogLevel,Tuple{String,Symbol}}
Add to this dict to introduce a print style for a custom log.
For instance:
```
custom_log_levels[LogLevel(-500)] = ("MyLog", :magenta)
```
"""
const custom_log_levels = Base.CoreLogging.custom_log_levels

using Base.CoreLogging:
closed_stream

Expand Down
3 changes: 2 additions & 1 deletion stdlib/Logging/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ end
end

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

buf = IOBuffer()
Expand All @@ -289,7 +290,7 @@ end
with_logger(logger) do
@customlog "a"
end
@test occursin("LogLevel(-500): a", String(take!(buf)))
@test occursin("CustomLog: a", String(take!(buf)))
end

end

0 comments on commit e67389b

Please sign in to comment.