tracing icon indicating copy to clipboard operation
tracing copied to clipboard

new feature: keep only the last N log files in rotation

Open ozgunozerk opened this issue 3 years ago • 7 comments

Motivation

tracing-appender does not have Rotation based on size yet. Also, it doesn't have the feature of keeping the most recent N log files

I believe the second feature is more easy to implement, and also will partially solve the Rotation based on size problem. Because people may choose hourly or daily rotation based on their needs, and put an extra boundary of keep the last 5 files for example. Of course it won't handle all the edge cases for Rotation based on size. But it will cover most of the scenarios. And also, it is a good feature to have on its own :)

Solution

Introduce another field called keep_last: Option<usize> to the Inner of RollingFileAppender struct. I managed to did not touch any of the existing functions, so it WON'T BE A BREAKING CHANGE. Yay :)

The solution is, whenever the rotation should happen, the refresh_writer() is called. So I embed the following logic into that function:

1- check the log folder and detect the log files 2- if there are more log files than the last_keep amount 3- store the filenames in a vector, and sort them by their dates (dates are already present in the filename) 4- keep deleting the oldest ones, till we have desired amount of log files in the log folder

ozgunozerk avatar Jun 17 '22 17:06 ozgunozerk

Is related to #858 and #1940

ozgunozerk avatar Jun 17 '22 17:06 ozgunozerk

There is one test failing :/

ozgunozerk avatar Jun 22 '22 10:06 ozgunozerk

i want to make a few additional notes, but since i'm packing for my move this weekend, it might be a day or two. sorry!

davidbarsky avatar Jun 23 '22 18:06 davidbarsky

Any update on this?

ozgunozerk avatar Jul 01 '22 11:07 ozgunozerk

I'm willing to make further updates if necessary :)

ozgunozerk avatar Jul 18 '22 09:07 ozgunozerk

Sorry, it's been a busy month. Do you mind if I make commits directly to your branch/fork for the changes that I think we need? Since I believe this approach works for you, I'll also open a branch that targets the master branch—it'll make the subsequent branch maintenance on our side easier.

davidbarsky avatar Jul 18 '22 15:07 davidbarsky

Of course, no worries!

ozgunozerk avatar Jul 18 '22 16:07 ozgunozerk

Any update on this? :)

ozgunozerk avatar Aug 11 '22 07:08 ozgunozerk

What happened to this PR? GitHub says it's merged, but it's not as far as I can tell

image

CBenoit avatar Aug 22 '22 15:08 CBenoit

Because unfortunately there were no responses from the tokio team, I decided to use the fork for my project, and thus synced the fork. Weird that it says merged. Let me re-open the PR, maybe we can get some attention :)

ozgunozerk avatar Aug 22 '22 15:08 ozgunozerk

New one is here: #2284

ozgunozerk avatar Aug 22 '22 15:08 ozgunozerk