Skip to content

Commit 8226ad9

Browse files
committed
Simplify/cleanup
1 parent 677d925 commit 8226ad9

File tree

3 files changed

+71
-33
lines changed

3 files changed

+71
-33
lines changed

src/LoggingExtras.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ include("formatlogger.jl")
5353
include("datetime_rotation.jl")
5454
include("overridelogger.jl")
5555
include("verbosity.jl")
56-
include("util.jl")
5756
include("deprecated.jl")
5857

5958
end # module

src/util.jl

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/verbosity.jl

Lines changed: 71 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,85 @@
1-
"""Calls @debug with the passed verbosity level"""
1+
function restore_callsite_source_position!(expr, src)
2+
# used to fix the logging source file + line
3+
# since we're lowering our verbose logging macros to the
4+
# Logging.jl macros; otherwise, they would always report this (verbosity.jl)
5+
# file as the logging callsite
6+
expr.args[1].args[2] = src
7+
return expr
8+
end
9+
10+
vlogmacrodocs = """
11+
@debugv N msg args...
12+
@infov N msg args...
13+
@warnv N msg args...
14+
@errorv N msg args...
15+
16+
"Verbose" logging macros. Drop in replacements of standard logging macros, but an
17+
additional verbosity level `N` is passed to indicate differing verbosity levels
18+
for a given log level. The verbosity argument is passed as the `group` argument
19+
to the core logging logic, so care should be taken if other loggers are being used
20+
that also use the group argument (which by default is passed the source file).
21+
22+
The default logger doesn't have support for filtering on the `group` argument,
23+
so an `LoggingExtras.EarlyFilteredLogger` can be used. For convenience, a
24+
[`LoggintExtras.with(f; level, verbosity)`](@ref) function is provided to temporarily
25+
wrap the current logger with a level/verbosity filter while `f` is executed.
26+
"""
27+
228
macro debugv(verbosity::Int64, msg, exs...)
3-
return esc(:($Base.@debug $msg verbosity=$verbosity $(exs...)))
29+
return restore_callsite_source_position!(
30+
esc(:($Base.@debug $msg _group=$verbosity $(exs...))),
31+
__source__,
32+
)
433
end
534

6-
"""Calls @info with the passed verbosity level"""
735
macro infov(verbosity::Int64, msg, exs...)
8-
return esc(:($Base.@info $msg verbosity=$verbosity $(exs...)))
36+
return restore_callsite_source_position!(
37+
esc(:($Base.@info $msg _group=$verbosity $(exs...))),
38+
__source__,
39+
)
940
end
1041

11-
"""Calls @warn with the passed verbosity level"""
1242
macro warnv(verbosity::Int64, msg, exs...)
13-
return esc(:($Base.@warn $msg verbosity=$verbosity $(exs...)))
43+
return restore_callsite_source_position!(
44+
esc(:($Base.@warn $msg _group=$verbosity $(exs...))),
45+
__source__,
46+
)
1447
end
1548

16-
"""Calls @error with the passed verbosity level"""
1749
macro errorv(verbosity::Int64, msg, exs...)
18-
return esc(:($Base.@error $msg verbosity=$verbosity $(exs...)))
50+
return restore_callsite_source_position!(
51+
esc(:($Base.@error $msg _group=$verbosity $(exs...))),
52+
__source__,
53+
)
1954
end
2055

21-
"""Calls @error with the passed verbosity level"""
2256
macro logmsgv(verbosity::Int64, level, msg, exs...)
23-
return esc(:($Base.@logmsgv $level $msg verbosity=$verbosity $(exs...)))
57+
return restore_callsite_source_position!(
58+
esc(:($Base.@logmsg $level $msg _group=$verbosity $(exs...))),
59+
__source__,
60+
)
61+
end
62+
63+
@eval @doc $vlogmacrodocs :(@logmsgv)
64+
@eval @doc $vlogmacrodocs :(@debugv)
65+
@eval @doc $vlogmacrodocs :(@infov)
66+
@eval @doc $vlogmacrodocs :(@warnv)
67+
@eval @doc $vlogmacrodocs :(@errorv)
68+
69+
"""
70+
LoggingExtras.with(f; level=Info, verbosity=0)
71+
72+
Convenience function like `Logging.with_logger` to temporarily wrap
73+
the current logger with a level/verbosity filter while `f` is executed.
74+
That is, the current logger will still be used for actual logging, but
75+
log messages will first be checked that they meet the `level` and
76+
`verbosity` levels before being passed on to be logged.
77+
"""
78+
function with(f; level::Union{Int, LogLevel}=Info, verbosity::Int=0)
79+
with_logger(EarlyFilteredLogger(
80+
args -> !(args.group isa Int) || verbosity >= args.group,
81+
LevelOverrideLogger(level, current_logger()))
82+
) do
83+
f()
84+
end
2485
end

0 commit comments

Comments
 (0)