Skip to content

Commit 3b7ee61

Browse files
quinnjoxinabox
andauthored
Switch verbosity macros to use group argument (#73)
* Switch verbosity macros to use group argument As noted in [this comment](https://github.com/JuliaLogging/LoggingExtras.jl/pull/64/files#r903295686), there's currently a semantic error with the verbosity macros where even if my verbosity is explicitly set to 0 or 1, but my log level is `Info`, then a message like: ```julia @errorv 2 "more detailed error message than you normally want to see" ``` will still be logged, since the current logic just does `Error - 2`. With the change proposed in this PR, we get the following change in behavior: * If `LoggingExtras.withlevel` or an alternative filter doesn't inspect the group.verbosity argument, then the verbose macros act just like the normal logging macros * If `withlevel` _is_ used, then the above case acts as expected; i.e. the `@errorv 2 msg` case only logs if `withlevel(Info; verbosity=2)` is set. I opted to wrap the verbosity level in a new `LoggingExtras.Verbosity` struct so that if the `group` argument is ever used for something else as an `Int`, it won't accidentally conflict with the verbosity filtering. We'll still clobber that argument if anyone else tries to use it, but it shouldn't affect cases where people _aren't_ using the verbosity macros. Fixes JuliaWeb/HTTP.jl#938. * additional test * Update src/verbosity.jl Co-authored-by: Frames Catherine White <[email protected]> * inject Verbosity into quote expansion Co-authored-by: Frames Catherine White <[email protected]>
1 parent be221f3 commit 3b7ee61

File tree

2 files changed

+31
-22
lines changed

2 files changed

+31
-22
lines changed

src/verbosity.jl

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,51 +18,56 @@ vlogmacrodocs = """
1818
1919
"Verbose" logging macros. Drop in replacements of standard logging macros, but an
2020
additional verbosity level `N` is passed to indicate differing verbosity levels
21-
for a given log level. The verbosity argument is subtracted from the base log level when passed down
22-
to the core logging logic, so `@debugv 1 msg` will essentially call `@logmsg Debug-1 msg`.
21+
for a given log level. The verbosity argument is passed as the `group` argument
22+
to the core logging logic as a `LoggingExtras.Verbosity` object.
2323
24-
An `LoggingExtras.LevelOverrideLogger`can then be used to filter on the `level` argument.
24+
Note these "verbose" logging messages will only be filtered if a filter logger is used.
25+
A `LoggingExtras.EarlyFilteredLogger`can be used to filter on the `group.verbosity` argument.
2526
For convenience, the
26-
[`LoggintExtras.with(f; level, verbosity)`](@ref) function is provided to temporarily
27-
wrap the current logger with a log level and verbosity subtracted to filter while `f` is executed.
27+
[`LoggintExtras.withlevel(f, level; verbosity)`](@ref) function is provided to temporarily
28+
wrap the current logger with a log level and verbosity to filter while `f` is executed.
2829
"""
2930

31+
struct Verbosity
32+
verbosity::Int
33+
end
34+
3035
"$vlogmacrodocs"
3136
macro debugv(verbosity::Int, msg, exs...)
3237
return restore_callsite_source_position!(
33-
esc(:($Base.@logmsg (Logging.Debug - $verbosity) $msg $(exs...))),
38+
esc(:($Base.@debug $msg _group=$(Verbosity(verbosity)) $(exs...))),
3439
__source__,
3540
)
3641
end
3742

3843
"$vlogmacrodocs"
3944
macro infov(verbosity::Int, msg, exs...)
4045
return restore_callsite_source_position!(
41-
esc(:($Base.@logmsg (Logging.Info - $verbosity) $msg $(exs...))),
46+
esc(:($Base.@info $msg _group=$(Verbosity(verbosity)) $(exs...))),
4247
__source__,
4348
)
4449
end
4550

4651
"$vlogmacrodocs"
4752
macro warnv(verbosity::Int, msg, exs...)
4853
return restore_callsite_source_position!(
49-
esc(:($Base.@logmsg (Logging.Warn - $verbosity) $msg $(exs...))),
54+
esc(:($Base.@warn $msg _group=$(Verbosity(verbosity)) $(exs...))),
5055
__source__,
5156
)
5257
end
5358

5459
"$vlogmacrodocs"
5560
macro errorv(verbosity::Int, msg, exs...)
5661
return restore_callsite_source_position!(
57-
esc(:($Base.@logmsg (Logging.Error - $verbosity) $msg $(exs...))),
62+
esc(:($Base.@error $msg _group=$(Verbosity(verbosity)) $(exs...))),
5863
__source__,
5964
)
6065
end
6166

6267
"$vlogmacrodocs"
6368
macro logmsgv(verbosity::Int, level, msg, exs...)
6469
return restore_callsite_source_position!(
65-
esc(:($Base.@logmsg ($level - $verbosity) $msg $(exs...))),
70+
esc(:($Base.@logmsg $level $msg _group=$(Verbosity(verbosity)) $(exs...))),
6671
__source__,
6772
)
6873
end
@@ -75,17 +80,16 @@ the current logger with a level filter while `f` is executed.
7580
That is, the current logger will still be used for actual logging, but
7681
log messages will first be checked that they meet the `level`
7782
log level before being passed on to be logged.
83+
84+
For convenience, a `verbosity` keyword argument can be passed which also
85+
filters the "verbose logging" messages; see [`@debugv`](@ref), [`@infov`](@ref),
86+
[`@warnv`](@ref), [`@errorv`](@ref), and [`@logmsgv`](@ref).
7887
"""
7988
function withlevel(f, level::Union{Int, LogLevel}=Info; verbosity::Integer=0)
80-
lvl = Base.CoreLogging._min_enabled_level[]
81-
try
82-
# by default, this global filter is Debug, but for debug logging
83-
# we want to enable sub-Debug levels
84-
Base.CoreLogging._min_enabled_level[] = BelowMinLevel
85-
with_logger(LevelOverrideLogger(level - verbosity, current_logger())) do
86-
f()
87-
end
88-
finally
89-
Base.CoreLogging._min_enabled_level[] = lvl
89+
with_logger(EarlyFilteredLogger(
90+
args -> !(args.group isa Verbosity) || verbosity >= args.group.verbosity,
91+
LevelOverrideLogger(level, current_logger()))
92+
) do
93+
f()
9094
end
9195
end

test/runtests.jl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,8 @@ end
255255
with_logger(logger) do
256256
@infov 1 "info 1 message"
257257
end
258-
@test isempty(logger.logs)
258+
# if no verbosity filter is used, message is just like @info
259+
@test !isempty(logger.logs)
259260

260261
logger = TestLogger(min_level=Info)
261262
with_logger(logger) do
@@ -271,10 +272,14 @@ end
271272
@debugv 0 "debug 0 message"
272273
@debugv 1 "debug 1 message"
273274
@debugv 2 "debug 2 message"
275+
# error message *also* isn't logged since
276+
# level *and* verbosity must match
277+
@errorv 2 "error 2 message"
274278
end
275279
end
276280
@test length(logger.logs) == 2
277-
@test map(x -> x.level, logger.logs) == [Debug, Debug-1]
281+
@test logger.logs[1].group == LoggingExtras.Verbosity(0)
282+
@test logger.logs[2].group == LoggingExtras.Verbosity(1)
278283

279284
logger = TestLogger(min_level=Info)
280285
with_logger(logger) do

0 commit comments

Comments
 (0)