Browse Source

Fatal is changed

pull/123/head
yedf2 4 years ago
parent
commit
7283f40f7e
  1. 63
      dtmcli/logger/log.go
  2. 17
      dtmcli/logger/logger_test.go

63
dtmcli/logger/log.go

@ -0,0 +1,63 @@
package logger
import (
"log"
"os"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var logger *zap.SugaredLogger = nil
func init() {
InitLog()
}
// InitLog is a initialization for a logger
func InitLog() {
config := zap.NewProductionConfig()
config.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
if os.Getenv("DTM_DEBUG") != "" {
config.Encoding = "console"
config.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
}
p, err := config.Build(zap.AddCallerSkip(1))
if err != nil {
log.Fatal("create logger failed: ", err)
}
logger = p.Sugar()
}
// Debugf log to level debug
func Debugf(fmt string, args ...interface{}) {
logger.Debugf(fmt, args...)
}
// Infof log to level info
func Infof(fmt string, args ...interface{}) {
logger.Infof(fmt, args...)
}
// Warnf log to level warn
func Warnf(fmt string, args ...interface{}) {
logger.Warnf(fmt, args...)
}
// Errorf log to level error
func Errorf(fmt string, args ...interface{}) {
logger.Errorf(fmt, args...)
}
// FatalfIf log to level error
func FatalfIf(cond bool, fmt string, args ...interface{}) {
if !cond {
return
}
logger.Fatalf(fmt, args...)
}
// FatalIfError if err is not nil, then log to level fatal and call os.Exit
func FatalIfError(err error) {
FatalfIf(err != nil, "fatal error: %v", err)
}

17
dtmcli/logger/logger_test.go

@ -0,0 +1,17 @@
package logger
import (
"os"
"testing"
)
func TestInitLog(t *testing.T) {
os.Setenv("DTM_DEBUG", "1")
InitLog()
Debugf("a debug msg")
Infof("a info msg")
Warnf("a warn msg")
Errorf("a error msg")
FatalfIf(false, "nothing")
FatalIfError(nil)
}
Loading…
Cancel
Save