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

Fix: logx with Compress auto delete old logs #3329

Merged
merged 1 commit into from
Jun 8, 2023

Conversation

ren544735689
Copy link
Contributor

Under the Windows operating system, if the Compress field of the logx component is set to true, the original uncompressed logs will not be automatically deleted.

This condition could NOT happen under Linux/Centos system.

Description:
logx has a function "gzipFile" in "go-zero/core/logx/rotatelogger.go", this function use "defer in.Close()" to close "in". But before that, "os.Remove(file)" want to delete this file, so it throws an exception "compress error: remove logs/error.log-2023-06-08:The process cannot access the file because it is being used by another process"

Solution:
I change "defer in.Close()" to "in.Close()" and put this code before "return os.Remove(file)" will fix this bug.

The code is:

func gzipFile(file string) error {
	in, err := os.Open(file)
	if err != nil {
		return err
	}
	//defer in.Close()

	out, err := os.Create(fmt.Sprintf("%s%s", file, gzipExt))
	if err != nil {
		return err
	}
	defer out.Close()

	w := gzip.NewWriter(out)
	if _, err = io.Copy(w, in); err != nil {
		return err
	} else if err = w.Close(); err != nil {
		return err
	}

        in.Close()
	return os.Remove(file)
}

OS: windows 10
go-zero version 1.4.0

@codecov
Copy link

codecov bot commented Jun 8, 2023

Codecov Report

Merging #3329 (111132a) into master (fd84b27) will increase coverage by 0.01%.
The diff coverage is 100.00%.

Impacted Files Coverage Δ
core/logx/rotatelogger.go 84.13% <100.00%> (-0.37%) ⬇️

... and 1 file with indirect coverage changes

@kevwan kevwan changed the title Fix:logx with Compress auto delete old logs Fix: logx with Compress auto delete old logs Jun 8, 2023
@kevwan kevwan added this pull request to the merge queue Jun 8, 2023
Merged via the queue into zeromicro:master with commit da81d8f Jun 8, 2023
7 checks passed
wuyazi pushed a commit to wuyazi/go-zero that referenced this pull request Jun 14, 2023
Co-authored-by: haoran.ren <haoran.ren@mihoyo.com>
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.

None yet

2 participants