Browse Source

添加日志支持io.Write

lxg 3 years ago
parent
commit
b04cb0374f
1 changed files with 98 additions and 76 deletions
  1. 98 76
      log/file.go

+ 98 - 76
log/file.go

@@ -14,7 +14,7 @@ type File struct {
 	MaxLogFiles int    `json:"max_log_files"`
 	Format      string `json:"format"`
 	Level       int    `json:"level"`
-	locker      sync.RWMutex
+	mutex       sync.RWMutex
 	buf         []byte
 	fp          *os.File
 	prefix      string
@@ -37,71 +37,71 @@ func itoa(buf *[]byte, i int, wid int) {
 	*buf = append(*buf, b[bp:]...)
 }
 
-func (log *File) SetLevel(lv int) {
-	log.Level = lv
+func (lg *File) SetLevel(lv int) {
+	lg.Level = lv
 }
 
-func (log *File) Prefix(s string) {
-	log.prefix = s
+func (lg *File) Prefix(s string) {
+	lg.prefix = s
 }
 
-func (log *File) Print(i ...interface{}) {
-	log.write(TraceLevel, fmt.Sprint(i...))
+func (lg *File) Print(i ...interface{}) {
+	lg.write(TraceLevel, fmt.Sprint(i...))
 }
 
-func (log *File) Printf(format string, args ...interface{}) {
-	log.write(TraceLevel, fmt.Sprintf(format, args...))
+func (lg *File) Printf(format string, args ...interface{}) {
+	lg.write(TraceLevel, fmt.Sprintf(format, args...))
 }
 
-func (log *File) Debug(i ...interface{}) {
-	log.write(DebugLevel, fmt.Sprint(i...))
+func (lg *File) Debug(i ...interface{}) {
+	lg.write(DebugLevel, fmt.Sprint(i...))
 }
 
-func (log *File) Debugf(format string, args ...interface{}) {
-	log.write(DebugLevel, fmt.Sprintf(format, args...))
+func (lg *File) Debugf(format string, args ...interface{}) {
+	lg.write(DebugLevel, fmt.Sprintf(format, args...))
 }
 
-func (log *File) Info(i ...interface{}) {
-	log.write(InfoLevel, fmt.Sprint(i...))
+func (lg *File) Info(i ...interface{}) {
+	lg.write(InfoLevel, fmt.Sprint(i...))
 }
 
-func (log *File) Infof(format string, args ...interface{}) {
-	log.write(InfoLevel, fmt.Sprintf(format, args...))
+func (lg *File) Infof(format string, args ...interface{}) {
+	lg.write(InfoLevel, fmt.Sprintf(format, args...))
 }
 
-func (log *File) Warn(i ...interface{}) {
-	log.write(WarnLevel, fmt.Sprint(i...))
+func (lg *File) Warn(i ...interface{}) {
+	lg.write(WarnLevel, fmt.Sprint(i...))
 }
 
-func (log *File) Warnf(format string, args ...interface{}) {
-	log.write(WarnLevel, fmt.Sprintf(format, args...))
+func (lg *File) Warnf(format string, args ...interface{}) {
+	lg.write(WarnLevel, fmt.Sprintf(format, args...))
 }
 
-func (log *File) Error(i ...interface{}) {
-	log.write(ErrorLevel, fmt.Sprint(i...))
+func (lg *File) Error(i ...interface{}) {
+	lg.write(ErrorLevel, fmt.Sprint(i...))
 }
 
-func (log *File) Errorf(format string, args ...interface{}) {
-	log.write(ErrorLevel, fmt.Sprintf(format, args...))
+func (lg *File) Errorf(format string, args ...interface{}) {
+	lg.write(ErrorLevel, fmt.Sprintf(format, args...))
 }
 
-func (log *File) Fatal(i ...interface{}) {
-	log.write(FatalLevel, fmt.Sprint(i...))
+func (lg *File) Fatal(i ...interface{}) {
+	lg.write(FatalLevel, fmt.Sprint(i...))
 }
 
-func (log *File) Fatalf(format string, args ...interface{}) {
-	log.write(FatalLevel, fmt.Sprintf(format, args...))
+func (lg *File) Fatalf(format string, args ...interface{}) {
+	lg.write(FatalLevel, fmt.Sprintf(format, args...))
 }
 
-func (log *File) Panic(i ...interface{}) {
-	log.write(PanicLevel, fmt.Sprint(i...))
+func (lg *File) Panic(i ...interface{}) {
+	lg.write(PanicLevel, fmt.Sprint(i...))
 }
 
-func (log *File) Panicf(format string, args ...interface{}) {
-	log.write(PanicLevel, fmt.Sprintf(format, args...))
+func (lg *File) Panicf(format string, args ...interface{}) {
+	lg.write(PanicLevel, fmt.Sprintf(format, args...))
 }
 
-func (log *File) format(buf *[]byte, level int, s string) (err error) {
+func (lg *File) format(buf *[]byte, level int, s string) (err error) {
 	t := time.Now()
 	year, month, day := t.Date()
 	itoa(buf, year, 4)
@@ -125,37 +125,55 @@ func (log *File) format(buf *[]byte, level int, s string) (err error) {
 	return
 }
 
-func (log *File) write(level int, s string) {
+//Write 实现标准的写入行数
+func (lg *File) Write(p []byte) (n int, err error) {
+	lg.mutex.Lock()
+	if n, err = lg.fp.Write(p); err == nil {
+		lg.mutex.Unlock()
+		lg.size += int64(n)
+		if lg.MaxSize > 0 && lg.size >= lg.MaxSize {
+			if err = lg.rotate(); err != nil {
+				return
+			}
+			lg.size = 0
+		}
+	} else {
+		lg.mutex.Unlock()
+	}
+	return
+}
+
+func (lg *File) write(level int, s string) {
 	var (
 		n   int
 		err error
 	)
-	if log.Level > level {
+	if lg.Level > level {
 		return
 	}
-	log.locker.Lock()
-	log.buf = log.buf[:0]
-	if err = log.format(&log.buf, level, s); err != nil {
-		log.locker.Unlock()
+	lg.mutex.Lock()
+	lg.buf = lg.buf[:0]
+	if err = lg.format(&lg.buf, level, s); err != nil {
+		lg.mutex.Unlock()
 		return
 	}
-	log.buf = append(log.buf, '\n')
-	if n, err = log.fp.Write(log.buf); err != nil {
-		log.locker.Unlock()
+	lg.buf = append(lg.buf, '\n')
+	if n, err = lg.fp.Write(lg.buf); err != nil {
+		lg.mutex.Unlock()
 		return
 	}
-	log.locker.Unlock()
+	lg.mutex.Unlock()
 	//write br
-	log.size += int64(n)
-	if log.MaxSize > 0 && log.size >= log.MaxSize {
-		if err = log.rotate(); err != nil {
+	lg.size += int64(n)
+	if lg.MaxSize > 0 && lg.size >= lg.MaxSize {
+		if err = lg.rotate(); err != nil {
 			return
 		}
-		log.size = 0
+		lg.size = 0
 	}
 }
 
-func (log *File) isExists(filename string) bool {
+func (lg *File) isExists(filename string) bool {
 	if _, err := os.Stat(filename); err == nil {
 		return true
 	} else {
@@ -163,69 +181,73 @@ func (log *File) isExists(filename string) bool {
 	}
 }
 
-func (log *File) rotate() (err error) {
-	log.locker.Lock()
-	defer log.locker.Unlock()
-	if err = log.close(); err != nil {
+//rotate 实现日志滚动处理
+func (lg *File) rotate() (err error) {
+	lg.mutex.Lock()
+	defer lg.mutex.Unlock()
+	if err = lg.close(); err != nil {
 		return
 	}
-	for i := log.MaxLogFiles; i >= 0; i-- {
-		filename := log.Filename
+	for i := lg.MaxLogFiles; i >= 0; i-- {
+		filename := lg.Filename
 		if i > 0 {
 			filename += "." + strconv.Itoa(i)
 		}
-		if i == log.MaxLogFiles {
-			if log.isExists(filename) {
+		if i == lg.MaxLogFiles {
+			if lg.isExists(filename) {
 				if err = os.Remove(filename); err != nil {
 					return
 				}
 			}
 		} else {
-			if log.isExists(filename) {
-				if err = os.Rename(filename, log.Filename+"."+strconv.Itoa(i+1)); err != nil {
+			if lg.isExists(filename) {
+				if err = os.Rename(filename, lg.Filename+"."+strconv.Itoa(i+1)); err != nil {
 					return
 				}
 			}
 		}
 	}
-	err = log.open()
+	err = lg.open()
 	return
 }
 
-func (log *File) Reload() (err error) {
-	log.locker.Lock()
-	_ = log.close()
-	err = log.open()
-	log.locker.Unlock()
+func (lg *File) Reload() (err error) {
+	lg.mutex.Lock()
+	_ = lg.close()
+	err = lg.open()
+	lg.mutex.Unlock()
 	return
 }
 
-func (log *File) open() (err error) {
-	if log.fp, err = os.OpenFile(log.Filename, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600); err != nil {
+func (lg *File) open() (err error) {
+	if lg.fp, err = os.OpenFile(lg.Filename, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600); err != nil {
 		return
 	}
 	return
 }
 
-func (log *File) Open() (err error) {
-	if err = log.open(); err != nil {
+func (lg *File) Open() (err error) {
+	var (
+		info os.FileInfo
+	)
+	if err = lg.open(); err != nil {
 		return
 	}
-	if info, err := os.Stat(log.Filename); err == nil {
-		log.size = info.Size()
+	if info, err = os.Stat(lg.Filename); err == nil {
+		lg.size = info.Size()
 	}
 	return
 }
 
-func (log *File) close() (err error) {
-	if log.fp != nil {
-		err = log.fp.Close()
+func (lg *File) close() (err error) {
+	if lg.fp != nil {
+		err = lg.fp.Close()
 	}
 	return
 }
 
-func (log *File) Close() (err error) {
-	err = log.close()
+func (lg *File) Close() (err error) {
+	err = lg.close()
 	return
 }