Skip to content

Commit caf7b94

Browse files
committed
address review comments
1 parent 73f17b8 commit caf7b94

File tree

4 files changed

+16
-13
lines changed

4 files changed

+16
-13
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ Main | [Warn] This is a warning, should take a look.
368368
## `LevelOverrideLogger` (*Filter*)
369369
Allows overriding the minimum log level set by the logger it wraps.
370370
Useful when debug logging
371-
and used in conjuction with `Logging.with_logger` or `LoggingExtras.with` to
371+
and used in conjuction with `Logging.with_logger` or `LoggingExtras.withlevel` to
372372
temporarily modify the current logger with a custom level.
373373
More generally useful if you want to use the current/global logger as a _sink_ but don't know if it is going to have a problematically high min log level set (as julia's default logger sets min level to `Info`).
374374
@@ -399,7 +399,7 @@ non-verbose counterparts, but allow specifying a verbosity level as well:
399399
* `@warnv N msg`
400400
* `@errorv N msg`
401401
402-
For verbosity filtering, the `LoggingExtras.with(f; level=Info, verbosity=0)` utlility is provided
402+
For verbosity filtering, the `LoggingExtras.withlevel(f, Info; verbosity=0)` utlility is provided
403403
for temporarily (i.e. while `f()` is executed) allowing log messages with `level` and `verbosity`.
404404
This is very handy for allowing finer grained control in debug logging for long-running or complex user API function
405405
calls. For example:
@@ -408,7 +408,7 @@ calls. For example:
408408
using LoggingExtras
409409

410410
function complex_user_call(; verbose=0)
411-
LoggingExtras.with(; level=Debug, verbosity=verbose)
411+
LoggingExtras.withlevel(Debug; verbosity=verbose)
412412
# execute complex function body
413413
@debugv 1 "a level 1 verbosity debug message"
414414
@debugv 2 "a more verbose level 2 debug message"

src/overridelogger.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ handle_message(logger::LevelOverrideLogger, args...; kwargs...) =
1616

1717
function shouldlog(logger::LevelOverrideLogger, level, args...)
1818
# Ignore the logger.logger's own level and instead check the override level
19+
@show level, logger.level
1920
level >= logger.level && shouldlog(logger.logger, level, args...)
2021
end
2122

src/verbosity.jl

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
function restore_callsite_source_position!(expr, src)
2+
@assert expr.head == :escape
3+
@assert expr.args[1].head == :macrocall
4+
@assert expr.args[1].args[2] isa LineNumberNode
25
# used to fix the logging source file + line
36
# since we're lowering our verbose logging macros to the
47
# Logging.jl macros; otherwise, they would always report this (verbosity.jl)
@@ -24,57 +27,56 @@ For convenience, the
2427
wrap the current logger with a log level and verbosity subtracted to filter while `f` is executed.
2528
"""
2629

30+
"$vlogmacrodocs"
2731
macro debugv(verbosity::Int, msg, exs...)
2832
return restore_callsite_source_position!(
2933
esc(:($Base.@logmsg (Logging.Debug - $verbosity) $msg $(exs...))),
3034
__source__,
3135
)
3236
end
3337

38+
"$vlogmacrodocs"
3439
macro infov(verbosity::Int, msg, exs...)
3540
return restore_callsite_source_position!(
3641
esc(:($Base.@logmsg (Logging.Info - $verbosity) $msg $(exs...))),
3742
__source__,
3843
)
3944
end
4045

46+
"$vlogmacrodocs"
4147
macro warnv(verbosity::Int, msg, exs...)
4248
return restore_callsite_source_position!(
4349
esc(:($Base.@logmsg (Logging.Warn - $verbosity) $msg $(exs...))),
4450
__source__,
4551
)
4652
end
4753

54+
"$vlogmacrodocs"
4855
macro errorv(verbosity::Int, msg, exs...)
4956
return restore_callsite_source_position!(
5057
esc(:($Base.@logmsg (Logging.Error - $verbosity) $msg $(exs...))),
5158
__source__,
5259
)
5360
end
5461

62+
"$vlogmacrodocs"
5563
macro logmsgv(verbosity::Int, level, msg, exs...)
5664
return restore_callsite_source_position!(
5765
esc(:($Base.@logmsg ($level - $verbosity) $msg $(exs...))),
5866
__source__,
5967
)
6068
end
6169

62-
@eval @doc $vlogmacrodocs :(@logmsgv)
63-
@eval @doc $vlogmacrodocs :(@debugv)
64-
@eval @doc $vlogmacrodocs :(@infov)
65-
@eval @doc $vlogmacrodocs :(@warnv)
66-
@eval @doc $vlogmacrodocs :(@errorv)
67-
6870
"""
69-
LoggingExtras.with(f; level=Info)
71+
LoggingExtras.withlevel(f, level; verbosity::Integer=0)
7072
7173
Convenience function like `Logging.with_logger` to temporarily wrap
7274
the current logger with a level filter while `f` is executed.
7375
That is, the current logger will still be used for actual logging, but
7476
log messages will first be checked that they meet the `level`
7577
log level before being passed on to be logged.
7678
"""
77-
function with(f; level::Union{Int, LogLevel}=Info, verbosity::Integer=0)
79+
function withlevel(f, level::Union{Int, LogLevel}=Info; verbosity::Integer=0)
7880
lvl = Base.CoreLogging._min_enabled_level[]
7981
try
8082
# by default, this global filter is Debug, but for debug logging

test/runtests.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,15 +243,15 @@ end
243243

244244
logger = TestLogger(min_level=Info)
245245
with_logger(logger) do
246-
LoggingExtras.with(level=Debug) do
246+
LoggingExtras.withlevel(Debug) do
247247
@debug "debug message"
248248
end
249249
end
250250
@test logger.logs[1].level == Debug
251251

252252
logger = TestLogger(min_level=Info)
253253
with_logger(logger) do
254-
LoggingExtras.with(level=Debug, verbosity=1) do
254+
LoggingExtras.withlevel(Debug; verbosity=1) do
255255
@debugv 0 "debug 0 message"
256256
@debugv 1 "debug 1 message"
257257
@debugv 2 "debug 2 message"

0 commit comments

Comments
 (0)