Skip to content

Commit a80071e

Browse files
add @create_log_macro for easily making log macros
1 parent 7a91126 commit a80071e

File tree

6 files changed

+60
-18
lines changed

6 files changed

+60
-18
lines changed

NEWS.md

+5
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ Standard library changes
116116
argument is the output of `bunchkaufman` or `lu` ([#50471]).
117117
* Structured matrices now retain either the axes of the parent (for `Symmetric`/`Hermitian`/`AbstractTriangular`/`UpperHessenberg`), or that of the principal diagonal (for banded matrices) ([#52480]).
118118

119+
#### Logging
120+
* New `@create_log_macro` macro for creating new log macros like `@info`, `@warn` etc. For instance
121+
`@create_log_macro MyLog 1500 :magenta` will create `@mylog` to be used like `@mylog "hello"` which
122+
will show as `┌ MyLog: hello` etc. ([#52196])
123+
119124
#### Printf
120125

121126
#### Profile

base/logging.jl

+31-4
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,10 @@ 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}}()
165+
const custom_log_levels = Dict{LogLevel,Tuple{Symbol,Union{Symbol,Int}}}()
168166

169167
function show(io::IO, level::LogLevel)
170-
if level in keys(custom_log_levels) print(io, custom_log_levels[level][1]::String)
168+
if level in keys(custom_log_levels) print(io, custom_log_levels[level][1])
171169
elseif level == BelowMinLevel print(io, "BelowMinLevel")
172170
elseif level == Debug print(io, "Debug")
173171
elseif level == Info print(io, "Info")
@@ -694,4 +692,33 @@ end
694692

695693
_global_logstate = LogState(SimpleLogger())
696694

695+
"""
696+
@create_log_macro(name::Symbol, level::Int, color::Union{Int,Symbol})
697+
698+
Creates a custom log macro like `@info`, `@warn` etc. with a given `name`, `level` and
699+
`color`. The macro created is named with the lowercase form of `name` but the given form
700+
is used for the printing.
701+
702+
See `Base.text_colors` for recognized color values.
703+
704+
```julia-repl
705+
julia> @create_log_macro(:MyLog, 200, :magenta)
706+
@mylog (macro with 1 method)
707+
708+
julia> @mylog "hello"
709+
[ MyLog: hello
710+
```
711+
"""
712+
macro create_log_macro(name, level, color)
713+
macro_name = Symbol(lowercase(string(name)))
714+
macro_string = QuoteNode(name)
715+
loglevel = LogLevel(level)
716+
quote
717+
custom_log_levels[$(esc(loglevel))] = ($(macro_string), $(esc(color)))
718+
macro $(esc(macro_name))(exs...)
719+
$logmsg_code(($@_sourceinfo)..., $(esc(loglevel)), exs...)
720+
end
721+
end
722+
end
723+
697724
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/ConsoleLogger.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +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 :
61+
level in keys(custom_log_levels) ? custom_log_levels[level][2] :
6262
level < Info ? Base.debug_color() :
6363
level < Warn ? Base.info_color() :
6464
level < Error ? Base.warn_color() :

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+
Symbol("@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

+13-7
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,17 +276,27 @@ end
280276
end
281277

282278
@testset "custom log macro" begin
283-
Logging.custom_log_levels[CustomLog] = ("CustomLog", :magenta)
284-
@test_logs (CustomLog, "a") min_level=CustomLog @customlog "a"
279+
@create_log_macro CustomLog -500 :magenta
280+
281+
llevel = LogLevel(-500)
282+
283+
@test_logs (llevel, "a") min_level=llevel @customlog "a"
285284

286285
buf = IOBuffer()
287286
io = IOContext(buf, :displaysize=>(30,80), :color=>false)
288-
logger = ConsoleLogger(io, CustomLog)
287+
logger = ConsoleLogger(io, llevel)
289288

290289
with_logger(logger) do
291290
@customlog "a"
292291
end
293292
@test occursin("CustomLog: a", String(take!(buf)))
293+
294+
@create_log_macro CustomLog2 1500 1
295+
296+
with_logger(logger) do
297+
@customlog2 "hello"
298+
end
299+
@test occursin("CustomLog2: a", String(take!(buf)))
294300
end
295301

296302
end

0 commit comments

Comments
 (0)