This commit is contained in:
bryanqiu 2022-12-05 10:55:24 +08:00
parent 7400653848
commit a2043ae99c
4 changed files with 114 additions and 234 deletions

237
log.go
View File

@ -3,13 +3,10 @@
package log package log
import ( import (
"bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"log" "log"
"os" "os"
"path/filepath"
"runtime"
"runtime/debug" "runtime/debug"
"strings" "strings"
"sync" "sync"
@ -18,73 +15,38 @@ import (
"qoobing.com/gomod/str" "qoobing.com/gomod/str"
) )
var ( const (
glogger *log.Logger = nil PANIC logLevel = 0
once sync.Once = sync.Once{} FATAL logLevel = 1
mylogger *Logger = nil ERROR logLevel = 2
modulelogs map[string]*Logger = map[string]*Logger{} NOTICE logLevel = 3
logidSetter func(string) = nil WARNING logLevel = 4
logidGetter func() string = nil INFO logLevel = 5
DEBUG logLevel = 9
ErrLogPanic = "~~~~panic~~~~~~"
) )
const ErrLogPanic = "~~~~panic~~~~~~" var (
golog *log.Logger = nil
//////////////global log//////////////////begin/////////////////// mylog *Logger = nil
func SetLogLevel(newlv logLevel) (oldlv logLevel) { logidCreator LogidCreator = nil
return mylogger.SetLogLevel(newlv) modulelogs map[string]*Logger = map[string]*Logger{}
} )
func PrintPretty(prefix string, v interface{}) {
mylogger.PrintPretty(prefix, v)
}
func DebugfWithDepth(calldepth int, format string, v ...interface{}) {
mylogger.DebugfWithDepth(calldepth, format, v...)
}
func Debugf(format string, v ...interface{}) {
mylogger.Debugf(format, v...)
}
func Noticef(format string, v ...interface{}) {
mylogger.Noticef(format, v...)
}
func NoticefWithDepth(calldepth int, format string, v ...interface{}) {
mylogger.NoticefWithDepth(calldepth, format, v...)
}
func Infof(format string, v ...interface{}) {
mylogger.Infof(format, v...)
}
func Warningf(format string, v ...interface{}) {
mylogger.Warningf(format, v...)
}
func Errorf(format string, v ...interface{}) {
mylogger.Warningf(format, v...)
}
func Panicf(format string, v ...interface{}) {
mylogger.Panicf(format, v...)
}
func Fatalf(format string, v ...interface{}) {
mylogger.Fatalf(format, v...)
}
//////////////////////////////////////////////////////////////////
type logLevel int type logLevel int
const (
PANIC logLevel = 0
FATAL logLevel = 1
ERROR logLevel = 2
NOTICE logLevel = 3
WARNING logLevel = 4
INFO logLevel = 5
DEBUG logLevel = 9
)
type Logger struct { type Logger struct {
module string module string
golog *log.Logger
loglevel logLevel loglevel logLevel
logger *log.Logger
} }
func New(module string) (logger *Logger) { type LogidCreator interface {
Cleanup()
GetLogid() string
SetLogid(logid string)
}
func New(module string) *Logger {
if module == "" { if module == "" {
module = "undefine" module = "undefine"
} }
@ -93,13 +55,14 @@ func New(module string) (logger *Logger) {
} else { } else {
modulelogs[module] = &Logger{ modulelogs[module] = &Logger{
module: module, module: module,
logger: glogger, golog: golog,
loglevel: DEBUG, loglevel: DEBUG,
} }
return modulelogs[module] return modulelogs[module]
} }
} }
//////////////////////////////////////////////////////////////////
func (log *Logger) SetLogLevel(newlv logLevel) (oldlv logLevel) { func (log *Logger) SetLogLevel(newlv logLevel) (oldlv logLevel) {
oldlv = log.loglevel oldlv = log.loglevel
log.loglevel = newlv log.loglevel = newlv
@ -155,7 +118,10 @@ func (log *Logger) Panicf(format string, v ...interface{}) {
} }
func (log *Logger) GetLogidStr(format string) string { func (log *Logger) GetLogidStr(format string) string {
return fmt.Sprintf(format, GetLogid()) if logidCreator != nil {
return fmt.Sprintf(format, logidCreator.GetLogid())
}
return ""
} }
func (log *Logger) logwrite(typ logLevel, calldepth int, format string, v ...interface{}) { func (log *Logger) logwrite(typ logLevel, calldepth int, format string, v ...interface{}) {
@ -167,152 +133,79 @@ func (log *Logger) logwrite(typ logLevel, calldepth int, format string, v ...int
format = strings.Trim(format, "\n") format = strings.Trim(format, "\n")
switch typ { switch typ {
case PANIC: case PANIC:
log.logger.SetPrefix("\x1b[31m" + "PANIC " + idstr + "[" + log.module + "] ") log.golog.SetPrefix("\x1b[31m" + "PANIC " + idstr + "[" + log.module + "] ")
case FATAL: case FATAL:
log.logger.SetPrefix("\x1b[31m" + "FATAL " + idstr + "[" + log.module + "] ") log.golog.SetPrefix("\x1b[31m" + "FATAL " + idstr + "[" + log.module + "] ")
case WARNING: case WARNING:
log.logger.SetPrefix("\x1b[32m" + "WARNING " + idstr + "[" + log.module + "] ") log.golog.SetPrefix("\x1b[32m" + "WARNING " + idstr + "[" + log.module + "] ")
case ERROR: case ERROR:
log.logger.SetPrefix("\x1b[32m" + "ERROR " + idstr + "[" + log.module + "] ") log.golog.SetPrefix("\x1b[32m" + "ERROR " + idstr + "[" + log.module + "] ")
case INFO: case INFO:
log.logger.SetPrefix("INFO " + idstr + "[" + log.module + "] ") log.golog.SetPrefix("INFO " + idstr + "[" + log.module + "] ")
case NOTICE: case NOTICE:
log.logger.SetPrefix("NOTICE " + idstr + "[" + log.module + "] ") log.golog.SetPrefix("NOTICE " + idstr + "[" + log.module + "] ")
case DEBUG: case DEBUG:
log.logger.SetPrefix("DEBUG " + idstr + "[" + log.module + "] ") log.golog.SetPrefix("DEBUG " + idstr + "[" + log.module + "] ")
default: default:
log.logger.SetPrefix("UNKNOWN " + idstr + "[" + log.module + "] ") log.golog.SetPrefix("UNKNOWN " + idstr + "[" + log.module + "] ")
} }
if mylogger == log { if mylog == log {
calldepth = calldepth + 1 calldepth = calldepth + 1
} }
if typ == FATAL || typ == WARNING || typ == ERROR { if typ == FATAL || typ == WARNING || typ == ERROR {
log.logger.Output(calldepth, fmt.Sprintf(format+"\x1b[0m\n", v...)) log.golog.Output(calldepth, fmt.Sprintf(format+"\x1b[0m\n", v...))
} else if typ == NOTICE { } else if typ == NOTICE {
calldepth = calldepth + 2 calldepth = calldepth + 2
log.logger.Output(calldepth, fmt.Sprintf(format+"\n", v...)) log.golog.Output(calldepth, fmt.Sprintf(format+"\n", v...))
} else if typ == PANIC { } else if typ == PANIC {
stack := strings.Replace(string(debug.Stack()), "\n", "\n== ", -1) stack := strings.Replace(string(debug.Stack()), "\n", "\n== ", -1)
stack = str.SkipLine(stack, calldepth*2+1) stack = str.SkipLine(stack, calldepth*2+1)
v = append(v, stack) v = append(v, stack)
panicstr := fmt.Sprintf(format+"\x1b[0m. Panic stack:\n%s\n", v...) panicstr := fmt.Sprintf(format+"\x1b[0m. Panic stack:\n%s\n", v...)
log.logger.Output(calldepth, panicstr) log.golog.Output(calldepth, panicstr)
panic(ErrLogPanic) panic(ErrLogPanic)
} else { } else {
log.logger.Output(calldepth, fmt.Sprintf(format+"\n", v...)) log.golog.Output(calldepth, fmt.Sprintf(format+"\n", v...))
} }
} }
//////////////global log trace//////////////////begin///////////////////
func TRACE_INTO(format string, v ...interface{}) string {
_, fn, line, _ := runtime.Caller(1)
strfn := fmt.Sprintf("%s:%d", fn, line)
Debugf("TRACE into ["+strfn+"]"+format+"...\r\n", v...)
return strfn
}
func TRACE_EXIT(strfn string, format string, v ...interface{}) {
Debugf("TRACE exit ["+strfn+"]"+format+"...\r\n", v...)
}
type mWriter struct {
logw *os.File
stdw *os.File
}
func (w mWriter) Write(p []byte) (n int, err error) {
if flagcachelog {
logCache.Write(p)
}
w.stdw.Write(p)
return w.logw.Write(p)
}
var cacheLogLock = new(sync.Mutex)
var flagcachelog = false
var logCache = bytes.Buffer{}
func StartCacheLog() {
cacheLogLock.Lock()
flagcachelog = true
logCache.Reset()
}
func StopCacheLog() {
cacheLogLock.Unlock()
flagcachelog = false
}
func GetCacheLog() string {
if flagcachelog != true {
panic("GetCacheLog MUST call after StartCacheLog called")
}
return logCache.String()
}
//////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////
/////////////////////////Initialize///////////////////////////////////////////////// /////////////////////////Initialize/////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////
var once sync.Once = sync.Once{}
// init
func init() { func init() {
once.Do(initlog) once.Do(initlog)
} }
// initlog
func initlog() { func initlog() {
var mwriter mWriter // Step 1. create base writer & cache writer for ALL log
var openlog = func(logname string) *log.Logger { var (
fp, err := os.OpenFile(logname, os.O_APPEND|os.O_CREATE|os.O_WRONLY, os.ModePerm) logDir = "./logs/"
if err != nil { logName = getExeFilename()
panic("open log file failed:" + err.Error()) logDirsOptional = []string{"./log", "/var/log"}
} logSplitTag = "20060102" //20060102150405
mwriter.logw.Close() baseWriter = NewBaseWriter(nil, os.Stdout, nil)
mwriter.logw = fp cacheWriter = NewCacheWriter(baseWriter)
mwriter.stdw = os.Stdout gologFlags = log.Ldate | log.Lmicroseconds | log.Lshortfile
glogger = log.New(mwriter, "", log.Ldate|log.Lmicroseconds|log.Lshortfile) goSystemLog = log.New(cacheWriter, "", gologFlags)
return glogger )
}
_, logfilename := filepath.Split(os.Args[0]) // Step 2. init golog(golang std logger) & mylog(myself defined logger)
LOGSPLITTIME := "20060102" //20060102150405 golog = goSystemLog
LOGFILENAME := logfilename + ".log" mylog = New("system")
if _, err := os.Stat("log/"); err == nil { baseWriter.OpenLogFile(logDir, logName, logDirsOptional)
LOGFILENAME = "log/" + LOGFILENAME
} else if _, err := os.Stat("logs/"); err == nil {
LOGFILENAME = "logs/" + LOGFILENAME
} else {
LOGFILENAME = "logs/" + LOGFILENAME
}
glogger = openlog(LOGFILENAME)
mylogger = New("system")
curhourtime := time.Now().Local().Format(LOGSPLITTIME)
prehourtime := curhourtime
// Step 3. backgroud split log (log rotate)
go func() { go func() {
intva := 2 * time.Minute
timer := time.NewTimer(1 * time.Second)
for { for {
select { tag := time.Now().Local().Format(logSplitTag)
case <-timer.C: baseWriter.TryBackupLogFile(tag)
curhourtime := time.Now().Local().Format(LOGSPLITTIME) time.Sleep(2 * time.Minute)
//Debugf("check logfile: prehourtime:%s,curhourtime:%s", prehourtime, curhourtime)
if prehourtime != curhourtime {
// close old logger & move log to backup file.
PREFILENAME := LOGFILENAME + "." + prehourtime
os.Rename(LOGFILENAME, PREFILENAME)
prehourtime = curhourtime
glogger = openlog(LOGFILENAME)
mylogger = New("system")
for _, l := range modulelogs {
l.logger = glogger
}
}
timer.Reset(intva)
}
} }
}() }()
} }

View File

@ -1,18 +1,38 @@
// +build glslogid
package log package log
import( import (
"github.com/jtolds/gls" "fmt"
) "github.com/tylerb/gls"
)
type glsLogid struct { type glsLogidCreator struct {
mgr *gls.ContextManager idkey string
idkey gls.ContextKey
} }
func GetLogid() string { func (creator *glsLogidCreator) GetLogid() string {
return "" id := gls.Get(creator.idkey)
if id == nil {
return ""
}
if logid, ok := id.(string); ok {
return logid
} else {
return fmt.Sprintf("%s", id)
}
} }
func SetLogid(logid string) { func (creator *glsLogidCreator) SetLogid(logid string) {
gls.Set(creator.idkey, logid)
}
func (creator *glsLogidCreator) Cleanup() {
gls.Cleanup()
}
func init() {
idc := &glsLogidCreator{
idkey: "logidkey",
}
RegisterLogidCreator(idc)
} }

View File

@ -1,9 +0,0 @@
package log
func GetLogid() string {
return ""
}
func SetLogid(logid string) {
}

View File

@ -1,45 +1,21 @@
// +build runtimelogid
package log package log
func GetLogid() string { //// Format writes the source file path and line number of the record to the buf.
if logidGetter != nil { //func (p *LogidFormatPart) Format(r *Record, buf *bytes.Buffer) {
return logidGetter() // logid := p.GetLogid()
} // logidstr := strconv.FormatInt(logid, 10)
return "" // buf.WriteString(logidstr)
} //}
//
func SetLogid(logid string) { //func (p *LogidFormatPart) GetLogid() int64 {
if logidSetter != nil { // return runtime.GetLogid()
logidSetter(logid) //}
} //
} //func (p *LogidFormatPart) SetLogid(newid int64) (oldid int64) {
// +build logid // return runtime.SetLogid(newid)
//}
package yk_logger //
//func init() {
import ( // LogidSupporter = &LogidFormatPart{}
"bytes" //}
"runtime"
"strconv"
)
// LogidFormatPart is a FormatPart of the full source code placeholder.
type LogidFormatPart struct{}
// Format writes the source file path and line number of the record to the buf.
func (p *LogidFormatPart) Format(r *Record, buf *bytes.Buffer) {
logid := p.GetLogid()
logidstr := strconv.FormatInt(logid, 10)
buf.WriteString(logidstr)
}
func (p *LogidFormatPart) GetLogid() int64 {
return runtime.GetLogid()
}
func (p *LogidFormatPart) SetLogid(newid int64) (oldid int64) {
return runtime.SetLogid(newid)
}
func init() {
LogidSupporter = &LogidFormatPart{}
}