Compare commits

..

No commits in common. "master" and "main" have entirely different histories.
master ... main

456
log.go
View File

@ -3,330 +3,244 @@
package log package log
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"log" "log"
"os" "os"
"runtime" "runtime/debug"
"runtime/debug" "strings"
"strings" "sync"
"sync" "time"
"time"
"qoobing.com/gomod/str" "qoobing.com/gomod/str"
) )
const ( const (
PANIC LogLevel = 0 PANIC LogLevel = 0
FATAL LogLevel = 1 FATAL LogLevel = 1
ERROR LogLevel = 2 ERROR LogLevel = 2
NOTICE LogLevel = 3 NOTICE LogLevel = 3
WARNING LogLevel = 4 WARNING LogLevel = 4
INFO LogLevel = 5 INFO LogLevel = 5
DEBUG LogLevel = 9 DEBUG LogLevel = 9
ErrLogLevel = "~~~~~invalid loglevel~~~~~" ErrLogLevel = "~~~~~invalid loglevel~~~~~"
ErrLogPanic = "~~~~panic~~~~~~" ErrLogPanic = "~~~~panic~~~~~~"
) )
var ( var (
mapStrToLevel = map[string]LogLevel{ mapStrToLevel = map[string]LogLevel{
"PANIC": PANIC, "PANIC": PANIC,
"FATAL": FATAL, "FATAL": FATAL,
"ERROR": ERROR, "ERROR": ERROR,
"NOTICE": NOTICE, "NOTICE": NOTICE,
"WARNING": WARNING, "WARNING": WARNING,
"INFO": INFO, "INFO": INFO,
"DEBUG": DEBUG, "DEBUG": DEBUG,
} }
mapLevelToStr = map[LogLevel]string{ mapLevelToStr = map[LogLevel]string{
PANIC: "PANIC", PANIC: "PANIC",
FATAL: "FATAL", FATAL: "FATAL",
ERROR: "ERROR", ERROR: "ERROR",
NOTICE: "NOTICE", NOTICE: "NOTICE",
WARNING: "WARNING", WARNING: "WARNING",
INFO: "INFO", INFO: "INFO",
DEBUG: "DEBUG", DEBUG: "DEBUG",
} }
) )
var ( var (
golog *log.Logger = nil golog *log.Logger = nil
mylog *Logger = nil mylog *Logger = nil
logidCreator LogidCreator = nil logidCreator LogidCreator = nil
modulelogs map[string]*Logger = map[string]*Logger{} modulelogs map[string]*Logger = map[string]*Logger{}
) )
type LogLevel int type LogLevel int
type Logger struct { type Logger struct {
module string module string
golog *log.Logger golog *log.Logger
loglevel LogLevel loglevel LogLevel
} }
type LogidCreator interface { type LogidCreator interface {
Cleanup() Cleanup()
GetLogid() string GetLogid() string
SetLogid(logid string) SetLogid(logid string)
} }
func New(module string) *Logger { func New(module string) *Logger {
if module == "" { if module == "" {
module = "undefine" module = "undefine"
} }
if _, ok := modulelogs[module]; ok { if _, ok := modulelogs[module]; ok {
return modulelogs[module] return modulelogs[module]
} else { } else {
modulelogs[module] = &Logger{ modulelogs[module] = &Logger{
module: module, module: module,
golog: golog, golog: golog,
loglevel: DEBUG, loglevel: DEBUG,
} }
return modulelogs[module] return modulelogs[module]
} }
} }
// SetLogLevel set new log level // SetLogLevel set new log level
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
return oldlv return oldlv
} }
// SetLogLevelByName set new log level by level name // SetLogLevelByName set new log level by level name
func (log *Logger) SetLogLevelByName(newlv string) (oldlv string) { func (log *Logger) SetLogLevelByName(newlv string) (oldlv string) {
if newlevel, ok := mapStrToLevel[newlv]; ok { if newlevel, ok := mapStrToLevel[newlv]; ok {
oldlevel := log.loglevel oldlevel := log.loglevel
log.loglevel = newlevel log.loglevel = newlevel
return mapLevelToStr[oldlevel] return mapLevelToStr[oldlevel]
} }
return ErrLogLevel return ErrLogLevel
} }
func (log *Logger) Print(prefix string, v interface{}) { func (log *Logger) Print(prefix string, v interface{}) {
var str = "" var str = ""
if pkg, err := json.Marshal(v); err != nil { if pkg, err := json.Marshal(v); err != nil {
str = err.Error() str = err.Error()
} else { } else {
str = string(pkg) str = string(pkg)
} }
log.logwrite(DEBUG, 3, prefix+"%s\n", str) log.logwrite(DEBUG, 3, prefix+"%s\n", str)
} }
func (log *Logger) PrintPretty(prefix string, v interface{}) { func (log *Logger) PrintPretty(prefix string, v interface{}) {
var preety = "" var preety = ""
if pkg, err := json.MarshalIndent(v, "==", " "); err != nil { if pkg, err := json.MarshalIndent(v, "==", " "); err != nil {
preety = err.Error() preety = err.Error()
} else { } else {
preety = string(pkg) preety = string(pkg)
} }
log.logwrite(DEBUG, 3, prefix+"%s\n", preety) log.logwrite(DEBUG, 3, prefix+"%s\n", preety)
} }
func (log *Logger) Debugf(format string, v ...interface{}) { func (log *Logger) Debugf(format string, v ...interface{}) {
log.logwrite(DEBUG, 3, format, v...) log.logwrite(DEBUG, 3, format, v...)
} }
func (log *Logger) DebugfWithDepth(calldepth int, format string, v ...interface{}) { func (log *Logger) DebugfWithDepth(calldepth int, format string, v ...interface{}) {
calldepth += 3 calldepth += 3
log.logwrite(DEBUG, calldepth, format, v...) log.logwrite(DEBUG, calldepth, format, v...)
} }
func (log *Logger) Noticef(format string, v ...interface{}) { func (log *Logger) Noticef(format string, v ...interface{}) {
log.logwrite(NOTICE, 3, format, v...) log.logwrite(NOTICE, 3, format, v...)
} }
func (log *Logger) NoticefWithDepth(calldepth int, format string, v ...interface{}) { func (log *Logger) NoticefWithDepth(calldepth int, format string, v ...interface{}) {
calldepth += 3 calldepth += 3
log.logwrite(NOTICE, calldepth, format, v...) log.logwrite(NOTICE, calldepth, format, v...)
} }
func (log *Logger) Warningf(format string, v ...interface{}) { func (log *Logger) Warningf(format string, v ...interface{}) {
log.logwrite(WARNING, 3, format, v...) log.logwrite(WARNING, 3, format, v...)
} }
func (log *Logger) Infof(format string, v ...interface{}) { func (log *Logger) Infof(format string, v ...interface{}) {
log.logwrite(INFO, 3, format, v...) log.logwrite(INFO, 3, format, v...)
} }
func (log *Logger) Errorf(format string, v ...interface{}) { func (log *Logger) Errorf(format string, v ...interface{}) {
log.logwrite(ERROR, 3, format, v...) log.logwrite(ERROR, 3, format, v...)
} }
func (log *Logger) Fatalf(format string, v ...interface{}) { func (log *Logger) Fatalf(format string, v ...interface{}) {
log.logwrite(FATAL, 3, format, v...) log.logwrite(FATAL, 3, format, v...)
} }
func (log *Logger) Panicf(format string, v ...interface{}) { func (log *Logger) Panicf(format string, v ...interface{}) {
log.logwrite(PANIC, 3, format, v...) log.logwrite(PANIC, 3, format, v...)
} }
func (log *Logger) Fatal(v ...interface{}) { func (log *Logger) Fatal(v ...interface{}){
log.logwrite(FATAL, 3, "%s", fmt.Sprintln(v...)) log.logwrite(FATAL, 3, "%s", fmt.Sprintln(v...))
} }
func (log *Logger) Println(v ...interface{}) { func (log *Logger) Println(v ...interface{}){
log.logwrite(DEBUG, 3, "%s", fmt.Sprintln(v...)) log.logwrite(DEBUG, 3, "%s", fmt.Sprintln(v...))
} }
func (log *Logger) GetLogidStr(format string) string { func (log *Logger) GetLogidStr(format string) string {
if logidCreator != nil { if logidCreator != nil {
return fmt.Sprintf(format, logidCreator.GetLogid()) return fmt.Sprintf(format, logidCreator.GetLogid())
} }
return "" 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{}) {
if typ > log.loglevel { if typ > log.loglevel {
return return
} }
var ( var (
idstr = log.GetLogidStr("[%s]") idstr = log.GetLogidStr("[%s] ")
prestr = formatHeader(calldepth) + "" + idstr + "$ " prestr = idstr // + "[" + log.module + "] "
) )
format = strings.Trim(format, "\n") format = strings.Trim(format, "\n")
switch typ { switch typ {
case PANIC: case PANIC:
log.golog.SetPrefix("\x1b[31m" + "PANI ") log.golog.SetPrefix("\x1b[31m" + "PANI ")
case FATAL: case FATAL:
log.golog.SetPrefix("\x1b[31m" + "FATA ") log.golog.SetPrefix("\x1b[31m" + "FATA ")
case WARNING: case WARNING:
log.golog.SetPrefix("\x1b[32m" + "WARN ") log.golog.SetPrefix("\x1b[32m" + "WARN ")
case ERROR: case ERROR:
log.golog.SetPrefix("\x1b[33m" + "ERRO ") log.golog.SetPrefix("\x1b[33m" + "ERRO ")
case INFO: case INFO:
log.golog.SetPrefix("INFO ") log.golog.SetPrefix("INFO ")
case NOTICE: case NOTICE:
log.golog.SetPrefix("NOTI ") log.golog.SetPrefix("NOTI ")
case DEBUG: case DEBUG:
log.golog.SetPrefix("DBUG ") log.golog.SetPrefix("DBUG ")
default: default:
log.golog.SetPrefix("UNKN ") log.golog.SetPrefix("UNKN ")
} }
if mylog == 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.golog.Output(calldepth, prestr+fmt.Sprintf(format+"\x1b[0m\n", v...)) log.golog.Output(calldepth, prestr+fmt.Sprintf(format+"\x1b[0m\n", v...))
} else if typ == NOTICE { } else if typ == NOTICE {
calldepth = calldepth + 2 calldepth = calldepth + 2
log.golog.Output(calldepth, prestr+fmt.Sprintf(format+"\n", v...)) log.golog.Output(calldepth, prestr+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(prestr+format+"\x1b[0m. Panic stack:\n%s\n", v...) panicstr := fmt.Sprintf(prestr+format+"\x1b[0m. Panic stack:\n%s\n", v...)
log.golog.Output(calldepth, panicstr) log.golog.Output(calldepth, panicstr)
panic(ErrLogPanic) panic(ErrLogPanic)
} else { } else {
log.golog.Output(calldepth, prestr+fmt.Sprintf(format+"\n", v...)) log.golog.Output(calldepth, prestr+fmt.Sprintf(format+"\n", v...))
} }
}
func formatHeader(calldepth int) string {
const MAX_LENGTH = 35 /* + 22 */
// log time
now := time.Now()
year, month, day := now.Date()
hour, min, sec := now.Clock()
nowstr := fmt.Sprintf("%4d%02d%02dT%02d:%02d:%02d.%03d ",
year, month, day, hour, min, sec, now.Nanosecond()/1e6)
// log position
_, file, line, ok := runtime.Caller(calldepth + 1)
if !ok {
file = "???"
line = 0
}
// Case 1: too short path, return directly
i, header, linestr := 0, "", fmt.Sprintf("%d", line)
if len(file)+len(linestr) < MAX_LENGTH {
header = file + ":" + linestr
return nowstr + header
}
// Case 2: filename long enough, just return filename
for i = len(file) - 1; i >= 0; i-- {
if file[i] == '/' {
header = file[i:] + ":" + linestr
i--
break
}
}
if len(header) >= MAX_LENGTH && i > 0 {
header = "…" + header[:4] + "…" + header[len(header)-MAX_LENGTH+6:]
return nowstr + header
} else if len(header) >= MAX_LENGTH && i <= 0 {
header = header[:4] + "…" + header[len(header)-MAX_LENGTH+5:]
return nowstr + header
}
// Case 3: try complex middle path with 'width=6'
tempheader := formatHeaderPath(file, i, 6) + header
if len(tempheader) > MAX_LENGTH {
tempheader = "…" + tempheader[len(tempheader)-MAX_LENGTH+1:]
return nowstr + tempheader
}
// Case 4: try complex middle path with 'width=8'
tempheader = formatHeaderPath(file, i, 8) + header
if len(tempheader) > MAX_LENGTH {
tempheader = "…" + tempheader[len(tempheader)-MAX_LENGTH+1:]
return nowstr + tempheader
}
// Case 5: try complex middle path with 'width=10'
tempheader = formatHeaderPath(file, i, 10) + header
if len(tempheader) > MAX_LENGTH {
tempheader = "…" + tempheader[len(tempheader)-MAX_LENGTH+1:]
return nowstr + tempheader
}
// Case 6: fallback
return nowstr + tempheader
}
func formatHeaderPath(file string, i, width int) (paths string) {
ii, iw := i, i
for i := ii; i >= 0; i-- {
if file[i] == '/' {
if iw-i > width {
// dirname lenght more then width
paths = file[i:i+width] + "." + paths
} else {
// dirname lenght less then width
paths = file[i:iw+1] + paths
}
//fmt.Println("w", iw-i, "width", width, "paths", paths)
if width > 2 {
// decrease width
width--
}
iw = i - 1
continue
}
}
return paths
} }
func needWriteLogToFile() bool { func needWriteLogToFile() bool {
if cls, ok := os.LookupEnv("QOOLOG_CLOSE_FILELOG"); ok && cls != "N" && cls != "n" { if cls, ok := os.LookupEnv("QOOLOG_CLOSE_FILELOG"); ok && cls != "N" && cls != "n" {
return false return false
} else if ink8s, ok := os.LookupEnv("KUBERNETES_PORT"); ok && ink8s != "" { } else if ink8s, ok := os.LookupEnv("KUBERNETES_PORT"); ok && ink8s != "" {
return false return false
} }
return true return true
} }
func needWriteLogToStdout() bool { func needWriteLogToStdout() bool {
if cls, ok := os.LookupEnv("QOOLOG_CLOSE_STDLOG"); ok && (cls == "Y" || cls == "y") { if cls, ok := os.LookupEnv("QOOLOG_CLOSE_STDLOG"); ok && (cls == "Y" || cls == "y") {
return false return false
} }
return true return true
} }
// ////////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////////
@ -336,38 +250,38 @@ var once sync.Once = sync.Once{}
// init // init
func init() { func init() {
once.Do(initlog) once.Do(initlog)
} }
// initlog // initlog
func initlog() { func initlog() {
// Step 1. create base writer & cache writer for ALL log // Step 1. create base writer & cache writer for ALL log
var ( var (
logDir = "./logs/" logDir = "./logs/"
logName = getExeFilename() logName = getExeFilename()
logDirsOptional = []string{"./log", "/var/log"} logDirsOptional = []string{"./log", "/var/log"}
logSplitTag = "20060102" //20060102150405 logSplitTag = "20060102" //20060102150405
baseWriter = NewBaseWriter(nil, os.Stdout, nil) baseWriter = NewBaseWriter(nil, os.Stdout, nil)
gologFlags = 0 /* log.Ldate | log.Lmicroseconds | log.Lshortfile */ gologFlags = log.Ldate | log.Lmicroseconds | log.Lshortfile
) )
// Step 2.1 add stdout to base writer if need // Step 2.1 add stdout to base writer if need
if needWriteLogToStdout() { if needWriteLogToStdout() {
baseWriter.stdw = os.Stdout baseWriter.stdw = os.Stdout
} }
// Step 2.2 rotate log(split log ) if need // Step 2.2 rotate log(split log ) if need
if needWriteLogToFile() { if needWriteLogToFile() {
baseWriter.OpenLogFile(logDir, logName, logDirsOptional) baseWriter.OpenLogFile(logDir, logName, logDirsOptional)
go func() { go func() {
for { for {
tag := time.Now().Local().Format(logSplitTag) tag := time.Now().Local().Format(logSplitTag)
baseWriter.TryBackupLogFile(tag) baseWriter.TryBackupLogFile(tag)
time.Sleep(2 * time.Minute) time.Sleep(2 * time.Minute)
} }
}() }()
} }
// Step 3. init golog(golang std logger) & mylog(myself defined logger) // Step 3. init golog(golang std logger) & mylog(myself defined logger)
golog = log.New(NewCacheWriter(baseWriter), "", gologFlags) golog = log.New(NewCacheWriter(baseWriter), "", gologFlags)
mylog = New("system") mylog = New("system")
} }