diff --git a/core/logx/logs.go b/core/logx/logs.go index 3347d333..98b57b27 100644 --- a/core/logx/logs.go +++ b/core/logx/logs.go @@ -43,6 +43,7 @@ const ( consoleMode = "console" volumeMode = "volume" + levelAlert = "alert" levelInfo = "info" levelError = "error" levelSevere = "severe" @@ -121,6 +122,10 @@ func SetUp(c LogConf) error { } } +func Alert(v string) { + output(errorLog, levelAlert, v) +} + func Close() error { if writeConsole { return nil diff --git a/core/logx/logs_test.go b/core/logx/logs_test.go index e1330673..ad2cf04d 100644 --- a/core/logx/logs_test.go +++ b/core/logx/logs_test.go @@ -84,6 +84,14 @@ func TestFileLineConsoleMode(t *testing.T) { assert.True(t, writer.Contains(fmt.Sprintf("%s:%d", file, line+1))) } +func TestStructedLogAlert(t *testing.T) { + doTestStructedLog(t, levelAlert, func(writer io.WriteCloser) { + errorLog = writer + }, func(v ...interface{}) { + Alert(fmt.Sprint(v...)) + }) +} + func TestStructedLogInfo(t *testing.T) { doTestStructedLog(t, levelInfo, func(writer io.WriteCloser) { infoLog = writer diff --git a/core/stat/alert.go b/core/stat/alert.go index 620dae29..61993954 100644 --- a/core/stat/alert.go +++ b/core/stat/alert.go @@ -11,6 +11,7 @@ import ( "time" "github.com/tal-tech/go-zero/core/executors" + "github.com/tal-tech/go-zero/core/logx" "github.com/tal-tech/go-zero/core/proc" "github.com/tal-tech/go-zero/core/sysx" "github.com/tal-tech/go-zero/core/timex" @@ -23,7 +24,7 @@ const ( ) var ( - reporter func(string) + reporter = logx.Alert lock sync.RWMutex lessExecutor = executors.NewLessExecutor(time.Minute * 5) dropped int32 diff --git a/example/fx/square.go b/example/fx/square.go index 20fa9b61..6bcedbf8 100644 --- a/example/fx/square.go +++ b/example/fx/square.go @@ -10,7 +10,6 @@ func main() { result, err := fx.From(func(source chan<- interface{}) { for i := 0; i < 10; i++ { source <- i - source <- i } }).Map(func(item interface{}) interface{} { i := item.(int)