Skip to content

Commit 8bf6a07

Browse files
revert "Add @create_log_macro for making custom styled logging macros (#52196)" (#53551)
1 parent 2b95956 commit 8bf6a07

File tree

6 files changed

+14
-77
lines changed

6 files changed

+14
-77
lines changed

HISTORY.md

-3
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,6 @@ Standard library changes
148148
* `lu` and `issuccess(::LU)` now accept an `allowsingular` keyword argument. When set to `true`, a valid factorization with rank-deficient U factor will be treated as success instead of throwing an error. Such factorizations are now shown by printing the factors together with a "rank-deficient" note rather than printing a "Failed Factorization" message ([#52957]).
149149

150150
#### Logging
151-
* New `@create_log_macro` macro for creating new log macros like `@info`, `@warn` etc. For instance
152-
`@create_log_macro MyLog 1500 :magenta` will create `@mylog` to be used like `@mylog "hello"` which
153-
will show as `┌ MyLog: hello` etc. ([#52196])
154151

155152
#### Printf
156153

base/logging.jl

+7-11
Original file line numberDiff line numberDiff line change
@@ -172,18 +172,14 @@ const AboveMaxLevel = LogLevel( 1000001)
172172
# Global log limiting mechanism for super fast but inflexible global log limiting.
173173
const _min_enabled_level = Ref{LogLevel}(Debug)
174174

175-
# stored as LogLevel => (name, color)
176-
const custom_log_levels = Dict{LogLevel,Tuple{Symbol,Union{Symbol,Int}}}()
177-
178175
function show(io::IO, level::LogLevel)
179-
if haskey(custom_log_levels, level) print(io, custom_log_levels[level][1])
180-
elseif level == BelowMinLevel print(io, "BelowMinLevel")
181-
elseif level == Debug print(io, "Debug")
182-
elseif level == Info print(io, "Info")
183-
elseif level == Warn print(io, "Warn")
184-
elseif level == Error print(io, "Error")
185-
elseif level == AboveMaxLevel print(io, "AboveMaxLevel")
186-
else print(io, "LogLevel($(level.level))")
176+
if level == BelowMinLevel print(io, "BelowMinLevel")
177+
elseif level == Debug print(io, "Debug")
178+
elseif level == Info print(io, "Info")
179+
elseif level == Warn print(io, "Warn")
180+
elseif level == Error print(io, "Error")
181+
elseif level == AboveMaxLevel print(io, "AboveMaxLevel")
182+
else print(io, "LogLevel($(level.level))")
187183
end
188184
end
189185

stdlib/Logging/docs/src/index.md

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

77-
You can create logging macros for custom log levels. For instance:
78-
```julia-repl
79-
julia> using Logging
80-
81-
julia> @create_log_macro MyLog 200 :magenta
82-
@mylog (macro with 1 method)
83-
84-
julia> @mylog "hello"
85-
[ MyLog: hello
86-
```
87-
8877
* The *message* is an object describing the event. By convention
8978
`AbstractString`s passed as messages are assumed to be in markdown format.
9079
Other types will be displayed using `print(io, obj)` or `string(obj)` for
@@ -315,7 +304,6 @@ Logging.Warn
315304
Logging.Error
316305
Logging.BelowMinLevel
317306
Logging.AboveMaxLevel
318-
Logging.@create_log_macro
319307
```
320308

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

stdlib/Logging/src/ConsoleLogger.jl

-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ 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] :
6261
level < Info ? :log_debug :
6362
level < Warn ? :log_info :
6463
level < Error ? :log_warn :

stdlib/Logging/src/Logging.jl

-35
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ for sym in [
2323
Symbol("@warn"),
2424
Symbol("@error"),
2525
Symbol("@logmsg"),
26-
:custom_log_levels,
2726
:with_logger,
2827
:current_logger,
2928
:global_logger,
@@ -32,39 +31,6 @@ for sym in [
3231
@eval const $sym = Base.CoreLogging.$sym
3332
end
3433

35-
"""
36-
@create_log_macro(name::Symbol, level::Int, face::Union{Symbol, StyledStrings.Face})
37-
38-
Creates a custom log macro like `@info`, `@warn` etc. with a given `name`,
39-
`level` to be displayed with `face`. The macro created is named with the
40-
lowercase form of `name` but the given form is used for the printing.
41-
42-
```julia-repl
43-
julia> @create_log_macro(:MyLog, 200, :magenta)
44-
@mylog (macro with 1 method)
45-
46-
julia> @mylog "hello"
47-
[ MyLog: hello
48-
```
49-
"""
50-
macro create_log_macro(name, level, color)
51-
macro_name = Symbol(lowercase(string(name)))
52-
macro_string = QuoteNode(name)
53-
loglevel = LogLevel(level)
54-
if loglevel in (BelowMinLevel, Debug, Info, Warn, Error, AboveMaxLevel)
55-
throw(ArgumentError("Cannot use the same log level as a built in log macro"))
56-
end
57-
if haskey(custom_log_levels, loglevel)
58-
throw(ArgumentError("Custom log macro already exists for given log level"))
59-
end
60-
quote
61-
$(custom_log_levels)[$(esc(loglevel))] = ($(macro_string), $(esc(color)))
62-
macro $(esc(macro_name))(exs...)
63-
$(Base.CoreLogging.logmsg_code)(($(Base.CoreLogging.@_sourceinfo))..., $(esc(loglevel)), exs...)
64-
end
65-
end
66-
end
67-
6834
# LogLevel aliases (re-)documented here (JuliaLang/julia#40978)
6935
"""
7036
Debug
@@ -115,7 +81,6 @@ export
11581
@warn,
11682
@error,
11783
@logmsg,
118-
@create_log_macro,
11984
with_logger,
12085
current_logger,
12186
global_logger,

stdlib/Logging/test/runtests.jl

+6-14
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import Logging: min_enabled_level, shouldlog, handle_message
77
@noinline func1() = backtrace()
88

99
# see "custom log macro" testset
10-
@create_log_macro CustomLog1 -500 :magenta
11-
@create_log_macro CustomLog2 1500 1
10+
CustomLog = LogLevel(-500)
11+
macro customlog(exs...) Base.CoreLogging.logmsg_code((Base.CoreLogging.@_sourceinfo)..., esc(CustomLog), exs...) end
1212

1313
@testset "Logging" begin
1414

@@ -289,24 +289,16 @@ end
289289
end
290290

291291
@testset "custom log macro" begin
292-
llevel = LogLevel(-500)
293-
294-
@test_logs (llevel, "foo") min_level=llevel @customlog1 "foo"
292+
@test_logs (CustomLog, "a") min_level=CustomLog @customlog "a"
295293

296294
buf = IOBuffer()
297295
io = IOContext(buf, :displaysize=>(30,80), :color=>false)
298-
logger = ConsoleLogger(io, llevel)
299-
300-
with_logger(logger) do
301-
@customlog1 "foo"
302-
end
303-
@test occursin("CustomLog1: foo", String(take!(buf)))
304-
296+
logger = ConsoleLogger(io, CustomLog)
305297

306298
with_logger(logger) do
307-
@customlog2 "hello"
299+
@customlog "a"
308300
end
309-
@test occursin("CustomLog2: hello", String(take!(buf)))
301+
@test occursin("LogLevel(-500): a", String(take!(buf)))
310302
end
311303

312304
@testset "Docstrings" begin

0 commit comments

Comments
 (0)