|
3 | 3 | [](https://travis-ci.org/oxinabox/LoggingExtras.jl) |
4 | 4 |
|
5 | 5 | [](http://codecov.io/github/oxinabox/LoggingExtras.jl?branch=master) |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +## Usage |
| 10 | +Load the package with `using LoggingExtras`. |
| 11 | +You likely also want to load the `Logging` standard lib. |
| 12 | +Loggers can be constructed and used like normal. |
| 13 | + |
| 14 | + |
| 15 | +### Basics of working with loggers |
| 16 | +For full details, see the [Julia documentation on Logging](https://docs.julialang.org/en/v1/stdlib/Logging/index.html) |
| 17 | + |
| 18 | +To use a `logger` in a given scope do |
| 19 | +``` |
| 20 | +with_logger(logger) do |
| 21 | + #things |
| 22 | +end |
| 23 | +``` |
| 24 | + |
| 25 | +To make a logger the global logger, use |
| 26 | +``` |
| 27 | +global_logger(logger) |
| 28 | +``` |
| 29 | + |
| 30 | +to get the current global logger, use |
| 31 | +``` |
| 32 | +logger = global_logger() |
| 33 | +``` |
| 34 | + |
| 35 | +# Loggers introduced by this package: |
| 36 | + |
| 37 | +## `DemuxLogger` and `FileLogger` |
| 38 | + |
| 39 | +The `DemuxLogger` sends the log messages to multiple places. |
| 40 | +It takes a list of loggers. |
| 41 | +It also has the keyword argument `include_current_global`, |
| 42 | +to determine if you also want to log to the global logger. |
| 43 | + |
| 44 | +It is up to those loggers to determine if they will accept it.\ |
| 45 | +Which they do using their methods for `shouldlog` and `min_enabled_level`. |
| 46 | +Or you can do, by wrapping them in a `FilteredLogger` as discussed below. |
| 47 | + |
| 48 | +The `FileLogger` does logging to file. |
| 49 | +It is really simple. |
| 50 | +It takes a filename; and the minimum level it should log. |
| 51 | + |
| 52 | +### Demo |
| 53 | +We are going to log info and above to one file, |
| 54 | +and warnings and above to another. |
| 55 | + |
| 56 | +``` |
| 57 | +julia> using Logging; using LoggingExtras; |
| 58 | +
|
| 59 | +julia> demux_logger = DemuxLogger( |
| 60 | + FileLogger("info.log", min_level=Logging.Info), |
| 61 | + FileLogger("warn.log", min_level=Logging.Warn), |
| 62 | + include_current_global=false |
| 63 | + ); |
| 64 | +
|
| 65 | +
|
| 66 | +julia> with_logger(demux_logger) do |
| 67 | + @warn("It is bad") |
| 68 | + @info("normal stuff") |
| 69 | + @error("THE WORSE THING") |
| 70 | + @debug("it is chill") |
| 71 | + end |
| 72 | +
|
| 73 | +shell> cat warn.log |
| 74 | +┌ Warning: It is bad |
| 75 | +└ @ Main REPL[34]:2 |
| 76 | +┌ Error: THE WORSE THING |
| 77 | +└ @ Main REPL[34]:4 |
| 78 | +
|
| 79 | +shell> cat info.log |
| 80 | +┌ Warning: It is bad |
| 81 | +└ @ Main REPL[34]:2 |
| 82 | +┌ Info: normal stuff |
| 83 | +└ @ Main REPL[34]:3 |
| 84 | +┌ Error: THE WORSE THING |
| 85 | +└ @ Main REPL[34]:4 |
| 86 | +``` |
| 87 | + |
| 88 | +## `FilteredLogger` |
| 89 | + |
| 90 | +The `FilteredLogger` exists to give more control over which messages should be logged. |
| 91 | +It warps any logger, and before sending messages to the logger to log, |
| 92 | +checks them against a filter function. |
| 93 | +The filter function takes the full set of parameters of the message. |
| 94 | +(See it's docstring with `?FilteredLogger` for more details.) |
| 95 | + |
| 96 | +### Demo |
| 97 | +We want to filter to only log strings staring with `"Yo Dawg!"`. |
| 98 | + |
| 99 | +``` |
| 100 | +julia> function yodawg_filter(level, message, _module, group, id, file, line; kwargs...) |
| 101 | + startswith(msg, "Yo Dawg!") |
| 102 | +end |
| 103 | + yodawg_filter (generic function with 1 method) |
| 104 | +
|
| 105 | +julia> filtered_logger = FilteredLogger(yodawg_filter, global_logger()); |
| 106 | +
|
| 107 | +julia> with_logger(filtered_logger) do |
| 108 | + @info "Boring message" |
| 109 | + @warn "Yo Dawg! it is bad" |
| 110 | + @info "Another boring message" |
| 111 | + @info "Yo Dawg! it is all good" |
| 112 | + end |
| 113 | +┌ Warning: Yo Dawg! it is bad |
| 114 | +└ @ Main REPL[28]:3 |
| 115 | +[ Info: Yo Dawg! it is all good |
| 116 | +``` |
| 117 | + |
0 commit comments