🔥A cross-language distributed transaction manager. Support xa, tcc, saga, transactional messages. 跨语言分布式事务管理器
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

78 lines
1.7 KiB

package logger
import (
"log"
"os"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
//var logger *zap.SugaredLogger = nil
var logger Logger = nil
func init() {
InitLog("info")
}
// Logger logger interface
type Logger interface {
Debugf(format string, args ...interface{})
Infof(format string, args ...interface{})
Warnf(format string, args ...interface{})
Errorf(format string, args ...interface{})
}
func WithLogger(log Logger) {
logger = log
}
// InitLog is an initialization for a logger
// level can be: debug info warn error
func InitLog(level string) {
config := zap.NewProductionConfig()
err := config.Level.UnmarshalText([]byte(level))
FatalIfError(err)
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))
FatalIfError(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
}
log.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)
}