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