Ensure inlined logs
Move the LogJSON check into the log function so that the caller function can be inlined. This is helpful for hot functions like `log.Debug` where it's likely that the `-vv` flag is not set thus the to avoid the extra function call.
This commit is contained in:
parent
5f41a687bf
commit
517bc57e9c
@ -93,6 +93,27 @@ func log(level int, tag, color string, formatted bool, format string, args ...in
|
||||
if Level < level {
|
||||
return
|
||||
}
|
||||
var msg string
|
||||
if formatted {
|
||||
msg = fmt.Sprintf(format, args...)
|
||||
} else {
|
||||
msg = fmt.Sprint(args...)
|
||||
}
|
||||
if LogJSON {
|
||||
switch tag {
|
||||
case "ERRO":
|
||||
logger.Error(msg)
|
||||
case "FATA":
|
||||
logger.Fatal(msg)
|
||||
case "WARN":
|
||||
logger.Warn(msg)
|
||||
case "DEBU":
|
||||
logger.Debug(msg)
|
||||
default:
|
||||
logger.Info(msg)
|
||||
}
|
||||
return
|
||||
}
|
||||
s := []byte(time.Now().Format("2006/01/02 15:04:05"))
|
||||
s = append(s, ' ')
|
||||
if tty {
|
||||
@ -105,11 +126,7 @@ func log(level int, tag, color string, formatted bool, format string, args ...in
|
||||
s = append(s, "\x1b[0m"...)
|
||||
}
|
||||
s = append(s, ' ')
|
||||
if formatted {
|
||||
s = append(s, fmt.Sprintf(format, args...)...)
|
||||
} else {
|
||||
s = append(s, fmt.Sprint(args...)...)
|
||||
}
|
||||
s = append(s, msg...)
|
||||
if s[len(s)-1] != '\n' {
|
||||
s = append(s, '\n')
|
||||
}
|
||||
@ -123,10 +140,6 @@ var emptyFormat string
|
||||
// Infof ...
|
||||
func Infof(format string, args ...interface{}) {
|
||||
if Level >= 1 {
|
||||
if LogJSON {
|
||||
logger.Infof(format, args...)
|
||||
return
|
||||
}
|
||||
log(1, "INFO", "\x1b[36m", true, format, args...)
|
||||
}
|
||||
}
|
||||
@ -134,10 +147,6 @@ func Infof(format string, args ...interface{}) {
|
||||
// Info ...
|
||||
func Info(args ...interface{}) {
|
||||
if Level >= 1 {
|
||||
if LogJSON {
|
||||
logger.Info(args...)
|
||||
return
|
||||
}
|
||||
log(1, "INFO", "\x1b[36m", false, emptyFormat, args...)
|
||||
}
|
||||
}
|
||||
@ -145,10 +154,6 @@ func Info(args ...interface{}) {
|
||||
// HTTPf ...
|
||||
func HTTPf(format string, args ...interface{}) {
|
||||
if Level >= 1 {
|
||||
if LogJSON {
|
||||
logger.Infof(format, args...)
|
||||
return
|
||||
}
|
||||
log(1, "HTTP", "\x1b[1m\x1b[30m", true, format, args...)
|
||||
}
|
||||
}
|
||||
@ -156,10 +161,6 @@ func HTTPf(format string, args ...interface{}) {
|
||||
// HTTP ...
|
||||
func HTTP(args ...interface{}) {
|
||||
if Level >= 1 {
|
||||
if LogJSON {
|
||||
logger.Info(args...)
|
||||
return
|
||||
}
|
||||
log(1, "HTTP", "\x1b[1m\x1b[30m", false, emptyFormat, args...)
|
||||
}
|
||||
}
|
||||
@ -167,10 +168,6 @@ func HTTP(args ...interface{}) {
|
||||
// Errorf ...
|
||||
func Errorf(format string, args ...interface{}) {
|
||||
if Level >= 1 {
|
||||
if LogJSON {
|
||||
logger.Errorf(format, args...)
|
||||
return
|
||||
}
|
||||
log(1, "ERRO", "\x1b[1m\x1b[31m", true, format, args...)
|
||||
}
|
||||
}
|
||||
@ -178,10 +175,6 @@ func Errorf(format string, args ...interface{}) {
|
||||
// Error ..
|
||||
func Error(args ...interface{}) {
|
||||
if Level >= 1 {
|
||||
if LogJSON {
|
||||
logger.Errorf(emptyFormat, args...)
|
||||
return
|
||||
}
|
||||
log(1, "ERRO", "\x1b[1m\x1b[31m", false, emptyFormat, args...)
|
||||
}
|
||||
}
|
||||
@ -189,10 +182,6 @@ func Error(args ...interface{}) {
|
||||
// Warnf ...
|
||||
func Warnf(format string, args ...interface{}) {
|
||||
if Level >= 1 {
|
||||
if LogJSON {
|
||||
logger.Warnf(format, args...)
|
||||
return
|
||||
}
|
||||
log(2, "WARN", "\x1b[33m", true, format, args...)
|
||||
}
|
||||
}
|
||||
@ -200,10 +189,6 @@ func Warnf(format string, args ...interface{}) {
|
||||
// Warn ...
|
||||
func Warn(args ...interface{}) {
|
||||
if Level >= 1 {
|
||||
if LogJSON {
|
||||
logger.Warnf(emptyFormat, args...)
|
||||
return
|
||||
}
|
||||
log(2, "WARN", "\x1b[33m", false, emptyFormat, args...)
|
||||
}
|
||||
}
|
||||
@ -211,10 +196,6 @@ func Warn(args ...interface{}) {
|
||||
// Debugf ...
|
||||
func Debugf(format string, args ...interface{}) {
|
||||
if Level >= 3 {
|
||||
if LogJSON {
|
||||
logger.Debugf(format, args...)
|
||||
return
|
||||
}
|
||||
log(3, "DEBU", "\x1b[35m", true, format, args...)
|
||||
}
|
||||
}
|
||||
@ -222,10 +203,6 @@ func Debugf(format string, args ...interface{}) {
|
||||
// Debug ...
|
||||
func Debug(args ...interface{}) {
|
||||
if Level >= 3 {
|
||||
if LogJSON {
|
||||
logger.Debugf(emptyFormat, args...)
|
||||
return
|
||||
}
|
||||
log(3, "DEBU", "\x1b[35m", false, emptyFormat, args...)
|
||||
}
|
||||
}
|
||||
@ -242,23 +219,12 @@ func Print(args ...interface{}) {
|
||||
|
||||
// Fatalf ...
|
||||
func Fatalf(format string, args ...interface{}) {
|
||||
if LogJSON {
|
||||
logger.Fatalf(format, args...)
|
||||
os.Exit(1)
|
||||
return
|
||||
}
|
||||
|
||||
log(1, "FATA", "\x1b[31m", true, format, args...)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Fatal ...
|
||||
func Fatal(args ...interface{}) {
|
||||
if LogJSON {
|
||||
logger.Fatalf(emptyFormat, args...)
|
||||
os.Exit(1)
|
||||
return
|
||||
}
|
||||
log(1, "FATA", "\x1b[31m", false, emptyFormat, args...)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user