Skip to content

Commit

Permalink
improve impelements for logging file rotation in windows (#3080)
Browse files Browse the repository at this point in the history
  • Loading branch information
gqcn authored Oct 31, 2023
1 parent b6935a7 commit e42aa64
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 18 deletions.
12 changes: 6 additions & 6 deletions os/glog/glog_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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,
Expand All @@ -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,
Expand Down
13 changes: 1 addition & 12 deletions os/glog/glog_logger_rotate.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package glog
import (
"context"
"fmt"
"runtime"
"strings"
"time"

Expand Down Expand Up @@ -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)
}
}()
Expand Down

0 comments on commit e42aa64

Please sign in to comment.