improve impelements for logging file rotation in windows (#3080)

This commit is contained in:
John Guo 2023-10-31 20:19:01 +08:00 committed by GitHub
parent b6935a7285
commit e42aa64d75
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 18 deletions

View File

@ -290,7 +290,7 @@ func (l *Logger) printToFile(ctx context.Context, t time.Time, in *HandlerInput)
// Rotation file size checks.
if l.config.RotateSize > 0 && gfile.Size(logFilePath) > l.config.RotateSize {
if runtime.GOOS == "windows" {
file := l.getFilePointer(ctx, logFilePath)
file := l.createFpInPool(ctx, logFilePath)
if file == nil {
intlog.Errorf(ctx, `got nil file pointer for: %s`, logFilePath)
return buffer
@ -311,7 +311,7 @@ func (l *Logger) printToFile(ctx context.Context, t time.Time, in *HandlerInput)
l.rotateFileBySize(ctx, t)
}
// Logging content outputting to disk file.
if file := l.getFilePointer(ctx, logFilePath); file == nil {
if file := l.createFpInPool(ctx, logFilePath); file == nil {
intlog.Errorf(ctx, `got nil file pointer for: %s`, logFilePath)
} else {
if _, err := file.Write(buffer.Bytes()); err != nil {
@ -324,8 +324,8 @@ func (l *Logger) printToFile(ctx context.Context, t time.Time, in *HandlerInput)
return buffer
}
// getFilePointer retrieves and returns a file pointer from file pool.
func (l *Logger) getFilePointer(ctx context.Context, path string) *gfpool.File {
// createFpInPool retrieves and returns a file pointer from file pool.
func (l *Logger) createFpInPool(ctx context.Context, path string) *gfpool.File {
file, err := gfpool.Open(
path,
defaultFileFlags,
@ -339,8 +339,8 @@ func (l *Logger) getFilePointer(ctx context.Context, path string) *gfpool.File {
return file
}
// getOpenedFilePointer retrieves and returns a file pointer from file pool.
func (l *Logger) getOpenedFilePointer(ctx context.Context, path string) *gfpool.File {
// getFpFromPool retrieves and returns a file pointer from file pool.
func (l *Logger) getFpFromPool(ctx context.Context, path string) *gfpool.File {
file := gfpool.Get(
path,
defaultFileFlags,

View File

@ -9,7 +9,6 @@ package glog
import (
"context"
"fmt"
"runtime"
"strings"
"time"
@ -170,23 +169,13 @@ func (l *Logger) rotateChecksTimely(ctx context.Context) {
return
}
defer gmlock.Unlock(memoryLockFileKey)
if runtime.GOOS == "windows" {
fp := l.getOpenedFilePointer(ctx, file)
if fp == nil {
intlog.Errorf(ctx, `got nil file pointer for: %s`, file)
return
}
if err := fp.Close(true); err != nil {
intlog.Errorf(ctx, `%+v`, err)
}
}
expireRotated = true
intlog.Printf(
ctx,
`%v - %v = %v > %v, rotation expire logging file: %s`,
now, mtime, subDuration, l.config.RotateExpire, file,
)
if err := l.doRotateFile(ctx, file); err != nil {
if err = l.doRotateFile(ctx, file); err != nil {
intlog.Errorf(ctx, `%+v`, err)
}
}()