Skip to content

Add maxlog example - #59

Merged
fredrikekre merged 3 commits into
JuliaLogging:masterfrom
ericphanson:patch-1
Dec 17, 2021
Merged

Add maxlog example#59
fredrikekre merged 3 commits into
JuliaLogging:masterfrom
ericphanson:patch-1

Conversation

@ericphanson

Copy link
Copy Markdown
Member

I found it useful, and am using this code in beacon-biosignals/BeaconK8sUtilities.jl#14
Comment thread README.md
Comment thread README.md Outdated
Comment thread README.md Outdated
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

ericphanson commented Jan 3, 2022

Copy link
Copy Markdown
Member Author

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

ericphanson commented Jan 3, 2022

Copy link
Copy Markdown
Member Author

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
Copy Markdown
Member

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

@fredrikekre

Copy link
Copy Markdown
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