Skip to content

Commit

Permalink
增加配置panic级别的panic处理方法
Browse files Browse the repository at this point in the history
  • Loading branch information
xfali committed Sep 4, 2020
1 parent 5dc74ef commit 7d3fc82
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 50 deletions.
26 changes: 21 additions & 5 deletions logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,14 @@ type Logging interface {
Clone() Logging
}

type ExitFunc func(code int)
type PanicFunc func(interface{})

type logging struct {
timeFormatter func(t time.Time) string
callerFormatter func(file string, line int, funcName string) string
exitFunc func(code int)
exitFunc ExitFunc
panicFunc PanicFunc
formatter atomic.Value
colorFlag int
fileFlag int
Expand All @@ -185,6 +189,7 @@ func NewLogging(opts ...LoggingOpt) Logging {
timeFormatter: TimeFormat,
callerFormatter: CallerFormat,
exitFunc: os.Exit,
panicFunc: defaultPanic,
//formatter: nil,
colorFlag: DefaultColorFlag,
fileFlag: DefaultPrintFileFlag,
Expand Down Expand Up @@ -323,7 +328,7 @@ func (l *logging) Logf(level Level, depth int, keyValues KeyValues, format strin
l.format(w, level, depth, keyValues, logInfo)

if level == PANIC {
panic(logInfo)
l.panicFunc(NewKeyValues(KeyContent, logInfo))
} else if level <= FATAL {
l.processFatal(w)
}
Expand All @@ -341,7 +346,7 @@ func (l *logging) Log(level Level, depth int, keyValues KeyValues, args ...inter
l.format(w, level, depth, keyValues, logInfo)

if level == PANIC {
panic(logInfo)
l.panicFunc(NewKeyValues(KeyContent, logInfo))
} else if level <= FATAL {
l.processFatal(w)
}
Expand All @@ -357,7 +362,7 @@ func (l *logging) Logln(level Level, depth int, keyValues KeyValues, args ...int
l.format(w, level, depth, keyValues, logInfo)

if level == PANIC {
panic(logInfo)
l.panicFunc(NewKeyValues(KeyContent, logInfo))
} else if level <= FATAL {
l.processFatal(w)
}
Expand Down Expand Up @@ -542,12 +547,23 @@ func SetCallerFormatter(f func(file string, line int, funcName string) string) f
}

// 配置内置Logging Fatal退出处理函数
func SetExitFunc(f func(int)) func(*logging) {
func SetExitFunc(f ExitFunc) func(*logging) {
return func(logging *logging) {
logging.exitFunc = f
}
}

// 配置内置Logging Panic处理函数
func SetPanicFunc(f PanicFunc) func(*logging) {
return func(logging *logging) {
logging.panicFunc = f
}
}

func defaultPanic(v interface{}) {
panic(v)
}

// 配置内置Logging实现的颜色的标志,有AutoColor、DisableColor、ForceColor
func SetColorFlag(flag int) func(*logging) {
return func(logging *logging) {
Expand Down
160 changes: 115 additions & 45 deletions test/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,58 +11,128 @@ import (
)

func TestLoggerln(t *testing.T) {
// reset default init at first
// no fatal trace, do not exit
xlog.Init(xlog.NewLogging(xlog.SetFatalNoTrace(true), xlog.SetExitFunc(func(i int) {
t.Log("exit: ", i)
})))
log := xlog.New()
defer func() {
recover()
t.Log("panic !")
t.Run("default", func(t *testing.T) {
// reset default init at first
// no fatal trace, do not exit
xlog.Init(xlog.NewLogging(xlog.SetFatalNoTrace(true), xlog.SetExitFunc(func(i int) {
t.Log("exit: ", i)
})))
log := xlog.New()
defer func() {
v := recover()
if kvs, ok := v.(xlog.KeyValues); ok {
t.Log("recover panic !", kvs.GetAll())
}
log.Fatalln("this ia a Fatalln test")
}()
log.Debugln("this is a Debugln test")
log.Infoln("this is a Infoln test")
log.Warnln("this ia a Warnln test")
log.Errorln("this ia a Errorln test")
log.Panicln("this ia a Panicln test")
})

t.Run("exit and panic", func(t *testing.T) {
// reset default init at first
// no fatal trace, do not exit
xlog.Init(xlog.NewLogging(xlog.SetFatalNoTrace(true), xlog.SetExitFunc(func(i int) {
t.Log("exit: ", i)
}), xlog.SetPanicFunc(func(v interface{}) {
if kvs, ok := v.(xlog.KeyValues); ok {
t.Log("panic !", kvs.GetAll())
}
})))
log := xlog.New()
log.Debugln("this is a Debugln test")
log.Infoln("this is a Infoln test")
log.Warnln("this ia a Warnln test")
log.Errorln("this ia a Errorln test")
log.Panicln("this ia a Panicln test")
log.Fatalln("this ia a Fatalln test")
}()
log.Debugln("this is a Debugln test")
log.Infoln("this is a Infoln test")
log.Warnln("this ia a Warnln test")
log.Errorln("this ia a Errorln test")
log.Panicln("this ia a Panicln test")
})
}

func TestLoggerf(t *testing.T) {
// reset default init at first
// no fatal trace, do not exit
xlog.Init(xlog.NewLogging(xlog.SetFatalNoTrace(true), xlog.SetExitFunc(func(i int) {
t.Log("exit: ", i)
})))
log := xlog.New()
defer func() {
recover()
t.Log("panic !")
t.Run("default", func(t *testing.T) {
// reset default init at first
// no fatal trace, do not exit
xlog.Init(xlog.NewLogging(xlog.SetFatalNoTrace(true), xlog.SetExitFunc(func(i int) {
t.Log("exit: ", i)
})))
log := xlog.New()
defer func() {
v := recover()
if kvs, ok := v.(xlog.KeyValues); ok {
t.Log("panic !", kvs.GetAll())
}
log.Fatalf("recover this ia a Fatalf test")
}()
log.Debugf("this is a Debugf test")
log.Infof("this is a Infof test")
log.Warnf("this ia a Warnf test")
log.Errorf("this ia a Errorf test")
log.Panicf("this ia a Panicf test")
log.Fatalf("this ia a Fatalf test")
}()
log.Debugf("this is a Debugf test")
log.Infof("this is a Infof test")
log.Warnf("this ia a Warnf test")
log.Errorf("this ia a Errorf test")
log.Panicf("this ia a Panicf test")
})

t.Run("exit and panic", func(t *testing.T) {
// reset default init at first
// no fatal trace, do not exit
xlog.Init(xlog.NewLogging(xlog.SetFatalNoTrace(true), xlog.SetExitFunc(func(i int) {
t.Log("exit: ", i)
}), xlog.SetPanicFunc(func(v interface{}) {
if kvs, ok := v.(xlog.KeyValues); ok {
t.Log("panic !", kvs.GetAll())
}
})))
log := xlog.New()
log.Debugf("this is a Debugf test")
log.Infof("this is a Infof test")
log.Warnf("this ia a Warnf test")
log.Errorf("this ia a Errorf test")
log.Panicf("this ia a Panicf test")
log.Fatalf("this ia a Fatalln test")
})
}

func TestLogger(t *testing.T) {
// reset default init at first
// no fatal trace, do not exit
xlog.Init(xlog.NewLogging(xlog.SetFatalNoTrace(true), xlog.SetExitFunc(func(i int) {
t.Log("exit: ", i)
})))
log := xlog.New()
defer func() {
recover()
t.Log("panic !")
t.Run("default", func(t *testing.T) {
// reset default init at first
// no fatal trace, do not exit
xlog.Init(xlog.NewLogging(xlog.SetFatalNoTrace(true), xlog.SetExitFunc(func(i int) {
t.Log("exit: ", i)
})))
log := xlog.New()
defer func() {
v := recover()
if kvs, ok := v.(xlog.KeyValues); ok {
t.Log("recover panic !", kvs.GetAll())
}
log.Fatal("this ia a Fatal test")
}()
log.Debug("this is a Debug test")
log.Info("this is a Info test")
log.Warn("this ia a Warn test")
log.Error("this ia a Error test")
log.Panic("this ia a Panic test")
})

t.Run("exit and exit", func(t *testing.T) {
// reset default init at first
// no fatal trace, do not exit
xlog.Init(xlog.NewLogging(xlog.SetFatalNoTrace(true), xlog.SetExitFunc(func(i int) {
t.Log("exit: ", i)
}), xlog.SetPanicFunc(func(v interface{}) {
if kvs, ok := v.(xlog.KeyValues); ok {
t.Log("panic !", kvs.GetAll())
}
})))
log := xlog.New()
log.Debug("this is a Debug test")
log.Info("this is a Info test")
log.Warn("this ia a Warn test")
log.Error("this ia a Error test")
log.Panic("this ia a Panic test")
log.Fatal("this ia a Fatal test")
}()
log.Debug("this is a Debug test")
log.Info("this is a Info test")
log.Warn("this ia a Warn test")
log.Error("this ia a Error test")
log.Panic("this ia a Panic test")
})
}

0 comments on commit 7d3fc82

Please sign in to comment.