|
|
@ -5,24 +5,21 @@ import (
|
|
|
|
"os"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
"b612.me/staros"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"b612.me/starmap"
|
|
|
|
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
var archMap starmap.StarMapKV
|
|
|
|
var archMap starMapKV
|
|
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
func init() {
|
|
|
|
archMap = starmap.NewStarMap()
|
|
|
|
archMap = newStarMap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type Archive interface {
|
|
|
|
type Archive interface {
|
|
|
|
ShouldArchiveNow(string, os.FileInfo) bool
|
|
|
|
ShouldArchiveNow(*StarLogger, string, os.FileInfo) bool
|
|
|
|
NextLogFilePath(string, os.FileInfo) string
|
|
|
|
NextLogFilePath(*StarLogger, string, os.FileInfo) string
|
|
|
|
|
|
|
|
ArchiveLogFilePath(*StarLogger, string, os.FileInfo) string
|
|
|
|
Interval() int64
|
|
|
|
Interval() int64
|
|
|
|
HookBeforArchive() func(string, os.FileInfo) error
|
|
|
|
HookBeforArchive() func(*StarLogger, string, string, os.FileInfo) error //archivePath;currentPath
|
|
|
|
HookAfterArchive() func(string, string, os.FileInfo) error
|
|
|
|
HookAfterArchive() func(*StarLogger, string, string, os.FileInfo) error //archivePath;currentPath
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type logfileinfo struct {
|
|
|
|
type logfileinfo struct {
|
|
|
@ -41,7 +38,7 @@ func SetLogFile(path string, logger *StarLogger, appendMode bool) error {
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !appendMode && staros.Exists(fullpath) {
|
|
|
|
if !appendMode && Exists(fullpath) {
|
|
|
|
os.Remove(fullpath)
|
|
|
|
os.Remove(fullpath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fp, err := os.OpenFile(fullpath, fileMode, 0644)
|
|
|
|
fp, err := os.OpenFile(fullpath, fileMode, 0644)
|
|
|
@ -119,7 +116,7 @@ func StartArchive(logger *StarLogger, arch Archive) error {
|
|
|
|
select {
|
|
|
|
select {
|
|
|
|
case <-stopChan:
|
|
|
|
case <-stopChan:
|
|
|
|
return
|
|
|
|
return
|
|
|
|
case <-time.After(time.Second * time.Duration(arch.Interval())):
|
|
|
|
case <-time.After(time.Millisecond * time.Duration(1000*arch.Interval())):
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fileinfo, err := GetLogFileInfo(logger)
|
|
|
|
fileinfo, err := GetLogFileInfo(logger)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
@ -131,21 +128,30 @@ func StartArchive(logger *StarLogger, arch Archive) error {
|
|
|
|
continue
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fullpath := archMap.MustGet(logger.logcore.id).(logfileinfo).fullpath
|
|
|
|
fullpath := archMap.MustGet(logger.logcore.id).(logfileinfo).fullpath
|
|
|
|
if !arch.ShouldArchiveNow(fullpath, fileinfo) {
|
|
|
|
if !arch.ShouldArchiveNow(logger, fullpath, fileinfo) {
|
|
|
|
continue
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
newLogPath := arch.NextLogFilePath(fullpath, fileinfo)
|
|
|
|
newLogPath := arch.NextLogFilePath(logger, fullpath, fileinfo)
|
|
|
|
|
|
|
|
archiveLogPath := arch.ArchiveLogFilePath(logger, fullpath, fileinfo)
|
|
|
|
if arch.HookBeforArchive() != nil {
|
|
|
|
if arch.HookBeforArchive() != nil {
|
|
|
|
if err := arch.HookBeforArchive()(fullpath, fileinfo); err != nil {
|
|
|
|
if err := arch.HookBeforArchive()(logger, archiveLogPath, fullpath, fileinfo); err != nil {
|
|
|
|
logger.Errorf("error occur while executing hook before archive,detail is %v\n", err)
|
|
|
|
logger.Errorf("error occur while executing hook before archive,detail is %v\n", err)
|
|
|
|
continue
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = CloseWithSwitching(logger)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.Rename(fullpath, archiveLogPath)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
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
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
logger.Debugln("Set Log Success")
|
|
|
|
logger.Debugln("Archive Log Success")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fileinfo, err = GetLogFileInfo(logger)
|
|
|
|
fileinfo, err = GetLogFileInfo(logger)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
@ -153,7 +159,7 @@ func StartArchive(logger *StarLogger, arch Archive) error {
|
|
|
|
continue
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if arch.HookAfterArchive() != nil {
|
|
|
|
if arch.HookAfterArchive() != nil {
|
|
|
|
if err := arch.HookAfterArchive()(fullpath, newLogPath, fileinfo); err != nil {
|
|
|
|
if err := arch.HookAfterArchive()(logger, archiveLogPath, newLogPath, fileinfo); err != nil {
|
|
|
|
logger.Errorf("error occur while executing hook after archive,detail is %v\n", err)
|
|
|
|
logger.Errorf("error occur while executing hook after archive,detail is %v\n", err)
|
|
|
|
continue
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -178,23 +184,47 @@ func StopArchive(logger *StarLogger) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type ArchiveByDate struct {
|
|
|
|
type ArchiveByDate struct {
|
|
|
|
interval int64
|
|
|
|
interval int64
|
|
|
|
checkInterval int64
|
|
|
|
checkInterval int64
|
|
|
|
newFileNameStyle string
|
|
|
|
baseFileStyle string
|
|
|
|
hookBefor func(string, os.FileInfo) error
|
|
|
|
archiveStyle string
|
|
|
|
hookAfter func(string, string, os.FileInfo) error
|
|
|
|
lastSwitchTime time.Time
|
|
|
|
|
|
|
|
changeArchiveName bool
|
|
|
|
|
|
|
|
hookBefore func(*StarLogger, string, string, os.FileInfo) error
|
|
|
|
|
|
|
|
hookAfter func(*StarLogger, string, string, os.FileInfo) error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (abd *ArchiveByDate) ShouldArchiveNow(fullpath string, info os.FileInfo) bool {
|
|
|
|
func (abd *ArchiveByDate) ShouldArchiveNow(l *StarLogger, fullpath string, info os.FileInfo) bool {
|
|
|
|
if time.Now().Unix()-staros.GetFileCreationTime(info).Unix() > abd.interval {
|
|
|
|
if abd.lastSwitchTime.IsZero() {
|
|
|
|
|
|
|
|
abd.lastSwitchTime = GetFileCreationTime(info)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
sub := time.Now().Unix() - abd.lastSwitchTime.Unix()
|
|
|
|
|
|
|
|
if sub >= abd.interval || abd.interval-sub <= abd.checkInterval/2 {
|
|
|
|
return true
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (abd *ArchiveByDate) NextLogFilePath(oldpath string, info os.FileInfo) string {
|
|
|
|
func (abd *ArchiveByDate) NextLogFilePath(l *StarLogger, oldpath string, info os.FileInfo) string {
|
|
|
|
|
|
|
|
var newName string
|
|
|
|
dir := filepath.Dir(oldpath)
|
|
|
|
dir := filepath.Dir(oldpath)
|
|
|
|
newName := time.Now().Format(abd.newFileNameStyle)
|
|
|
|
if !abd.changeArchiveName {
|
|
|
|
|
|
|
|
newName = abd.baseFileStyle + time.Now().Format(abd.archiveStyle)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
newName = abd.baseFileStyle
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return filepath.Join(dir, newName)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (abd *ArchiveByDate) ArchiveLogFilePath(l *StarLogger, oldpath string, info os.FileInfo) string {
|
|
|
|
|
|
|
|
var newName string
|
|
|
|
|
|
|
|
dir := filepath.Dir(oldpath)
|
|
|
|
|
|
|
|
if abd.changeArchiveName {
|
|
|
|
|
|
|
|
newName = filepath.Base(abd.baseFileStyle) + time.Now().Format(abd.archiveStyle)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
newName = abd.baseFileStyle
|
|
|
|
|
|
|
|
}
|
|
|
|
return filepath.Join(dir, newName)
|
|
|
|
return filepath.Join(dir, newName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -202,52 +232,77 @@ func (abd *ArchiveByDate) Interval() int64 {
|
|
|
|
return abd.checkInterval
|
|
|
|
return abd.checkInterval
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (abd *ArchiveByDate) HookBeforArchive() func(string, os.FileInfo) error {
|
|
|
|
func (abd *ArchiveByDate) HookBeforArchive() func(*StarLogger, string, string, os.FileInfo) error {
|
|
|
|
|
|
|
|
|
|
|
|
return abd.hookBefor
|
|
|
|
return abd.hookBefore
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (abd *ArchiveByDate) HookAfterArchive() func(string, string, os.FileInfo) error {
|
|
|
|
func (abd *ArchiveByDate) HookAfterArchive() func(*StarLogger, string, string, os.FileInfo) error {
|
|
|
|
return abd.hookAfter
|
|
|
|
return func(logger *StarLogger, s string, s2 string, info os.FileInfo) error {
|
|
|
|
|
|
|
|
abd.lastSwitchTime = time.Now()
|
|
|
|
|
|
|
|
if abd.hookAfter != nil {
|
|
|
|
|
|
|
|
return abd.hookAfter(logger, s, s2, info)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (abd *ArchiveByDate) SetHookBeforArchive(f func(string, os.FileInfo) error) {
|
|
|
|
func (abd *ArchiveByDate) SetHookBeforArchive(f func(*StarLogger, string, string, os.FileInfo) error) {
|
|
|
|
|
|
|
|
abd.hookBefore = f
|
|
|
|
abd.hookBefor = f
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (abd *ArchiveByDate) SetHookAfterArchive(f func(string, string, os.FileInfo) error) {
|
|
|
|
func (abd *ArchiveByDate) SetHookAfterArchive(f func(*StarLogger, string, string, os.FileInfo) error) {
|
|
|
|
abd.hookAfter = f
|
|
|
|
abd.hookAfter = f
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func NewArchiveByDate(archInterval int64, checkInterval int64, fileStyle string, hookbefor func(string, os.FileInfo) error, hookafter func(string, string, os.FileInfo) error) *ArchiveByDate {
|
|
|
|
func NewArchiveByDate(archInterval int64, checkInterval int64, baseFileName string, archiveFileName string, changeArchiveName bool, hookbefore func(*StarLogger, string, string, os.FileInfo) error, hookafter func(*StarLogger, string, string, os.FileInfo) error) *ArchiveByDate {
|
|
|
|
return &ArchiveByDate{
|
|
|
|
return &ArchiveByDate{
|
|
|
|
interval: archInterval,
|
|
|
|
interval: archInterval,
|
|
|
|
checkInterval: checkInterval,
|
|
|
|
checkInterval: checkInterval,
|
|
|
|
newFileNameStyle: fileStyle,
|
|
|
|
changeArchiveName: changeArchiveName,
|
|
|
|
hookBefor: hookbefor,
|
|
|
|
baseFileStyle: baseFileName,
|
|
|
|
hookAfter: hookafter,
|
|
|
|
archiveStyle: archiveFileName,
|
|
|
|
|
|
|
|
hookBefore: hookbefore,
|
|
|
|
|
|
|
|
hookAfter: hookafter,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type ArchiveBySize struct {
|
|
|
|
type ArchiveBySize struct {
|
|
|
|
size int64
|
|
|
|
size int64
|
|
|
|
checkInterval int64
|
|
|
|
checkInterval int64
|
|
|
|
newFileNameStyle string
|
|
|
|
changeArchiveName bool
|
|
|
|
hookBefor func(string, os.FileInfo) error
|
|
|
|
baseFileStyle string
|
|
|
|
hookAfter func(string, string, os.FileInfo) error
|
|
|
|
archiveStyle string
|
|
|
|
|
|
|
|
hookBefore func(*StarLogger, string, string, os.FileInfo) error
|
|
|
|
|
|
|
|
hookAfter func(*StarLogger, string, string, os.FileInfo) error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (abd *ArchiveBySize) ShouldArchiveNow(fullpath string, info os.FileInfo) bool {
|
|
|
|
func (abd *ArchiveBySize) ShouldArchiveNow(l *StarLogger, fullpath string, info os.FileInfo) bool {
|
|
|
|
if info.Size() > abd.size {
|
|
|
|
if info.Size() > abd.size {
|
|
|
|
return true
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (abd *ArchiveBySize) NextLogFilePath(oldpath string, info os.FileInfo) string {
|
|
|
|
func (abd *ArchiveBySize) NextLogFilePath(l *StarLogger, oldpath string, info os.FileInfo) string {
|
|
|
|
|
|
|
|
var newName string
|
|
|
|
|
|
|
|
dir := filepath.Dir(oldpath)
|
|
|
|
|
|
|
|
if !abd.changeArchiveName {
|
|
|
|
|
|
|
|
newName = abd.baseFileStyle + time.Now().Format(abd.archiveStyle)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
newName = abd.baseFileStyle
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return filepath.Join(dir, newName)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (abd *ArchiveBySize) ArchiveLogFilePath(l *StarLogger, oldpath string, info os.FileInfo) string {
|
|
|
|
|
|
|
|
var newName string
|
|
|
|
dir := filepath.Dir(oldpath)
|
|
|
|
dir := filepath.Dir(oldpath)
|
|
|
|
newName := time.Now().Format(abd.newFileNameStyle)
|
|
|
|
if abd.changeArchiveName {
|
|
|
|
|
|
|
|
newName = filepath.Base(abd.baseFileStyle) + time.Now().Format(abd.archiveStyle)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
newName = abd.baseFileStyle
|
|
|
|
|
|
|
|
}
|
|
|
|
return filepath.Join(dir, newName)
|
|
|
|
return filepath.Join(dir, newName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -255,30 +310,117 @@ func (abd *ArchiveBySize) Interval() int64 {
|
|
|
|
return abd.checkInterval
|
|
|
|
return abd.checkInterval
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (abd *ArchiveBySize) HookBeforArchive() func(string, os.FileInfo) error {
|
|
|
|
func (abd *ArchiveBySize) HookBeforArchive() func(*StarLogger, string, string, os.FileInfo) error {
|
|
|
|
|
|
|
|
return abd.hookBefore
|
|
|
|
return abd.hookBefor
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (abd *ArchiveBySize) HookAfterArchive() func(string, string, os.FileInfo) error {
|
|
|
|
func (abd *ArchiveBySize) HookAfterArchive() func(*StarLogger, string, string, os.FileInfo) error {
|
|
|
|
return abd.hookAfter
|
|
|
|
return abd.hookAfter
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (abd *ArchiveBySize) SetHookBeforArchive(f func(string, os.FileInfo) error) {
|
|
|
|
func (abd *ArchiveBySize) SetHookBeforArchive(f func(*StarLogger, string, string, os.FileInfo) error) {
|
|
|
|
|
|
|
|
abd.hookBefore = f
|
|
|
|
abd.hookBefor = f
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (abd *ArchiveBySize) SetHookAfterArchive(f func(string, string, os.FileInfo) error) {
|
|
|
|
func (abd *ArchiveBySize) SetHookAfterArchive(f func(*StarLogger, string, string, os.FileInfo) error) {
|
|
|
|
abd.hookAfter = f
|
|
|
|
abd.hookAfter = f
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func NewArchiveBySize(size int64, checkInterval int64, fileStyle string, hookbefor func(string, os.FileInfo) error, hookafter func(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,
|
|
|
|
checkInterval: checkInterval,
|
|
|
|
checkInterval: checkInterval,
|
|
|
|
newFileNameStyle: fileStyle,
|
|
|
|
baseFileStyle: baseFileStyle,
|
|
|
|
hookBefor: hookbefor,
|
|
|
|
archiveStyle: archiveFileStyle,
|
|
|
|
hookAfter: hookafter,
|
|
|
|
hookBefore: hookbefore,
|
|
|
|
|
|
|
|
hookAfter: hookafter,
|
|
|
|
|
|
|
|
changeArchiveName: changeArchiveFileName,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type ArchiveByDateSize struct {
|
|
|
|
|
|
|
|
interval int64
|
|
|
|
|
|
|
|
size int64
|
|
|
|
|
|
|
|
checkInterval int64
|
|
|
|
|
|
|
|
changeArchiveName bool
|
|
|
|
|
|
|
|
lastSwitchTime time.Time
|
|
|
|
|
|
|
|
baseFileStyle string
|
|
|
|
|
|
|
|
archiveStyle string
|
|
|
|
|
|
|
|
hookBefore func(*StarLogger, string, string, os.FileInfo) error
|
|
|
|
|
|
|
|
hookAfter func(*StarLogger, string, string, os.FileInfo) error
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (abd *ArchiveByDateSize) ShouldArchiveNow(l *StarLogger, fullpath string, info os.FileInfo) bool {
|
|
|
|
|
|
|
|
if abd.lastSwitchTime.IsZero() {
|
|
|
|
|
|
|
|
abd.lastSwitchTime = GetFileCreationTime(info)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if info.Size() > abd.size {
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
sub := time.Now().Unix() - abd.lastSwitchTime.Unix()
|
|
|
|
|
|
|
|
if sub >= abd.interval || abd.interval-sub <= abd.checkInterval/2 {
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (abd *ArchiveByDateSize) NextLogFilePath(l *StarLogger, oldpath string, info os.FileInfo) string {
|
|
|
|
|
|
|
|
var newName string
|
|
|
|
|
|
|
|
dir := filepath.Dir(oldpath)
|
|
|
|
|
|
|
|
if !abd.changeArchiveName {
|
|
|
|
|
|
|
|
newName = abd.baseFileStyle + time.Now().Format(abd.archiveStyle)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
newName = abd.baseFileStyle
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return filepath.Join(dir, newName)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (abd *ArchiveByDateSize) ArchiveLogFilePath(l *StarLogger, oldpath string, info os.FileInfo) string {
|
|
|
|
|
|
|
|
var newName string
|
|
|
|
|
|
|
|
dir := filepath.Dir(oldpath)
|
|
|
|
|
|
|
|
if abd.changeArchiveName {
|
|
|
|
|
|
|
|
newName = filepath.Base(abd.baseFileStyle) + time.Now().Format(abd.archiveStyle)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
newName = abd.baseFileStyle
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return filepath.Join(dir, newName)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (abd *ArchiveByDateSize) Interval() int64 {
|
|
|
|
|
|
|
|
return abd.checkInterval
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (abd *ArchiveByDateSize) HookBeforArchive() func(*StarLogger, string, string, os.FileInfo) error {
|
|
|
|
|
|
|
|
return abd.hookBefore
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (abd *ArchiveByDateSize) HookAfterArchive() func(*StarLogger, string, string, os.FileInfo) error {
|
|
|
|
|
|
|
|
return func(logger *StarLogger, s string, s2 string, info os.FileInfo) error {
|
|
|
|
|
|
|
|
abd.lastSwitchTime = time.Now()
|
|
|
|
|
|
|
|
if abd.hookAfter != nil {
|
|
|
|
|
|
|
|
return abd.hookAfter(logger, s, s2, info)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (abd *ArchiveByDateSize) SetHookBeforArchive(f func(*StarLogger, string, string, os.FileInfo) error) {
|
|
|
|
|
|
|
|
abd.hookBefore = f
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (abd *ArchiveByDateSize) SetHookAfterArchive(f func(*StarLogger, string, string, os.FileInfo) error) {
|
|
|
|
|
|
|
|
abd.hookAfter = f
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
|
|
|
interval: interval,
|
|
|
|
|
|
|
|
checkInterval: checkInterval,
|
|
|
|
|
|
|
|
baseFileStyle: baseFileStyle,
|
|
|
|
|
|
|
|
archiveStyle: archiveFileStyle,
|
|
|
|
|
|
|
|
hookBefore: hookbefore,
|
|
|
|
|
|
|
|
hookAfter: hookafter,
|
|
|
|
|
|
|
|
changeArchiveName: changeArchiveFileName,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|