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 Interval() int64
HookBeforArchive() func(*StarLogger, string, string, os.FileInfo) error //archivePath;currentPath HookBeforArchive() func(*StarLogger, string, string, os.FileInfo) error //archivePath;currentPath
HookAfterArchive() 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 { type logfileinfo struct {
@ -143,10 +144,18 @@ func StartArchive(logger *StarLogger, arch Archive) error {
if err != nil { if err != nil {
continue continue
} }
if arch.DoArchive() == nil {
err = os.Rename(fullpath, archiveLogPath) err = os.Rename(fullpath, archiveLogPath)
if err != nil { if err != nil {
continue 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 { if err := SetLogFile(newLogPath, logger, false); err != nil {
logger.Errorf("error occur while executing coverting new log file,detail is %v\n", err) logger.Errorf("error occur while executing coverting new log file,detail is %v\n", err)
continue continue
@ -210,7 +219,8 @@ func (abd *ArchiveByDate) NextLogFilePath(l *StarLogger, oldpath string, info os
var newName string var newName string
dir := filepath.Dir(oldpath) dir := filepath.Dir(oldpath)
if !abd.changeArchiveName { 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 { } else {
newName = abd.baseFileStyle newName = abd.baseFileStyle
} }
@ -221,7 +231,8 @@ func (abd *ArchiveByDate) ArchiveLogFilePath(l *StarLogger, oldpath string, info
var newName string var newName string
dir := filepath.Dir(oldpath) dir := filepath.Dir(oldpath)
if abd.changeArchiveName { 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 { } else {
newName = abd.baseFileStyle 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) { func (abd *ArchiveByDate) SetHookBeforArchive(f func(*StarLogger, string, string, os.FileInfo) error) {
abd.hookBefore = f abd.hookBefore = f
} }
@ -288,7 +303,8 @@ func (abd *ArchiveBySize) NextLogFilePath(l *StarLogger, oldpath string, info os
var newName string var newName string
dir := filepath.Dir(oldpath) dir := filepath.Dir(oldpath)
if !abd.changeArchiveName { 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 { } else {
newName = abd.baseFileStyle newName = abd.baseFileStyle
} }
@ -299,7 +315,8 @@ func (abd *ArchiveBySize) ArchiveLogFilePath(l *StarLogger, oldpath string, info
var newName string var newName string
dir := filepath.Dir(oldpath) dir := filepath.Dir(oldpath)
if abd.changeArchiveName { 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 { } else {
newName = abd.baseFileStyle newName = abd.baseFileStyle
} }
@ -326,6 +343,10 @@ func (abd *ArchiveBySize) SetHookAfterArchive(f func(*StarLogger, string, string
abd.hookAfter = f 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 { 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{ return &ArchiveBySize{
size: size, size: size,
@ -368,7 +389,8 @@ func (abd *ArchiveByDateSize) NextLogFilePath(l *StarLogger, oldpath string, inf
var newName string var newName string
dir := filepath.Dir(oldpath) dir := filepath.Dir(oldpath)
if !abd.changeArchiveName { 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 { } else {
newName = abd.baseFileStyle newName = abd.baseFileStyle
} }
@ -379,7 +401,8 @@ func (abd *ArchiveByDateSize) ArchiveLogFilePath(l *StarLogger, oldpath string,
var newName string var newName string
dir := filepath.Dir(oldpath) dir := filepath.Dir(oldpath)
if abd.changeArchiveName { 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 { } else {
newName = abd.baseFileStyle newName = abd.baseFileStyle
} }
@ -412,6 +435,10 @@ func (abd *ArchiveByDateSize) SetHookAfterArchive(f func(*StarLogger, string, st
abd.hookAfter = f 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 { 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{ return &ArchiveByDateSize{
size: size, size: size,

View File

@ -8,7 +8,7 @@ import (
func TestArchiveByDate(t *testing.T) { func TestArchiveByDate(t *testing.T) {
l := Std l := Std
SetLogFile("test.log", l, true) 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)) "_2006_01_02_15_04_05.log", true, nil, nil))
for { for {
time.Sleep(time.Second) 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{}) { func (logger *starlog) Write(str ...interface{}) {
strs := fmt.Sprint(str...) strs := fmt.Sprint(str...)
logger.Write(strs) logger.write(strs)
} }
func (logger *starlog) Writef(format string, str ...interface{}) { func (logger *starlog) Writef(format string, str ...interface{}) {