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

The outdated log file is not deleted when the rorate is on #14

Closed
jsfaint opened this issue Dec 16, 2019 · 4 comments
Closed

The outdated log file is not deleted when the rorate is on #14

jsfaint opened this issue Dec 16, 2019 · 4 comments

Comments

@jsfaint
Copy link
Contributor

jsfaint commented Dec 16, 2019

I use clog as logging library in my project.
I meet an weird issue that the outdated log file is not deleted when rotate is on.

My go version is

$ go version
go version go1.13.5 linux/amd64

clog was initialled with the code below:

func New(c Config) (err error) {
	name := filepath.Join(c.Path, c.LogName)

	if c.IsConsole {
		_ = clog.NewConsole()
	}

	return clog.NewFile(clog.FileConfig{
		Level:    c.logLevel(),
		Filename: name,
		FileRotationConfig: clog.FileRotationConfig{
			Rotate:  true,
			Daily:   true,
			MaxSize: c.MaxSize,
			MaxDays: c.MaxDays,
		},
	})
}

The daemon is running since last Thursday.
The MaxDays is set to 1, but the old log is still there.

$ ls -1
daemon.log
daemon.log.2019-12-12
daemon.log.2019-12-12.001
daemon.log.2019-12-12.002
daemon.log.2019-12-13
daemon.log.2019-12-13.001
daemon.log.2019-12-13.002
daemon.log.2019-12-14
daemon.log.2019-12-14.001
daemon.log.2019-12-15
daemon.log.2019-12-15.001

Did I make anything wrong or miss anything ?
Thanks

@jsfaint
Copy link
Contributor Author

jsfaint commented Dec 16, 2019

Oh ,forget the clog version.
I tried clog v1.2.0 and v2.0.0, the behavior is same.

I once got it work with v2.0.0 at 2019/11/26.
But I can't find that version of clog anymore.

@unknwon
Copy link
Member

unknwon commented Dec 16, 2019

Thank you for filing this issue! I can confirm the bug is that the "delete" logic is only triggered once at start.

@unknwon unknwon added the bug label Dec 16, 2019
@jsfaint
Copy link
Contributor Author

jsfaint commented Dec 16, 2019

Thanks for your reply.

Currently, I wrote a workaround for it. But maybe it's better to handle it in the clog library.

go func() {
	for {
		select {
		case <-ctx.Done():
			return
		case <-time.After(time.Hour * 24):
			if err := log.NewFile(conf); err != nil {
				fmt.Fprintln(os.Stderr, err)
				os.Exit(1)
			}
		}
	}
}()

@jsfaint
Copy link
Contributor Author

jsfaint commented Dec 18, 2019

Can we call deleteOutdatedFiles() in here?
https://github.com/go-clog/clog/blob/master/file.go#L189

if needsRotate {
	_ = l.file.Close()
	if err := os.Rename(l.filename, rotateFilename(l.filename, rotateDate.Format(simpleDateFormat))); err != nil {
		return bytesWrote, fmt.Errorf("rename rotated file %q: %v", l.filename, err)
	}

	if err := l.initFile(); err != nil {
		return bytesWrote, fmt.Errorf("init file %q: %v", l.filename, err)
	}

	l.openDay = now.Day()
	l.currentSize = 0
	l.currentLines = 0

	//delete outdated file in here
	if err := l.deleteOutdatedFiles(); err != nil {
		return bytesWrote, fmt.Errorf("delete outdated file: %v", err)
	}
}

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

No branches or pull requests

2 participants