add Infof & logLevel
This commit is contained in:
parent
997627c1b8
commit
71b00c8a37
86
log.go
86
log.go
@ -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,8 +65,22 @@ 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
|
||||
loglevel logLevel
|
||||
logger *log.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,18 +179,20 @@ 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":
|
||||
case FATAL:
|
||||
log.logger.SetPrefix("\x1b[31m" + "FATAL " + idstr + "[" + log.module + "] ")
|
||||
case "WARNING":
|
||||
case WARNING:
|
||||
log.logger.SetPrefix("\x1b[32m" + "WARNING " + idstr + "[" + log.module + "] ")
|
||||
case "ERROR":
|
||||
case ERROR:
|
||||
log.logger.SetPrefix("\x1b[32m" + "ERROR " + idstr + "[" + log.module + "] ")
|
||||
case "NOTICE":
|
||||
case INFO:
|
||||
log.logger.SetPrefix("INFO " + idstr + "[" + log.module + "] ")
|
||||
case NOTICE:
|
||||
log.logger.SetPrefix("NOTICE " + idstr + "[" + log.module + "] ")
|
||||
case "DEBUGE":
|
||||
log.logger.SetPrefix("DEBUGE " + idstr + "[" + log.module + "] ")
|
||||
case DEBUG:
|
||||
log.logger.SetPrefix("DEBUG " + idstr + "[" + log.module + "] ")
|
||||
default:
|
||||
log.logger.SetPrefix("UNKNOWN " + idstr + "[" + log.module + "] ")
|
||||
}
|
||||
@ -161,12 +201,12 @@ func (log *Logger) logwrite(typ string, calldepth int, format string, v ...inter
|
||||
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)
|
||||
|
Loading…
Reference in New Issue
Block a user