mirror of
https://github.com/duanhf2012/origin.git
synced 2026-02-28 01:44:47 +08:00
log optimization: split log by date
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
@@ -12,6 +13,10 @@ import (
|
|||||||
"github.com/duanhf2012/origin/service"
|
"github.com/duanhf2012/origin/service"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
maxLinesInLog = 5000 //一个日志文件最多只写这么多行
|
||||||
|
)
|
||||||
|
|
||||||
//等级从低到高
|
//等级从低到高
|
||||||
const (
|
const (
|
||||||
LEVER_UNKNOW = 0
|
LEVER_UNKNOW = 0
|
||||||
@@ -35,7 +40,8 @@ type FunListenLog func(uint, string)
|
|||||||
|
|
||||||
type LogModule struct {
|
type LogModule struct {
|
||||||
service.BaseModule
|
service.BaseModule
|
||||||
currentDay int
|
currentDay int64
|
||||||
|
lines int64
|
||||||
logfilename string
|
logfilename string
|
||||||
logger [LEVEL_MAX]*log.Logger
|
logger [LEVEL_MAX]*log.Logger
|
||||||
logFile *os.File
|
logFile *os.File
|
||||||
@@ -46,38 +52,52 @@ type LogModule struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (slf *LogModule) GetCurrentFileName() string {
|
func (slf *LogModule) GetCurrentFileName() string {
|
||||||
return slf.logfilename + "-" + time.Now().Format("2006-01-02") + ".log"
|
now := time.Now()
|
||||||
|
fpath := filepath.Join("logs", now.Format("2006-01-02"))
|
||||||
|
os.MkdirAll(fpath, os.ModePerm)
|
||||||
|
fname := slf.logfilename + "-" + now.Format("20060102-150405") + ".log"
|
||||||
|
ret := filepath.Join(fpath, fname)
|
||||||
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
func (slf *LogModule) CheckAndGenFile() {
|
//检查是否需要切换新的日志文件
|
||||||
|
func (slf *LogModule) CheckAndGenFile(fileline string) (newFile bool) {
|
||||||
|
now := time.Now()
|
||||||
|
nowDate := int64(now.Day())
|
||||||
|
|
||||||
slf.locker.Lock()
|
slf.locker.Lock()
|
||||||
if time.Now().Day() != slf.currentDay {
|
|
||||||
|
|
||||||
if time.Now().Day() == slf.currentDay {
|
isNewDay := nowDate != slf.currentDay
|
||||||
slf.locker.Unlock()
|
slf.lines++
|
||||||
return
|
if isNewDay || slf.lines > maxLinesInLog {
|
||||||
}
|
// if time.Now().Day() == slf.currentDay {
|
||||||
|
// slf.locker.Unlock()
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
//fmt.Println("new log file", slf.currentDay, nowDate, isNewDay, slf.lines, maxLinesInLog)
|
||||||
|
|
||||||
slf.currentDay = time.Now().Day()
|
slf.currentDay = nowDate
|
||||||
|
slf.lines = 1
|
||||||
|
newFile = true
|
||||||
if slf.logFile != nil {
|
if slf.logFile != nil {
|
||||||
slf.logFile.Close()
|
slf.logFile.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
slf.logFile, err = os.OpenFile(slf.GetCurrentFileName(), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0766)
|
slf.logFile, err = os.OpenFile(slf.GetCurrentFileName(), os.O_RDWR|os.O_CREATE|os.O_APPEND, os.ModePerm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("create log file %+v error!", slf.GetCurrentFileName())
|
fmt.Printf("create log file %+v error!", slf.GetCurrentFileName())
|
||||||
slf.locker.Unlock()
|
slf.locker.Unlock()
|
||||||
return
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
for level := 0; level < LEVEL_MAX; level++ {
|
for level := 0; level < LEVEL_MAX; level++ {
|
||||||
slf.logger[level] = log.New(slf.logFile, LogPrefix[level], log.Lshortfile|log.LstdFlags)
|
slf.logger[level] = log.New(slf.logFile, LogPrefix[level], log.Lshortfile|log.LstdFlags)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
slf.locker.Unlock()
|
slf.locker.Unlock()
|
||||||
|
return newFile
|
||||||
}
|
}
|
||||||
|
|
||||||
func (slf *LogModule) Init(logfilename string, openLevel uint) {
|
func (slf *LogModule) Init(logfilename string, openLevel uint) {
|
||||||
@@ -103,33 +123,16 @@ func (slf *LogModule) Printf(level uint, format string, v ...interface{}) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if slf.openLevel == LEVER_DEBUG || slf.listenFun != nil {
|
_, file, line, ok := runtime.Caller(slf.calldepth - 1)
|
||||||
strlog := fmt.Sprintf(format, v...)
|
if !ok {
|
||||||
if slf.openLevel == LEVER_DEBUG {
|
file = "???"
|
||||||
fmt.Println(LogPrefix[level], time.Now().Format("2006/1/2 15:04:05"), strlog)
|
line = 0
|
||||||
}
|
|
||||||
|
|
||||||
if slf.listenFun != nil {
|
|
||||||
var file string
|
|
||||||
var line int
|
|
||||||
var ok bool
|
|
||||||
_, file, line, ok = runtime.Caller(slf.calldepth - 1)
|
|
||||||
if !ok {
|
|
||||||
file = "???"
|
|
||||||
line = 0
|
|
||||||
}
|
|
||||||
parts := strings.Split(file, "/")
|
|
||||||
if len(parts) > 0 {
|
|
||||||
file = parts[len(parts)-1]
|
|
||||||
}
|
|
||||||
|
|
||||||
ft := LogPrefix[level] + time.Now().Format("2006/1/2 15:04:05") + fmt.Sprintf(" %s:%d:", file, line) + format
|
|
||||||
slf.listenFun(level, fmt.Sprintf(ft, v...))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
fileLine := fmt.Sprintf(" %s:%d: ", file, line)
|
||||||
|
slf.CheckAndGenFile(fileLine)
|
||||||
|
|
||||||
slf.CheckAndGenFile()
|
logContents := fmt.Sprintf(format, v...)
|
||||||
slf.GetLoggerByLevel(level).Output(slf.calldepth, fmt.Sprintf(format, v...))
|
slf.doPutLog(level, fileLine, logContents)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (slf *LogModule) Print(level uint, v ...interface{}) {
|
func (slf *LogModule) Print(level uint, v ...interface{}) {
|
||||||
@@ -137,19 +140,38 @@ func (slf *LogModule) Print(level uint, v ...interface{}) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_, file, line, ok := runtime.Caller(slf.calldepth - 1)
|
||||||
|
if !ok {
|
||||||
|
file = "???"
|
||||||
|
line = 0
|
||||||
|
}
|
||||||
|
fileLine := fmt.Sprintf(" %s:%d: ", file, line)
|
||||||
|
slf.CheckAndGenFile(fileLine)
|
||||||
|
|
||||||
|
logContents := fmt.Sprint(v...)
|
||||||
|
slf.doPutLog(level, fileLine, logContents)
|
||||||
|
}
|
||||||
|
|
||||||
|
//最终写日志的接口
|
||||||
|
func (slf *LogModule) doPutLog(level uint, fileLine, logContents string) {
|
||||||
if slf.openLevel == LEVER_DEBUG || slf.listenFun != nil {
|
if slf.openLevel == LEVER_DEBUG || slf.listenFun != nil {
|
||||||
strlog := fmt.Sprint(v...)
|
strlog := fmt.Sprintf("%s %s %s", LogPrefix[level], time.Now().Format("2006-01-02 15:04:05"), logContents)
|
||||||
if slf.openLevel == LEVER_DEBUG {
|
if slf.openLevel == LEVER_DEBUG {
|
||||||
fmt.Println(LogPrefix[level], strlog)
|
fmt.Println(strlog)
|
||||||
}
|
}
|
||||||
|
|
||||||
if slf.listenFun != nil {
|
if slf.listenFun != nil {
|
||||||
slf.listenFun(level, fmt.Sprint(v...))
|
fline := fileLine
|
||||||
|
if idx := strings.LastIndex(fileLine, "/"); idx >= 0 {
|
||||||
|
fline = fileLine[idx+1:]
|
||||||
|
}
|
||||||
|
|
||||||
|
ft := fline + " " + strlog
|
||||||
|
slf.listenFun(level, fmt.Sprintf(ft))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
slf.CheckAndGenFile()
|
slf.GetLoggerByLevel(level).Output(slf.calldepth+1, logContents)
|
||||||
slf.GetLoggerByLevel(level).Output(slf.calldepth, fmt.Sprint(v...))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (slf *LogModule) AppendCallDepth(calldepth int) {
|
func (slf *LogModule) AppendCallDepth(calldepth int) {
|
||||||
|
|||||||
Reference in New Issue
Block a user