This commit is contained in:
兔子 2025-04-28 13:16:36 +08:00
parent 121819ef13
commit 51608601cf
3 changed files with 38 additions and 11 deletions

View File

@ -20,6 +20,7 @@ type Archive interface {
Interval() int64
HookBeforArchive() func(*StarLogger, string, string, os.FileInfo) error //archivePath;currentPath
HookAfterArchive() func(*StarLogger, string, string, os.FileInfo) error //archivePath;currentPath
DoArchive() func(*StarLogger, string, string, os.FileInfo) error
}
type logfileinfo struct {
@ -143,9 +144,17 @@ func StartArchive(logger *StarLogger, arch Archive) error {
if err != nil {
continue
}
err = os.Rename(fullpath, archiveLogPath)
if err != nil {
continue
if arch.DoArchive() == nil {
err = os.Rename(fullpath, archiveLogPath)
if err != nil {
continue
}
} else {
err = arch.DoArchive()(logger, fullpath, archiveLogPath, fileinfo)
if err != nil {
logger.Errorf("error occur while executing archive log file,detail is %v\n", err)
continue
}
}
if err := SetLogFile(newLogPath, logger, false); err != nil {
logger.Errorf("error occur while executing coverting new log file,detail is %v\n", err)
@ -210,7 +219,8 @@ func (abd *ArchiveByDate) NextLogFilePath(l *StarLogger, oldpath string, info os
var newName string
dir := filepath.Dir(oldpath)
if !abd.changeArchiveName {
newName = abd.baseFileStyle + time.Now().Format(abd.archiveStyle)
base := filepath.Base(abd.baseFileStyle)
newName = base[:len(base)-len(filepath.Ext(base))] + time.Now().Format(abd.archiveStyle)
} else {
newName = abd.baseFileStyle
}
@ -221,7 +231,8 @@ func (abd *ArchiveByDate) ArchiveLogFilePath(l *StarLogger, oldpath string, info
var newName string
dir := filepath.Dir(oldpath)
if abd.changeArchiveName {
newName = filepath.Base(abd.baseFileStyle) + time.Now().Format(abd.archiveStyle)
base := filepath.Base(abd.baseFileStyle)
newName = base[:len(base)-len(filepath.Ext(base))] + time.Now().Format(abd.archiveStyle)
} else {
newName = abd.baseFileStyle
}
@ -247,6 +258,10 @@ func (abd *ArchiveByDate) HookAfterArchive() func(*StarLogger, string, string, o
}
}
func (abd *ArchiveByDate) DoArchive() func(*StarLogger, string, string, os.FileInfo) error {
return nil
}
func (abd *ArchiveByDate) SetHookBeforArchive(f func(*StarLogger, string, string, os.FileInfo) error) {
abd.hookBefore = f
}
@ -288,7 +303,8 @@ func (abd *ArchiveBySize) NextLogFilePath(l *StarLogger, oldpath string, info os
var newName string
dir := filepath.Dir(oldpath)
if !abd.changeArchiveName {
newName = abd.baseFileStyle + time.Now().Format(abd.archiveStyle)
base := filepath.Base(abd.baseFileStyle)
newName = base[:len(base)-len(filepath.Ext(base))] + time.Now().Format(abd.archiveStyle)
} else {
newName = abd.baseFileStyle
}
@ -299,7 +315,8 @@ func (abd *ArchiveBySize) ArchiveLogFilePath(l *StarLogger, oldpath string, info
var newName string
dir := filepath.Dir(oldpath)
if abd.changeArchiveName {
newName = filepath.Base(abd.baseFileStyle) + time.Now().Format(abd.archiveStyle)
base := filepath.Base(abd.baseFileStyle)
newName = base[:len(base)-len(filepath.Ext(base))] + time.Now().Format(abd.archiveStyle)
} else {
newName = abd.baseFileStyle
}
@ -326,6 +343,10 @@ func (abd *ArchiveBySize) SetHookAfterArchive(f func(*StarLogger, string, string
abd.hookAfter = f
}
func (abd *ArchiveBySize) DoArchive() func(*StarLogger, string, string, os.FileInfo) error {
return nil
}
func NewArchiveBySize(size int64, checkInterval int64, baseFileStyle, archiveFileStyle string, changeArchiveFileName bool, hookbefore func(*StarLogger, string, string, os.FileInfo) error, hookafter func(*StarLogger, string, string, os.FileInfo) error) *ArchiveBySize {
return &ArchiveBySize{
size: size,
@ -368,7 +389,8 @@ func (abd *ArchiveByDateSize) NextLogFilePath(l *StarLogger, oldpath string, inf
var newName string
dir := filepath.Dir(oldpath)
if !abd.changeArchiveName {
newName = abd.baseFileStyle + time.Now().Format(abd.archiveStyle)
base := filepath.Base(abd.baseFileStyle)
newName = base[:len(base)-len(filepath.Ext(base))] + time.Now().Format(abd.archiveStyle)
} else {
newName = abd.baseFileStyle
}
@ -379,7 +401,8 @@ func (abd *ArchiveByDateSize) ArchiveLogFilePath(l *StarLogger, oldpath string,
var newName string
dir := filepath.Dir(oldpath)
if abd.changeArchiveName {
newName = filepath.Base(abd.baseFileStyle) + time.Now().Format(abd.archiveStyle)
base := filepath.Base(abd.baseFileStyle)
newName = base[:len(base)-len(filepath.Ext(base))] + time.Now().Format(abd.archiveStyle)
} else {
newName = abd.baseFileStyle
}
@ -412,6 +435,10 @@ func (abd *ArchiveByDateSize) SetHookAfterArchive(f func(*StarLogger, string, st
abd.hookAfter = f
}
func (abd *ArchiveByDateSize) DoArchive() func(*StarLogger, string, string, os.FileInfo) error {
return nil
}
func NewArchiveByDateSize(size int64, interval int64, checkInterval int64, baseFileStyle, archiveFileStyle string, changeArchiveFileName bool, hookbefore func(*StarLogger, string, string, os.FileInfo) error, hookafter func(*StarLogger, string, string, os.FileInfo) error) *ArchiveByDateSize {
return &ArchiveByDateSize{
size: size,

View File

@ -8,7 +8,7 @@ import (
func TestArchiveByDate(t *testing.T) {
l := Std
SetLogFile("test.log", l, true)
StartArchive(l, NewArchiveByDateSize(4096, 24, 2, "test.log",
StartArchive(l, NewArchiveByDateSize(4096, 10, 2, "test.log",
"_2006_01_02_15_04_05.log", true, nil, nil))
for {
time.Sleep(time.Second)

View File

@ -311,7 +311,7 @@ func (logger *starlog) Logln(thread string, isStd bool, isShow bool, level int,
func (logger *starlog) Write(str ...interface{}) {
strs := fmt.Sprint(str...)
logger.Write(strs)
logger.write(strs)
}
func (logger *starlog) Writef(format string, str ...interface{}) {