From b1c301e3f5b784822fa9e4de16fb154d73b122ed Mon Sep 17 00:00:00 2001 From: cupen Date: Fri, 22 May 2020 19:41:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A5=E5=BF=97=E6=94=B9=E7=94=A8=20stderr?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 6 +++--- config/config.go | 4 ++-- go.mod | 2 ++ listener/listener.go | 9 ++++----- listener/notify/bearychat.go | 4 ++-- listener/notify/init.go | 8 ++++---- supervisor-event-listener.go | 4 ++-- tests/supervisor-app.ini | 1 + utils/{tmpfslog => errlog}/log.go | 16 +++++++++++----- 9 files changed, 31 insertions(+), 23 deletions(-) rename utils/{tmpfslog => errlog}/log.go (78%) diff --git a/Makefile b/Makefile index 5614c83..e2df72d 100644 --- a/Makefile +++ b/Makefile @@ -8,12 +8,12 @@ install: test-integration: - sudo supervisorctl stop supervisor-event-listener go build + sudo supervisorctl stop supervisor-event-listener sudo cp ./supervisor-event-listener /usr/local/bin/ sudo cp ./tests/supervisor-app.ini /etc/supervisor.d/ - sudo supervisorctl start supervisor-event-listener - sudo supervisorctl update + sudo supervisorctl remove supervisor-event-listener + sudo supervisorctl update supervisor-event-listener sudo supervisorctl start sleep-then-exit diff --git a/config/config.go b/config/config.go index 8955229..9a04493 100644 --- a/config/config.go +++ b/config/config.go @@ -6,7 +6,7 @@ import ( "strings" "github.com/ouqiang/supervisor-event-listener/utils" - "github.com/ouqiang/supervisor-event-listener/utils/tmpfslog" + "github.com/ouqiang/supervisor-event-listener/utils/errlog" "gopkg.in/ini.v1" ) @@ -66,7 +66,7 @@ func ParseConfig(configFile string) *Config { config := &Config{} config.NotifyType = notifyType - tmpfslog.Info("notifyType: %+v\n", config.NotifyType) + errlog.Info("notifyType: %+v\n", config.NotifyType) switch notifyType { case "mail": config.MailServer = parseMailServer(section) diff --git a/go.mod b/go.mod index 427e87a..5f309f5 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,7 @@ module github.com/ouqiang/supervisor-event-listener +go 1.14 + require ( github.com/go-gomail/gomail v0.0.0-20160411212932-81ebce5c23df github.com/smartystreets/goconvey v0.0.0-20190330032615-68dc04aab96a // indirect diff --git a/listener/listener.go b/listener/listener.go index 1eaad2c..399a0a4 100644 --- a/listener/listener.go +++ b/listener/listener.go @@ -9,7 +9,7 @@ import ( "github.com/ouqiang/supervisor-event-listener/event" "github.com/ouqiang/supervisor-event-listener/listener/notify" - "github.com/ouqiang/supervisor-event-listener/utils/tmpfslog" + "github.com/ouqiang/supervisor-event-listener/utils/errlog" ) var ( @@ -33,13 +33,13 @@ func listen() { for { ready() header, err := readHeader(reader) - tmpfslog.Debug("header:%+v err:%+v", header, err) + errlog.Info("header:%+v err:%+v", header, err) if err != nil { failure(err) continue } payload, err := readPayload(reader, header.Len) - tmpfslog.Debug("payloadL%+v err:%+v", payload, err) + errlog.Info("payloadL%+v err:%+v", payload, err) if err != nil { failure(err) continue @@ -82,7 +82,6 @@ func readPayload(reader *bufio.Reader, payloadLen int) (*event.Payload, error) { if err != nil { return nil, err } - return payload, nil } @@ -96,5 +95,5 @@ func success() { func failure(err error) { fmt.Fprintln(os.Stderr, err) - fmt.Fprint(os.Stdout, "Result 2\nFAIL") + fmt.Fprint(os.Stdout, "RESULT 2\nFAIL") } diff --git a/listener/notify/bearychat.go b/listener/notify/bearychat.go index b72c9fb..d2bc92a 100644 --- a/listener/notify/bearychat.go +++ b/listener/notify/bearychat.go @@ -4,8 +4,8 @@ import ( "encoding/json" "github.com/ouqiang/supervisor-event-listener/event" + "github.com/ouqiang/supervisor-event-listener/utils/errlog" "github.com/ouqiang/supervisor-event-listener/utils/httpclient" - "github.com/ouqiang/supervisor-event-listener/utils/tmpfslog" ) type BearyChat struct{} @@ -28,7 +28,7 @@ func (this *BearyChat) Send(msg event.Message) error { } resp := httpclient.PostJson(url, string(body), timeout) if !resp.IsOK() { - tmpfslog.Error("params: %v err: %v", params, resp.Error()) + errlog.Error("params: %v err: %v", params, resp.Error()) return resp.Error() } return nil diff --git a/listener/notify/init.go b/listener/notify/init.go index 24f3eeb..5860b4a 100644 --- a/listener/notify/init.go +++ b/listener/notify/init.go @@ -5,7 +5,7 @@ import ( "github.com/ouqiang/supervisor-event-listener/config" "github.com/ouqiang/supervisor-event-listener/event" - "github.com/ouqiang/supervisor-event-listener/utils/tmpfslog" + "github.com/ouqiang/supervisor-event-listener/utils/errlog" "fmt" "os" @@ -21,7 +21,7 @@ var ( ) func Init(fpath string) error { - tmpfslog.Info("loading config: %s", fpath) + errlog.Info("loading config: %s", fpath) if Conf != nil { return fmt.Errorf("init twice!!!") } @@ -34,7 +34,7 @@ func Init(fpath string) error { func Reload() error { fpath := confFilePath - tmpfslog.Info("loading config: %s", fpath) + errlog.Info("loading config: %s", fpath) Conf = config.ParseConfig(fpath) return nil } @@ -68,7 +68,7 @@ func handleSignal(sig os.Signal) error { } func handleMessage(msg event.Message) error { - tmpfslog.Debug("message: %+v\n", msg) + errlog.Debug("message: %+v\n", msg) var notifyHandler Notifiable switch Conf.NotifyType { case "mail": diff --git a/supervisor-event-listener.go b/supervisor-event-listener.go index 0bdb797..910fe8c 100644 --- a/supervisor-event-listener.go +++ b/supervisor-event-listener.go @@ -7,7 +7,7 @@ import ( "github.com/ouqiang/supervisor-event-listener/listener" "github.com/ouqiang/supervisor-event-listener/listener/notify" - "github.com/ouqiang/supervisor-event-listener/utils/tmpfslog" + "github.com/ouqiang/supervisor-event-listener/utils/errlog" ) func main() { @@ -25,7 +25,7 @@ func main() { flag.Parse() err := notify.Init(configFile) if err != nil { - tmpfslog.Error("notify init failed. err: %+v", err) + errlog.Error("notify init failed. err: %+v", err) os.Exit(127) } if dryRun { diff --git a/tests/supervisor-app.ini b/tests/supervisor-app.ini index ec0b2a3..60de31b 100644 --- a/tests/supervisor-app.ini +++ b/tests/supervisor-app.ini @@ -2,6 +2,7 @@ command=/usr/local/bin/supervisor-event-listener -c /etc/supervisor-event-listener.ini events= + TICK_60, PROCESS_STATE_EXITED, PROCESS_STATE_FATAL, PROCESS_STATE_STOPPED, diff --git a/utils/tmpfslog/log.go b/utils/errlog/log.go similarity index 78% rename from utils/tmpfslog/log.go rename to utils/errlog/log.go index 5078398..8467ac5 100644 --- a/utils/tmpfslog/log.go +++ b/utils/errlog/log.go @@ -1,8 +1,10 @@ -package tmpfslog +package errlog import ( "fmt" "os" + "path" + "runtime" "time" ) @@ -26,12 +28,12 @@ var LEVELS_NAME = map[int]string{ ALL: "all", } -var f *os.File +var f = os.Stderr var curLogLevel = INFO func init() { - fpath := "/tmp/supervisor-event-listener.log" - f = newLogFile(fpath) + // fpath := "/tmp/supervisor-event-listener.log" + // f = newLogFile(fpath) } func newLogFile(fpath string) *os.File { @@ -47,9 +49,13 @@ func log(level int, _fmt string, args ...interface{}) { if level > curLogLevel { return } + + _, fn, lineno, _ := runtime.Caller(2) + fn = path.Base(fn) now := time.Now() levelName := LEVELS_NAME[level] - prefix := fmt.Sprintf("%s [%s]: ", now.Format(time.RFC3339), levelName) + prefix := fmt.Sprintf("%s [%s] %s:%d: ", + now.Format(time.RFC3339), levelName, fn, lineno) f.WriteString(prefix) f.WriteString(fmt.Sprintf(_fmt, args...)) f.WriteString("\n")