add Infof & logLevel

This commit is contained in:
bryanqiu 2022-11-15 15:19:18 +08:00
parent 997627c1b8
commit 71b00c8a37

98
log.go
View File

@ -30,7 +30,9 @@ var (
const ErrLogPanic = "~~~~panic~~~~~~"
//////////////global log//////////////////begin///////////////////
func SetLogLevel(newlv logLevel) (oldlv logLevel) {
return mylogger.SetLogLevel(newlv)
}
func PrintPretty(prefix string, v interface{}) {
mylogger.PrintPretty(prefix, v)
}
@ -46,6 +48,9 @@ func Noticef(format string, v ...interface{}) {
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...)
}
@ -60,9 +65,23 @@ func Fatalf(format string, v ...interface{}) {
}
//////////////////////////////////////////////////////////////////
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 {
module string
logger *log.Logger
module string
loglevel logLevel
logger *log.Logger
}
func New(module string) (logger *Logger) {
@ -72,52 +91,67 @@ func New(module string) (logger *Logger) {
if _, ok := modulelogs[module]; ok {
return modulelogs[module]
} else {
modulelogs[module] = &Logger{module: module, logger: glogger}
modulelogs[module] = &Logger{
module: module,
logger: glogger,
loglevel: DEBUG,
}
return modulelogs[module]
}
}
func (log *Logger) SetLogLevel(newlv logLevel) (oldlv logLevel) {
oldlv = log.loglevel
log.loglevel = newlv
return oldlv
}
func (log *Logger) Print(prefix string, v interface{}) {
preety, _ := json.Marshal(v)
log.logwrite("DEBUGE", 3, prefix+"%s\n", preety)
log.logwrite(DEBUG, 3, prefix+"%s\n", preety)
}
func (log *Logger) PrintPretty(prefix string, v interface{}) {
preety, _ := json.MarshalIndent(v, "==", " ")
log.logwrite("DEBUGE", 3, prefix+"%s\n", preety)
log.logwrite(DEBUG, 3, prefix+"%s\n", preety)
}
func (log *Logger) Debugf(format string, v ...interface{}) {
log.logwrite("DEBUGE", 3, format, v...)
log.logwrite(DEBUG, 3, format, v...)
}
func (log *Logger) DebugfWithDepth(calldepth int, format string, v ...interface{}) {
calldepth += 3
log.logwrite("DEBUGE", calldepth, format, v...)
log.logwrite(DEBUG, calldepth, format, v...)
}
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{}) {
calldepth += 3
log.logwrite("NOTICE", calldepth, format, v...)
log.logwrite(NOTICE, calldepth, format, v...)
}
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{}) {
log.logwrite(INFO, 3, format, v...)
}
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{}) {
log.logwrite("FATAL", 3, format, v...)
log.logwrite(FATAL, 3, format, v...)
}
func (log *Logger) Panicf(format string, v ...interface{}) {
log.logwrite("PANIC", 3, format, v...)
log.logwrite(PANIC, 3, format, v...)
}
func GetLogid() string {
@ -133,7 +167,11 @@ func SetLogid(logid string) {
}
}
func (log *Logger) logwrite(typ string, calldepth int, format string, v ...interface{}) {
func (log *Logger) logwrite(typ logLevel, calldepth int, format string, v ...interface{}) {
if typ > log.loglevel {
return
}
var idstr = ""
if id := GetLogid(); id != "" {
idstr = "[" + id + "] "
@ -141,32 +179,34 @@ func (log *Logger) logwrite(typ string, calldepth int, format string, v ...inter
format = strings.Trim(format, "\n")
switch typ {
case "PANIC":
case PANIC:
log.logger.SetPrefix("\x1b[31m" + "PANIC " + idstr + "[" + log.module + "] ")
case "FATAL":
log.logger.SetPrefix("\x1b[31m" + "FATAL " + idstr + "[" + log.module + "] ")
case "WARNING":
log.logger.SetPrefix("\x1b[32m" + "WARNING " + idstr + "[" + log.module + "] ")
case "ERROR":
case FATAL:
log.logger.SetPrefix("\x1b[31m" + "FATAL " + idstr + "[" + log.module + "] ")
case WARNING:
log.logger.SetPrefix("\x1b[32m" + "WARNING " + idstr + "[" + log.module + "] ")
case ERROR:
log.logger.SetPrefix("\x1b[32m" + "ERROR " + idstr + "[" + log.module + "] ")
case "NOTICE":
log.logger.SetPrefix("NOTICE " + idstr + "[" + log.module + "] ")
case "DEBUGE":
log.logger.SetPrefix("DEBUGE " + idstr + "[" + log.module + "] ")
case INFO:
log.logger.SetPrefix("INFO " + idstr + "[" + log.module + "] ")
case NOTICE:
log.logger.SetPrefix("NOTICE " + idstr + "[" + log.module + "] ")
case DEBUG:
log.logger.SetPrefix("DEBUG " + idstr + "[" + log.module + "] ")
default:
log.logger.SetPrefix("UNKNOWN " + idstr + "[" + log.module + "] ")
log.logger.SetPrefix("UNKNOWN " + idstr + "[" + log.module + "] ")
}
if mylogger == log {
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...))
} else if typ == "NOTICE" {
} else if typ == NOTICE {
calldepth = calldepth + 2
log.logger.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 = str.SkipLine(stack, calldepth*2+1)
v = append(v, stack)