This commit is contained in:
bryanqiu 2023-04-10 11:50:25 +08:00
parent e6efed682b
commit 2455ca35b2

36
log.go
View File

@ -29,6 +29,7 @@ const (
var (
golog *log.Logger = nil
mylog *Logger = nil
logWriter *cacheWriter = nil
logidCreator LogidCreator = nil
modulelogs map[string]*Logger = map[string]*Logger{}
)
@ -129,25 +130,29 @@ func (log *Logger) logwrite(typ logLevel, calldepth int, format string, v ...int
return
}
var idstr = log.GetLogidStr("[%s] ")
var (
idstr = log.GetLogidStr("[%s] ")
prestr = idstr + "[" + log.module + "] "
)
format = strings.Trim(format, "\n")
switch typ {
case PANIC:
log.golog.SetPrefix("\x1b[31m" + "PANIC " + idstr + "[" + log.module + "] ")
log.golog.SetPrefix("\x1b[31m" + "PANI ")
case FATAL:
log.golog.SetPrefix("\x1b[31m" + "FATAL " + idstr + "[" + log.module + "] ")
log.golog.SetPrefix("\x1b[31m" + "FATA ")
case WARNING:
log.golog.SetPrefix("\x1b[32m" + "WARNING " + idstr + "[" + log.module + "] ")
log.golog.SetPrefix("\x1b[32m" + "WARN ")
case ERROR:
log.golog.SetPrefix("\x1b[32m" + "ERROR " + idstr + "[" + log.module + "] ")
log.golog.SetPrefix("\x1b[32m" + "ERRO ")
case INFO:
log.golog.SetPrefix("INFO " + idstr + "[" + log.module + "] ")
log.golog.SetPrefix("INFO ")
case NOTICE:
log.golog.SetPrefix("NOTICE " + idstr + "[" + log.module + "] ")
log.golog.SetPrefix("NOTI ")
case DEBUG:
log.golog.SetPrefix("DEBUG " + idstr + "[" + log.module + "] ")
log.golog.SetPrefix("DBUG ")
default:
log.golog.SetPrefix("UNKNOWN " + idstr + "[" + log.module + "] ")
log.golog.SetPrefix("UNKN ")
}
if mylog == log {
@ -155,19 +160,19 @@ func (log *Logger) logwrite(typ logLevel, calldepth int, format string, v ...int
}
if typ == FATAL || typ == WARNING || typ == ERROR {
log.golog.Output(calldepth, fmt.Sprintf(format+"\x1b[0m\n", v...))
log.golog.Output(calldepth, prestr+fmt.Sprintf(format+"\x1b[0m\n", v...))
} else if typ == NOTICE {
calldepth = calldepth + 2
log.golog.Output(calldepth, fmt.Sprintf(format+"\n", v...))
log.golog.Output(calldepth, prestr+fmt.Sprintf(format+"\n", v...))
} else if typ == PANIC {
stack := strings.Replace(string(debug.Stack()), "\n", "\n== ", -1)
stack = str.SkipLine(stack, calldepth*2+1)
v = append(v, stack)
panicstr := fmt.Sprintf(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)
panic(ErrLogPanic)
} else {
log.golog.Output(calldepth, fmt.Sprintf(format+"\n", v...))
log.golog.Output(calldepth, prestr+fmt.Sprintf(format+"\n", v...))
}
}
@ -190,13 +195,12 @@ func initlog() {
logDirsOptional = []string{"./log", "/var/log"}
logSplitTag = "20060102" //20060102150405
baseWriter = NewBaseWriter(nil, os.Stdout, nil)
cacheWriter = NewCacheWriter(baseWriter)
gologFlags = log.Ldate | log.Lmicroseconds | log.Lshortfile
goSystemLog = log.New(cacheWriter, "", gologFlags)
)
// Step 2. init golog(golang std logger) & mylog(myself defined logger)
golog = goSystemLog
logWriter = NewCacheWriter(baseWriter)
golog = log.New(logWriter, "", gologFlags)
mylog = New("system")
baseWriter.OpenLogFile(logDir, logName, logDirsOptional)