timberjack icon indicating copy to clipboard operation
timberjack copied to clipboard

[FEATURE] Support initializing l.lastRotationTime from the last file modification time

Open qiulaidongfeng opened this issue 1 year ago • 1 comments

💡 Feature request

Describe the problem
I have multiple websites of my own, and because I am deployed on a single core VPS, I do not want to use nginx as a reverse proxy (which would definitely mean context switching), so I run them in the same Go process, distribute requests to different handlers through SNIs, and use TimberJack to record logs during this process.

I often add new features and restart the web process, so that l.lastRotationTime becomes now. Repeating this a few times may result in actually meeting RotationInterval without rotation logs.


Proposed solution / API
Add field LastRotationTimeToModTime

change Write method

	// Open (or create) the file on first write.
	if l.file == nil {
		if err = l.openExistingOrNew(len(p)); err != nil {
			return 0, err
		}
		if l.lastRotationTime.IsZero() {
			// Initialize to 'now' so interval/minute checks start from here.
			l.lastRotationTime = now
		}
	}

to

	// Open (or create) the file on first write.
	if l.file == nil {
		if err = l.openExistingOrNew(len(p)); err != nil {
			return 0, err
		}
if l.LastRotationTimeToModTime{
l.lastRotationTime=l.file.Stat().ModTime()//Pseudo code
}
		if l.lastRotationTime.IsZero() {
			// Initialize to 'now' so interval/minute checks start from here.
			l.lastRotationTime = now
		}
	}
// Example
tj := &timberjack.Logger{
    Filename:    "app.log",
    LastRotationTimeToModTime:true,
    RotateEvery: time.Hour, // new option for fixed-interval rotation
}

Benefits Solved my above use case

qiulaidongfeng avatar Dec 03 '25 10:12 qiulaidongfeng

Withdraw, I think RotateWithReason+User-defined next Time with time.Ticker can meet my requirements

qiulaidongfeng avatar Dec 03 '25 11:12 qiulaidongfeng