Skip to content

Commit dd1b706

Browse files
add create_log_macro for easily making log macros
1 parent 94aaf97 commit dd1b706

File tree

4 files changed

+50
-14
lines changed

4 files changed

+50
-14
lines changed

base/logging.jl

+34-3
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,11 @@ const AboveMaxLevel = LogLevel( 1000001)
163163
const _min_enabled_level = Ref{LogLevel}(Debug)
164164

165165
# 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}}()
166+
# i.e. custom_log_levels[LogLevel(-500)] = (:mylog, :magenta)
167+
const custom_log_levels = Dict{LogLevel,Tuple{Symbol,Symbol}}()
168168

169169
function show(io::IO, level::LogLevel)
170-
if level in keys(custom_log_levels) print(io, custom_log_levels[level][1]::String)
170+
if level in keys(custom_log_levels) print(io, custom_log_levels[level][1])
171171
elseif level == BelowMinLevel print(io, "BelowMinLevel")
172172
elseif level == Debug print(io, "Debug")
173173
elseif level == Info print(io, "Info")
@@ -694,4 +694,35 @@ end
694694

695695
_global_logstate = LogState(SimpleLogger())
696696

697+
"""
698+
create_log_macro(name::Symbol, level::Int, color::Union{Int,Symbol}=:default)
699+
create_log_macro(name::Symbol, loglevel::LogLevel, color::Union{Int,Symbol}=:default)
700+
701+
Creates a custom log macro like `@info`, `@warn` etc. with a given `name`, `level` and
702+
`color`. The macro created is named with the lowercase form of `name` but the given form
703+
is used for the printing.
704+
705+
See `Base.text_colors` for recognized color values.
706+
707+
```julia-repl
708+
julia> create_log_macro(:MyLog, 200, :magenta)
709+
@mylog (macro with 1 method)
710+
711+
julia> @mylog "hello"
712+
[ MyLog: hello
713+
```
714+
"""
715+
function create_log_macro(name::Symbol, loglevel::LogLevel, color::Union{Int,Symbol}=:default)
716+
haskey(Base.text_colors, color) || @warn "Color $(repr(color)) not recognized"
717+
custom_log_levels[loglevel] = (name, color)
718+
macro_name = Symbol(lowercase(string(name)))
719+
@eval begin
720+
macro $macro_name(exs...)
721+
logmsg_code((@_sourceinfo)..., $loglevel, exs...)
722+
end
723+
end
724+
end
725+
create_log_macro(name::Symbol, level::Int, color::Union{Int,Symbol}=:default) =
726+
create_log_macro(name, LogLevel(level), color)
727+
697728
end # CoreLogging

stdlib/Logging/docs/src/index.md

+8-6
Original file line numberDiff line numberDiff line change
@@ -70,13 +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
73+
You can also create logging macros for custom log levels. For instance:
74+
```julia-repl
75+
julia> using Logging
76+
77+
julia> create_log_macro(:MyLog, 200, :magenta)
78+
@mylog (macro with 1 method)
7879
79-
@mylog "foo"
80+
julia> @mylog "hello"
81+
[ MyLog: hello
8082
```
8183
8284
* The *message* is an object describing the event. By convention

stdlib/Logging/src/Logging.jl

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ for sym in [
2121
Symbol("@warn"),
2222
Symbol("@error"),
2323
Symbol("@logmsg"),
24+
:create_log_macro,
2425
:with_logger,
2526
:current_logger,
2627
:global_logger,
@@ -79,6 +80,7 @@ export
7980
@warn,
8081
@error,
8182
@logmsg,
83+
create_log_macro,
8284
with_logger,
8385
current_logger,
8486
global_logger,

stdlib/Logging/test/runtests.jl

+6-5
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@ import Logging: min_enabled_level, shouldlog, handle_message
66

77
@noinline func1() = backtrace()
88

9-
# see "custom log macro" testset
10-
CustomLog = LogLevel(-500)
11-
macro customlog(exs...) Base.CoreLogging.logmsg_code((Base.CoreLogging.@_sourceinfo)..., esc(CustomLog), exs...) end
12-
139
@testset "Logging" begin
1410

1511
@testset "Core" begin
@@ -280,7 +276,12 @@ end
280276
end
281277

282278
@testset "custom log macro" begin
283-
Logging.custom_log_levels[CustomLog] = ("CustomLog", :magenta)
279+
CustomLog = LogLevel(-500)
280+
281+
# TODO: Figure out how to avoid the need to `var"@customlog" = `
282+
# Maybe this should be a macro, but I couldn't figure out a macro creating a macro
283+
var"@customlog" = create_log_macro("CustomLog", CustomLog, :magenta)
284+
284285
@test_logs (CustomLog, "a") min_level=CustomLog @customlog "a"
285286

286287
buf = IOBuffer()

0 commit comments

Comments
 (0)