Skip to content

Commit

Permalink
Ftr: add fatal method for logger(1.5 branch) (#1483)
Browse files Browse the repository at this point in the history
* feat: add fatal method & union test

* fix: replace xml to yml

* feat: test fatal by redo test
  • Loading branch information
cjphaha committed Oct 6, 2021
1 parent 874f43d commit 1dbb573
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
2 changes: 2 additions & 0 deletions common/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@ type Logger interface {
Warn(args ...interface{})
Error(args ...interface{})
Debug(args ...interface{})
Fatal(args ...interface{})

Infof(fmt string, args ...interface{})
Warnf(fmt string, args ...interface{})
Errorf(fmt string, args ...interface{})
Debugf(fmt string, args ...interface{})
Fatalf(fmt string, args ...interface{})
}

func init() {
Expand Down
34 changes: 34 additions & 0 deletions common/logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package logger

import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"testing"
Expand Down Expand Up @@ -81,3 +83,35 @@ func TestSetLevel(t *testing.T) {
Debug("debug")
Info("info")
}

func TestFatal(t *testing.T) {
err := InitLog("./log.yml")
assert.NoError(t, err)
if os.Getenv("BE_Fatal") == "1" {
Fatal("fool")
return
}
cmd := exec.Command(os.Args[0], "-test.run=TestFatal")
cmd.Env = append(os.Environ(), "BE_Fatal=1")
err = cmd.Run()
if e, ok := err.(*exec.ExitError); ok && !e.Success() {
return
}
t.Fatalf("process ran with err %v, want exit status 1", err)
}

func TestFatalf(t *testing.T) {
err := InitLog("./log.yml")
assert.NoError(t, err)
if os.Getenv("BE_Fatalf") == "1" {
Fatalf("%s", "foolf")
return
}
cmd := exec.Command(os.Args[0], "-test.run=TestFatalf")
cmd.Env = append(os.Environ(), "BE_Fatalf=1")
err = cmd.Run()
if e, ok := err.(*exec.ExitError); ok && !e.Success() {
return
}
t.Fatalf("process ran with err %v, want exit status 1", err)
}
10 changes: 10 additions & 0 deletions common/logger/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,13 @@ func Errorf(fmt string, args ...interface{}) {
func Debugf(fmt string, args ...interface{}) {
logger.Debugf(fmt, args...)
}

// Fatal logs a message, then calls os.Exit.
func Fatal(args ...interface{}) {
logger.Fatal(args...)
}

// Fatalf logs a templated message, then calls os.Exit.
func Fatalf(fmt string, args ...interface{}) {
logger.Fatalf(fmt, args...)
}

0 comments on commit 1dbb573

Please sign in to comment.