add log fatal level (#1067)

pull/1070/head
包子 3 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 ( import (
"context" "context"
"fmt" "fmt"
"os"
) )
// Helper is a logger helper. // Helper is a logger helper.
@ -89,3 +90,21 @@ func (h *Helper) Errorf(format string, a ...interface{}) {
func (h *Helper) Errorw(keyvals ...interface{}) { func (h *Helper) Errorw(keyvals ...interface{}) {
h.logger.Log(LevelError, keyvals...) 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 LevelWarn
// LevelError is logger error level. // LevelError is logger error level.
LevelError LevelError
// LevelFatal is logger fatal level
LevelFatal
) )
func (l Level) String() string { func (l Level) String() string {
@ -29,6 +31,8 @@ func (l Level) String() string {
return "WARN" return "WARN"
case LevelError: case LevelError:
return "ERROR" return "ERROR"
case LevelFatal:
return "FATAL"
default: default:
return "" return ""
} }
@ -45,6 +49,8 @@ func ParseLevel(s string) Level {
return LevelWarn return LevelWarn
case "ERROR": case "ERROR":
return LevelError return LevelError
case "FATAL":
return LevelFatal
} }
return LevelInfo return LevelInfo
} }

Loading…
Cancel
Save