github.com/deroproject/derosuite@v2.1.6-1.0.20200307070847-0f2e589c7a2b+incompatible/globals/log.go (about) 1 package globals 2 3 import "os" 4 import "path" 5 6 import "github.com/romana/rlog" 7 import "github.com/sirupsen/logrus" 8 9 type RLOG_HOOK struct { // rlog HOOK 10 dummy string 11 formatter *logrus.TextFormatter 12 } 13 14 var HOOK RLOG_HOOK 15 16 // setup default logging to current directory 17 func Init_rlog() { 18 19 HOOK.formatter = new(logrus.TextFormatter) 20 HOOK.formatter.DisableColors = true 21 HOOK.formatter.DisableTimestamp = true 22 23 if os.Getenv("RLOG_LOG_LEVEL") == "" { 24 os.Setenv("RLOG_LOG_LEVEL", "WARN") // default logging in debug mode 25 } 26 27 if os.Getenv("RLOG_LOG_FILE") == "" { 28 exename, _ := os.Executable() 29 filename := path.Base(exename) + ".log" 30 os.Setenv("RLOG_LOG_FILE", filename) // default log file name 31 } 32 33 if os.Getenv("RLOG_LOG_STREAM") == "" { 34 os.Setenv("RLOG_LOG_STREAM", "NONE") // do not log to stdout/stderr 35 } 36 37 if os.Getenv("RLOG_CALLER_INFO") == "" { 38 os.Setenv("RLOG_CALLER_INFO", "RLOG_CALLER_INFO") // log caller info 39 } 40 41 //os.Setenv("RLOG_TRACE_LEVEL", "10") user can request tracing 42 //os.Setenv("RLOG_LOG_LEVEL", "DEBUG") 43 rlog.UpdateEnv() 44 45 } 46 47 // log logrus messages to rlog 48 func (hook *RLOG_HOOK) Fire(entry *logrus.Entry) error { 49 msg, err := hook.formatter.Format(entry) 50 if err == nil { 51 rlog.Infof(string(msg)) // log to file 52 } 53 return nil 54 } 55 56 // Levels returns configured log levels., we log everything 57 func (hook *RLOG_HOOK) Levels() []logrus.Level { 58 return logrus.AllLevels 59 }