Skip to content

Commit 78df79e

Browse files
authored
Add maxlog example (#59)
1 parent 6045c24 commit 78df79e

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

README.md

+24
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,30 @@ end
138138
[ Info: Yo Dawg! it is all good
139139
```
140140
141+
### Respecting `maxlog` convention
142+
143+
An `ActiveFilterLogger` can be used to wrap another logger to obey `maxlog` directives, for example,
144+
similar to the `make_throttled_logger` example below,
145+
```julia
146+
function make_maxlog_logger(logger)
147+
counts = Dict{Any,Int}()
148+
return ActiveFilteredLogger(logger) do log
149+
maxlog = get(log.kwargs, :maxlog, nothing)
150+
maxlog === nothing && return true # no limit
151+
c = get(counts, log.id, 0)
152+
if c < maxlog
153+
# log this message and update the count
154+
counts[log.id] = c + 1
155+
return true
156+
else
157+
return false
158+
end
159+
end
160+
end
161+
```
162+
wraps another logger to filter logs that have already fired `maxlog` many times.
163+
See <https://docs.julialang.org/en/v1/stdlib/Logging/#Logging.@logmsg> for more on `maxlog`.
164+
141165
## `EarlyFilteredLogger` (*Filter*)
142166
143167
The `EarlyFilteredLogger` is similar to the `ActiveFilteredLogger`,

0 commit comments

Comments
 (0)