Skip to content

Commit 99835e1

Browse files
committed
Add docs
1 parent 8226ad9 commit 99835e1

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ All of them, except `FormatLogger`, just wrap existing loggers.
9393
- The `FileLogger` is a simple logger sink that writes to file.
9494
- The `DatetimeRotatingFileLogger` is a logger sink that writes to file, rotating logs based upon a user-provided `DateFormat`.
9595
- The `FormatLogger` is a logger sink that simply formats the message and writes to the logger stream.
96+
- The `LevelOverrideLogger` for overriding the log level of other loggers
9697

9798
By combining `TeeLogger` with filter loggers you can arbitrarily route log messages, wherever you want.
9899

@@ -364,6 +365,56 @@ Main | [Info] This is an informational message.
364365
Main | [Warn] This is a warning, should take a look.
365366
```
366367
368+
## `LevelOverrideLogger` (*Filter*)
369+
Allows overriding the log level set by any nested logger. Useful when debug logging
370+
and used in conjuction with `Logging.with_logger` or `LoggingExtras.with` to
371+
temporarily modify the current logger with a custom level.
372+
373+
```julia
374+
julia> using LoggingExtras
375+
376+
julia> logger = LevelOverrideLogger(Debug, global_logger())
377+
378+
julia> with_logger(logger) do
379+
@debug "This message will log since we're overriding the global Info default log level"
380+
end
381+
┌ Debug: This message will log since we're overriding the global Info default log level
382+
└ @ Main REPL[33]:2
383+
```
384+
385+
# Utilities
386+
387+
## Verbosity macros
388+
Sometimes when logging, it is desirable to be able to specify a verbosity level in addition to
389+
the log level, and to be able to filter on verbosity levels. For example, you may want multiple levels
390+
of verbosity for `Debug` log statements. LoggingExtras.jl exports verbosity macros that act like their
391+
non-verbose counterparts, but allow specifying a verbosity level as well:
392+
* `@debugv N msg`
393+
* `@infov N msg`
394+
* `@warnv N msg`
395+
* `@errorv N msg`
396+
397+
For verbosity filtering, the `LoggingExtras.with(f; level=Info, verbosity=0)` utlility is provided
398+
for temporarily (i.e. while `f()` is executed) allowing log messages with `level` and `verbosity`.
399+
This is very handy for allowing control in debug logging for long-running or complex user API function
400+
calls. For example:
401+
402+
```julia
403+
using LoggingExtras
404+
405+
function complex_user_call(; verbose=0)
406+
LoggingExtras.with(; level=Debug, verbosity=verbose)
407+
# execute complex function body
408+
@debugv 1 "a level 1 verbosity debug message"
409+
@debugv 2 "a more verbose level 2 debug message"
410+
end
411+
end
412+
```
413+
414+
This allows easy control by the user to specify verbosity (by passing `verbose=2` or any > 0 value),
415+
and convenience for the function developer by being able to sprinkle `@debugv N msg` calls as desired,
416+
even in highly nested functions.
417+
367418
# More Examples
368419
369420
## Filter out any overly long messages

src/LoggingExtras.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Base.CoreLogging:
1010

1111
export TeeLogger, TransformerLogger, FileLogger,
1212
ActiveFilteredLogger, EarlyFilteredLogger, MinLevelLogger,
13-
DatetimeRotatingFileLogger, FormatLogger,
13+
DatetimeRotatingFileLogger, FormatLogger, LevelOverrideLogger,
1414
@debugv, @infov, @warnv, @errorv, @logmsgv
1515

1616
######

src/overridelogger.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
"""
2+
LevelOverrideLogger(level, logger)
3+
4+
A logger that allows overriding the log level of a child level.
5+
Useful in debugging scenarios, where it is desirable to ignore
6+
the log level any other logger may have set.
7+
"""
18
struct LevelOverrideLogger{T <: AbstractLogger} <: AbstractLogger
29
level::LogLevel
310
logger::T

0 commit comments

Comments
 (0)