Skip to content

Conversation

@fredrikekre
Copy link
Member

This allows passing a callback that is called with the previous file whenever the logger rotates the backing file. Example callback to compress the logfile:

callback(nt) = run(`gzip $(nt.file)`)

@fredrikekre
Copy link
Member Author

fredrikekre commented Jan 30, 2021

On second thought,

function callback(file::String)
    # do stuff
end

is probably enough. The directory you can get from dirname(file) and if you need the datformat for some more elaborate handling you can just use that from the "outside" since the general pattern would be something like

using Logging, LoggingExtras

const pattern = raw"yyyy-mm-dd.\l\o\g"

function callback(file)
    # do something with `file` and global `pattern`
end

logger = DatetimeFileRotatingLogger(dir, pattern; callback=callback)

@fredrikekre fredrikekre changed the title [WIP] Implement a callback function for DatetimeRotatingFileLogger. Implement a callback function for DatetimeRotatingFileLogger. Jan 30, 2021
Copy link
Member

@oxinabox oxinabox left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice.
this is useful and make a lot of sense as a thing to have.

Some fairly minor comments. but basically looks good.

@fredrikekre fredrikekre force-pushed the fe/callback branch 3 times, most recently from ebe5a1f to 0b378cc Compare February 8, 2021 17:59
@oxinabox
Copy link
Member

oxinabox commented Feb 8, 2021

Hmm why is CI failing here, saying it was only rotated once?
https://github.com/oxinabox/LoggingExtras.jl/pull/42/checks#step:6:41

@fredrikekre
Copy link
Member Author

Yea I was just discussing this with @staticfloat and basically allowing to rotate for so small time-scales is not very robust. Our idea (which is implemented in the last commit) is to just error for sub-minute resolution. I think this is fine since I believe in real life one wouldn't use this for sub-day resolution, possibly hour-level. Thoughts?

@oxinabox
Copy link
Member

oxinabox commented Feb 8, 2021

Yeah could do.
For testing purposes now should we increase the sleep?

Or use Mocking.jl / SimpleMock.jl to mock the call to now()?

@fredrikekre
Copy link
Member Author

Yea the sleeping now should have ~5 seconds of margin

@oxinabox oxinabox merged commit 4143a7d into JuliaLogging:master Feb 8, 2021
@fredrikekre fredrikekre deleted the fe/callback branch February 8, 2021 20:59
@fredrikekre
Copy link
Member Author

Thanks

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