add log fatal level (#1067)

pull/1070/head
包子 4 years ago committed by GitHub
parent 51a3a32502
commit 53522c4098
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 19
      log/helper.go
  2. 6
      log/level.go

@ -3,6 +3,7 @@ package log
import (
"context"
"fmt"
"os"
)
// Helper is a logger helper.
@ -89,3 +90,21 @@ func (h *Helper) Errorf(format string, a ...interface{}) {
func (h *Helper) Errorw(keyvals ...interface{}) {
h.logger.Log(LevelError, keyvals...)
}
// Fatal logs a message at fatal level.
func (h *Helper) Fatal(a ...interface{}) {
h.logger.Log(LevelFatal, "msg", fmt.Sprint(a...))
os.Exit(1)
}
// Fatalf logs a message at fatal level.
func (h *Helper) Fatalf(format string, a ...interface{}) {
h.logger.Log(LevelFatal, "msg", fmt.Sprintf(format, a...))
os.Exit(1)
}
// Fatalw logs a message at fatal level.
func (h *Helper) Fatalw(keyvals ...interface{}) {
h.logger.Log(LevelFatal, keyvals...)
os.Exit(1)
}

@ -17,6 +17,8 @@ const (
LevelWarn
// LevelError is logger error level.
LevelError
// LevelFatal is logger fatal level
LevelFatal
)
func (l Level) String() string {
@ -29,6 +31,8 @@ func (l Level) String() string {
return "WARN"
case LevelError:
return "ERROR"
case LevelFatal:
return "FATAL"
default:
return ""
}
@ -45,6 +49,8 @@ func ParseLevel(s string) Level {
return LevelWarn
case "ERROR":
return LevelError
case "FATAL":
return LevelFatal
}
return LevelInfo
}

Loading…
Cancel
Save