上传日志优化
This commit is contained in:
parent
69957984af
commit
489ab8e5d1
@ -35,6 +35,7 @@ type LogFileOut struct {
|
||||
MaxBackups int `json:"maxbackups" yaml:"maxbackups"` //备份日志的最大数量
|
||||
LocalTime bool `json:"localtime" yaml:"localtime"` //是使用本地时间还是使用世界标准时间
|
||||
Compress bool `json:"compress" yaml:"compress"` //是否压缩备份日志
|
||||
UniqueLog bool `json:"uniquelog" yaml:"uniquelog"` //是否唯一日志文件
|
||||
size int64
|
||||
ctime time.Time
|
||||
file *os.File
|
||||
@ -79,31 +80,42 @@ func (l *LogFileOut) Write(p []byte) (n int, err error) {
|
||||
return n, err
|
||||
}
|
||||
|
||||
func (l *LogFileOut) openExistingOrNew(writeLen int) error {
|
||||
l.mill()
|
||||
func (this *LogFileOut) openExistingOrNew(writeLen int) error {
|
||||
this.mill()
|
||||
|
||||
filename := l.filename()
|
||||
filename := this.filename()
|
||||
info, err := osStat(filename)
|
||||
if os.IsNotExist(err) {
|
||||
return l.openNew()
|
||||
return this.openNew()
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("error getting log file info: %s", err)
|
||||
}
|
||||
|
||||
//校验是否需要切割日志
|
||||
if info.Size()+int64(writeLen) >= l.max() || (!l.ctime.IsZero() && time.Since(l.ctime) > l.CupTime) {
|
||||
return l.rotate()
|
||||
if !this.UniqueLog && (info.Size()+int64(writeLen) >= this.max() || (!this.ctime.IsZero() && time.Since(this.ctime) > this.CupTime)) {
|
||||
return this.rotate()
|
||||
}
|
||||
|
||||
file, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
|
||||
if err != nil {
|
||||
// if we fail to open the old log file for some reason, just ignore
|
||||
// it and open a new log file.
|
||||
return l.openNew()
|
||||
mode := os.FileMode(0666)
|
||||
if !this.UniqueLog {
|
||||
file, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, mode)
|
||||
if err != nil {
|
||||
// if we fail to open the old log file for some reason, just ignore
|
||||
// it and open a new log file.
|
||||
return this.openNew()
|
||||
}
|
||||
this.file = file
|
||||
this.size = info.Size()
|
||||
} else {
|
||||
//最佳写入
|
||||
f, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, mode)
|
||||
if err != nil {
|
||||
return fmt.Errorf("can't open new logfile: %s", err)
|
||||
}
|
||||
this.file = f
|
||||
this.size = 0
|
||||
this.ctime = currentTime()
|
||||
}
|
||||
l.file = file
|
||||
l.size = info.Size()
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -210,21 +222,21 @@ func (l *LogFileOut) mill() {
|
||||
}
|
||||
|
||||
//备份老的日志文件创建新的日志文件
|
||||
func (l *LogFileOut) openNew() error {
|
||||
err := os.MkdirAll(l.dir(), 0755)
|
||||
func (this *LogFileOut) openNew() error {
|
||||
err := os.MkdirAll(this.dir(), 0755)
|
||||
if err != nil {
|
||||
return fmt.Errorf("can't make directories for new logfile: %s", err)
|
||||
}
|
||||
|
||||
name := l.filename()
|
||||
name := this.filename()
|
||||
mode := os.FileMode(0666)
|
||||
info, err := osStat(name)
|
||||
//备份老的日志文件
|
||||
if err == nil {
|
||||
if !this.UniqueLog && err == nil {
|
||||
// Copy the mode off the old logfile.
|
||||
mode = info.Mode()
|
||||
// move the existing file
|
||||
newname := backupName(name, l.LocalTime)
|
||||
newname := backupName(name, this.LocalTime)
|
||||
if err := os.Rename(name, newname); err != nil {
|
||||
return fmt.Errorf("can't rename log file: %s", err)
|
||||
}
|
||||
@ -235,14 +247,25 @@ func (l *LogFileOut) openNew() error {
|
||||
}
|
||||
}
|
||||
|
||||
//创建新的日志文件
|
||||
f, err := os.OpenFile(name, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, mode)
|
||||
if err != nil {
|
||||
return fmt.Errorf("can't open new logfile: %s", err)
|
||||
if !this.UniqueLog {
|
||||
//创建新的日志文件
|
||||
f, err := os.OpenFile(name, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, mode)
|
||||
if err != nil {
|
||||
return fmt.Errorf("can't open new logfile: %s", err)
|
||||
}
|
||||
this.file = f
|
||||
this.size = 0
|
||||
this.ctime = currentTime()
|
||||
} else {
|
||||
//最佳写入
|
||||
f, err := os.OpenFile(name, os.O_APPEND|os.O_CREATE|os.O_WRONLY, mode)
|
||||
if err != nil {
|
||||
return fmt.Errorf("can't open new logfile: %s", err)
|
||||
}
|
||||
this.file = f
|
||||
this.size = 0
|
||||
this.ctime = currentTime()
|
||||
}
|
||||
l.file = f
|
||||
l.size = 0
|
||||
l.ctime = currentTime()
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -17,6 +17,7 @@ func newSys(options *Options) (sys *Logger, err error) {
|
||||
MaxBackups: options.MaxBackups, //最大备份数
|
||||
MaxSize: options.MaxSize, //最大日志文件大小
|
||||
LocalTime: true, //使用本地时间
|
||||
UniqueLog: options.UniqueLog, //是否唯一文件
|
||||
}
|
||||
if !options.IsDebug {
|
||||
if err = hook.openNew(); err != nil {
|
||||
|
@ -22,6 +22,7 @@ type Options struct {
|
||||
ReportCaller Loglevel //是否输出堆栈信息
|
||||
CallerSkip int //堆栈深度
|
||||
Encoder LogEncoder //日志输出样式
|
||||
UniqueLog bool //唯一日志文件(文件不分割 不备份,一直追加日志)
|
||||
CupTimeTime int //日志分割时间 单位 小时
|
||||
MaxAgeTime int //日志最大保存时间 单位天
|
||||
MaxBackups int //最大备份日志个数
|
||||
@ -75,6 +76,13 @@ func SetEncoder(v LogEncoder) Option {
|
||||
}
|
||||
}
|
||||
|
||||
///设置唯一日志
|
||||
func SetUniqueLog(v bool) Option {
|
||||
return func(o *Options) {
|
||||
o.UniqueLog = v
|
||||
}
|
||||
}
|
||||
|
||||
///日志分割时间 单位 小时
|
||||
func SetRotationTime(v int) Option {
|
||||
return func(o *Options) {
|
||||
|
Loading…
Reference in New Issue
Block a user