From 44c4e84fa4a150d5fd3510a465f2cb5181c07b07 Mon Sep 17 00:00:00 2001 From: Eric Hanson <5846501+ericphanson@users.noreply.github.com> Date: Tue, 14 Dec 2021 23:30:44 -0500 Subject: [PATCH 1/3] Add `maxlog` example I found it useful, and am using this code in https://github.com/beacon-biosignals/BeaconK8sUtilities.jl/pull/14 --- README.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/README.md b/README.md index f1042b4..ac4d201 100644 --- a/README.md +++ b/README.md @@ -138,6 +138,28 @@ end [ Info: Yo Dawg! it is all good ``` +### Respecting `maxlog` convention + +An `ActiveFilterLogger` can be used to wrap another logger to obey `maxlog` directives, for example, +similar to the `make_throttled_logger` example below, +```julia +function make_maxlog_logger(logger) + counts = Dict{Symbol,Int}() + return ActiveFilteredLogger(logger) do log + haskey(log.kwargs, :maxlog) || return true + if !haskey(counts, log.id) || (counts[log.id] < log.kwargs[:maxlog]) + # then we will log it, and update the corresponding count + counts[log.id] = get(counts, log.id, 0) + 1 + return true + else + return false + end + end +end +``` +wraps another logger to filter logs that have already fired `maxlog` many times. +See for more on `maxlog`. + ## `EarlyFilteredLogger` (*Filter*) The `EarlyFilteredLogger` is similar to the `ActiveFilteredLogger`, From 5851b048523ccb41a4089f31e97b2fb5b40bb415 Mon Sep 17 00:00:00 2001 From: Eric Hanson <5846501+ericphanson@users.noreply.github.com> Date: Fri, 17 Dec 2021 15:11:18 -0500 Subject: [PATCH 2/3] Update README.md Co-authored-by: Fredrik Ekre --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ac4d201..986b693 100644 --- a/README.md +++ b/README.md @@ -144,7 +144,7 @@ An `ActiveFilterLogger` can be used to wrap another logger to obey `maxlog` dire similar to the `make_throttled_logger` example below, ```julia function make_maxlog_logger(logger) - counts = Dict{Symbol,Int}() + counts = Dict{Any,Int}() return ActiveFilteredLogger(logger) do log haskey(log.kwargs, :maxlog) || return true if !haskey(counts, log.id) || (counts[log.id] < log.kwargs[:maxlog]) From c19e7581b80b3f3ed44245c81cb006423bf493a8 Mon Sep 17 00:00:00 2001 From: Eric Hanson <5846501+ericphanson@users.noreply.github.com> Date: Fri, 17 Dec 2021 15:11:41 -0500 Subject: [PATCH 3/3] Update README.md Co-authored-by: Fredrik Ekre --- README.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 986b693..9824a6a 100644 --- a/README.md +++ b/README.md @@ -146,10 +146,12 @@ similar to the `make_throttled_logger` example below, function make_maxlog_logger(logger) counts = Dict{Any,Int}() return ActiveFilteredLogger(logger) do log - haskey(log.kwargs, :maxlog) || return true - if !haskey(counts, log.id) || (counts[log.id] < log.kwargs[:maxlog]) - # then we will log it, and update the corresponding count - counts[log.id] = get(counts, log.id, 0) + 1 + maxlog = get(log.kwargs, :maxlog, nothing) + maxlog === nothing && return true # no limit + c = get(counts, log.id, 0) + if c < maxlog + # log this message and update the count + counts[log.id] = c + 1 return true else return false