github.com/annchain/OG@v0.0.9/debug/debuglog/dev.go (about) 1 package debuglog 2 3 import ( 4 "fmt" 5 "github.com/bshuster-repo/logrus-logstash-hook" 6 "github.com/sirupsen/logrus" 7 "net" 8 "strconv" 9 ) 10 11 // All stucts that need call InitDefault() before being used 12 type NeedInitDefault interface { 13 InitDefault() 14 } 15 16 type NodeLogger struct { 17 Logger *logrus.Logger 18 } 19 20 type AddPeerLogHook struct { 21 Id string 22 } 23 24 func (a AddPeerLogHook) Levels() []logrus.Level { 25 return logrus.AllLevels 26 } 27 28 func (a AddPeerLogHook) Fire(e *logrus.Entry) error { 29 e.Message = fmt.Sprintf("[%s] ", a.Id) + e.Message 30 return nil 31 } 32 33 func SetupOrderedLog(id int) *logrus.Logger { 34 logger := logrus.New() 35 logger.Hooks.Add(AddPeerLogHook{Id: strconv.Itoa(id)}) 36 // for socket debugging 37 conn, err := net.Dial("tcp", "127.0.0.1:1088") 38 if err != nil { 39 logrus.Warn("socket logger is not enabled") 40 } 41 hook := logrustash.New(conn, logrustash.DefaultFormatter(logrus.Fields{"type": "myappName"})) 42 43 logger.Hooks.Add(hook) 44 45 logger.SetLevel(logrus.TraceLevel) 46 logger.SetFormatter(&logrus.TextFormatter{ 47 ForceColors: true, 48 TimestampFormat: "15:04:05.000000", 49 }) 50 51 return logger 52 }