mirror of
https://github.com/gogf/gf.git
synced 2025-04-05 03:05:05 +08:00
fix glog bug (#1844)
This commit is contained in:
parent
3b9e5c71bf
commit
141f3512a9
@ -39,6 +39,23 @@ func Open(path string, flag int, perm os.FileMode, ttl ...time.Duration) (file *
|
||||
return pool.File()
|
||||
}
|
||||
|
||||
// Get returns a file item with given file path, flag and opening permission.
|
||||
// It retrieves a file item from the file pointer pool after then.
|
||||
func Get(path string, flag int, perm os.FileMode, ttl ...time.Duration) (file *File) {
|
||||
var fpTTL time.Duration
|
||||
if len(ttl) > 0 {
|
||||
fpTTL = ttl[0]
|
||||
}
|
||||
|
||||
f, found := pools.Search(fmt.Sprintf("%s&%d&%d&%d", path, flag, fpTTL, perm))
|
||||
if !found {
|
||||
return nil
|
||||
}
|
||||
|
||||
fp, _ := f.(*Pool).pool.Get()
|
||||
return fp.(*File)
|
||||
}
|
||||
|
||||
// Stat returns the FileInfo structure describing file.
|
||||
func (f *File) Stat() (os.FileInfo, error) {
|
||||
if f.stat == nil {
|
||||
@ -48,7 +65,11 @@ func (f *File) Stat() (os.FileInfo, error) {
|
||||
}
|
||||
|
||||
// Close puts the file pointer back to the file pointer pool.
|
||||
func (f *File) Close() error {
|
||||
func (f *File) Close(close ...bool) error {
|
||||
if len(close) > 0 && close[0] {
|
||||
f.File.Close()
|
||||
}
|
||||
|
||||
if f.pid == f.pool.id.Val() {
|
||||
return f.pool.pool.Put(f)
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@ -309,10 +310,27 @@ func (l *Logger) printToFile(ctx context.Context, t time.Time, in *HandlerInput)
|
||||
defer gmlock.Unlock(memoryLockKey)
|
||||
|
||||
// Rotation file size checks.
|
||||
if l.config.RotateSize > 0 {
|
||||
if gfile.Size(logFilePath) > l.config.RotateSize {
|
||||
if l.config.RotateSize > 0 && gfile.Size(logFilePath) > l.config.RotateSize {
|
||||
if runtime.GOOS == "windows" {
|
||||
file := l.getFilePointer(ctx, logFilePath)
|
||||
if file == nil {
|
||||
intlog.Errorf(ctx, `got nil file pointer for: %s`, logFilePath)
|
||||
return buffer
|
||||
}
|
||||
|
||||
if _, err := file.Write(buffer.Bytes()); err != nil {
|
||||
intlog.Errorf(ctx, `%+v`, err)
|
||||
}
|
||||
|
||||
if err := file.Close(true); err != nil {
|
||||
intlog.Errorf(ctx, `%+v`, err)
|
||||
}
|
||||
l.rotateFileBySize(ctx, t)
|
||||
|
||||
return buffer
|
||||
}
|
||||
|
||||
l.rotateFileBySize(ctx, t)
|
||||
}
|
||||
// Logging content outputting to disk file.
|
||||
if file := l.getFilePointer(ctx, logFilePath); file == nil {
|
||||
@ -343,6 +361,21 @@ func (l *Logger) getFilePointer(ctx context.Context, path string) *gfpool.File {
|
||||
return file
|
||||
}
|
||||
|
||||
// getFilePointer retrieves and returns a file pointer from file pool.
|
||||
func (l *Logger) getOpenedFilePointer(ctx context.Context, path string) *gfpool.File {
|
||||
|
||||
file := gfpool.Get(
|
||||
path,
|
||||
defaultFileFlags,
|
||||
defaultFilePerm,
|
||||
defaultFileExpire,
|
||||
)
|
||||
if file == nil {
|
||||
intlog.Errorf(ctx, `can not find the file, path:%s`, path)
|
||||
}
|
||||
return file
|
||||
}
|
||||
|
||||
// printStd prints content `s` without stack.
|
||||
func (l *Logger) printStd(ctx context.Context, level int, value ...interface{}) {
|
||||
l.print(ctx, level, "", value...)
|
||||
|
@ -9,6 +9,7 @@ package glog
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"runtime"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/v2/container/garray"
|
||||
@ -152,15 +153,37 @@ func (l *Logger) rotateChecksTimely(ctx context.Context) {
|
||||
mtime = gfile.MTime(file)
|
||||
subDuration = now.Sub(mtime)
|
||||
if subDuration > l.config.RotateExpire {
|
||||
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 {
|
||||
intlog.Errorf(ctx, `%+v`, err)
|
||||
}
|
||||
func() {
|
||||
memoryLockFileKey := memoryLockPrefixForPrintingToFile + file
|
||||
if !gmlock.TryLock(memoryLockFileKey) {
|
||||
return
|
||||
}
|
||||
defer gmlock.Unlock(memoryLockFileKey)
|
||||
|
||||
fp := l.getOpenedFilePointer(ctx, file)
|
||||
if fp == nil {
|
||||
intlog.Errorf(ctx, `got nil file pointer for: %s`, file)
|
||||
return
|
||||
}
|
||||
|
||||
if runtime.GOOS == "windows" {
|
||||
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 {
|
||||
intlog.Errorf(ctx, `%+v`, err)
|
||||
}
|
||||
|
||||
}()
|
||||
|
||||
}
|
||||
}
|
||||
if expireRotated {
|
||||
|
@ -8,7 +8,6 @@ package glog_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@ -42,9 +41,17 @@ func Test_Rotate_Size(t *testing.T) {
|
||||
defer gfile.Remove(p)
|
||||
|
||||
s := "1234567890abcdefg"
|
||||
for i := 0; i < 10; i++ {
|
||||
fmt.Println(ctx, "logging content index:", i)
|
||||
for i := 0; i < 8; i++ {
|
||||
l.Print(ctx, s)
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
|
||||
logFiles, err := gfile.ScanDirFile(p, "access*")
|
||||
t.AssertNil(err)
|
||||
|
||||
for _, v := range logFiles {
|
||||
content := gfile.GetContents(v)
|
||||
t.AssertIN(gstr.Count(content, s), []int{1, 2})
|
||||
}
|
||||
|
||||
time.Sleep(time.Second * 3)
|
||||
@ -53,9 +60,6 @@ func Test_Rotate_Size(t *testing.T) {
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(files), 2)
|
||||
|
||||
content := gfile.GetContents(gfile.Join(p, "access.log"))
|
||||
t.Assert(gstr.Count(content, s), 1)
|
||||
|
||||
time.Sleep(time.Second * 5)
|
||||
files, err = gfile.ScanDirFile(p, "*.gz")
|
||||
t.AssertNil(err)
|
||||
@ -93,6 +97,8 @@ func Test_Rotate_Expire(t *testing.T) {
|
||||
|
||||
time.Sleep(time.Second * 3)
|
||||
|
||||
filenames, err := gfile.ScanDirFile(p, "*")
|
||||
t.Log(filenames, err)
|
||||
files, err = gfile.ScanDirFile(p, "*.gz")
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(files), 1)
|
||||
|
Loading…
x
Reference in New Issue
Block a user