Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add maxlog example #59

Merged
merged 3 commits into from
Dec 17, 2021
Merged

Add maxlog example #59

merged 3 commits into from
Dec 17, 2021

Conversation

ericphanson
Copy link
Member

I found it useful, and am using this code in https://github.com/beacon-biosignals/BeaconK8sUtilities.jl/pull/14

I found it useful, and am using this code in beacon-biosignals/BeaconK8sUtilities.jl#14
README.md Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
ericphanson and others added 2 commits December 17, 2021 15:11
Co-authored-by: Fredrik Ekre <ekrefredrik@gmail.com>
Co-authored-by: Fredrik Ekre <ekrefredrik@gmail.com>
@fredrikekre fredrikekre merged commit 78df79e into JuliaLogging:master Dec 17, 2021
@ericphanson ericphanson deleted the patch-1 branch December 17, 2021 21:01
@ericphanson
Copy link
Member Author

ericphanson commented Jan 3, 2022

Ah, the way Base does this is it keeps a dictionary of counts of remaining times to emit the log, instead of count of logs emitted. Then in shouldlog it can just check that, so it only needs the id and not maxlog. Then later in handle_message it populates the dict with maxlog info. (Example: JuliaLang/julia#43641). So they are basically doing it as an EarlyFilteredLogger instead of ActiveFilteredLogger IIUC, in terms of when the filtering happens.

However, I don't really know how to do that in the LoggingExtras framework. It seems we'd need to share state between an EarlyFilteredLogger and a TransformerLogger, or something like that.

edit: ah, but that can probably just be done with a closure!

@ericphanson
Copy link
Member Author

ericphanson commented Jan 3, 2022

I think

function make_maxlog_logger(logger)
    counts_remaining = Dict{Any,Int}()
    A = ActiveFilteredLogger(logger) do log
        maxlog = get(log.kwargs, :maxlog, nothing)
        if maxlog isa Integer
            remaining = get!(counts_remaining, log.id, Int(maxlog)::Int)
            counts_remaining[log.id] = remaining-1
            return remaining > 0
        else
            return true
        end
    end
   return EarlyFilteredLogger(A) do log
        return get(counts_remaining, log.id, 1) > 0
    end
end

is basically what Base is doing. Should I update the example? I'm not totally sure how much of a difference it makes; we can early exit from maxlogs at the cost of doing an extra layer of loggers to check.

@fredrikekre
Copy link
Member

That definitely isn't as clean as the example we merged, which is a shame.

@fredrikekre
Copy link
Member

Maybe both options can be added to https://julialogging.github.io/ in a "how to throttle messages" section or something like that, and then the complicated example can be included too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants