Decimate go-logging & replace with logrus backend
License: MIT Signed-off-by: rht <rhtbot@gmail.com>
rht committed
Jun 12, 2015 at 07:16 UTC
9ceca9896c1dbea3cc1d9c931cf165745a5a5973
1 file changed
+38
-41
util/log.go
+38
-41
@@ -3,23 +3,21 @@ package util
3
import (
4
"os"
5
6
- logging "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/whyrusleeping/go-logging"
6
+ "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/Sirupsen/logrus"
7
)
8
9
func init() {
10
SetupLogging()
11
}
12
13
-var log = Logger("util")
14
-
15
-var ansiGray = "\033[0;37m"
16
-var ansiBlue = "\033[0;34m"
13
+var log = logrus.New()
14
15
// LogFormats is a map of formats used for our logger, keyed by name.
19
-var LogFormats = map[string]string{
20
- "nocolor": "%{time:2006-01-02 15:04:05.000000} %{level} %{module} %{shortfile}: %{message}",
21
- "color": ansiGray + "%{time:15:04:05.000} %{color}%{level:5.5s} " + ansiBlue +
22
- "%{module:10.10s}: %{color:reset}%{message} " + ansiGray + "%{shortfile}%{color:reset}",
16
+// TODO: write custom TextFormatter (don't print module=name explicitly) and
17
+// fork logrus to add shortfile
18
+var LogFormats = map[string]*logrus.TextFormatter{
19
+ "nocolor": &logrus.TextFormatter{DisableColors: true, FullTimestamp: true, TimestampFormat: "2006-01-02 15:04:05.000000", DisableSorting: true},
20
+ "color": &logrus.TextFormatter{DisableColors: false, FullTimestamp: true, TimestampFormat: "15:04:05:000", DisableSorting: true},
21
}
22
var defaultLogFormat = "color"
23
@@ -30,65 +28,66 @@ const (
28
)
29
30
// loggers is the set of loggers in the system
33
-var loggers = map[string]*logging.Logger{}
31
+var loggers = map[string]*logrus.Entry{}
32
33
// SetupLogging will initialize the logger backend and set the flags.
34
func SetupLogging() {
35
38
- fmt := LogFormats[os.Getenv(envLoggingFmt)]
39
- if fmt == "" {
40
- fmt = LogFormats[defaultLogFormat]
36
+ format, ok := LogFormats[os.Getenv(envLoggingFmt)]
37
+ if !ok {
38
+ format = LogFormats[defaultLogFormat]
39
}
40
43
- backend := logging.NewLogBackend(os.Stderr, "", 0)
44
- logging.SetBackend(backend)
45
- logging.SetFormatter(logging.MustStringFormatter(fmt))
41
+ log.Out = os.Stderr
42
+ log.Formatter = format
43
47
- lvl := logging.ERROR
44
+ lvl := logrus.ErrorLevel
45
46
if logenv := os.Getenv(envLogging); logenv != "" {
47
var err error
51
- lvl, err = logging.LogLevel(logenv)
48
+ lvl, err = logrus.ParseLevel(logenv)
49
if err != nil {
53
- log.Debugf("logging.LogLevel() Error: %q", err)
54
- lvl = logging.ERROR // reset to ERROR, could be undefined now(?)
50
+ log.Debugf("logrus.ParseLevel() Error: %q", err)
51
+ lvl = logrus.ErrorLevel // reset to ERROR, could be undefined now(?)
52
}
53
}
54
58
- Debug = GetenvBool("IPFS_DEBUG")
59
- if Debug {
60
- lvl = logging.DEBUG
55
+ if Debug := GetenvBool("IPFS_DEBUG"); Debug {
56
+ lvl = logrus.DebugLevel
57
}
58
59
SetAllLoggers(lvl)
64
-
60
}
61
67
-// SetDebugLogging calls SetAllLoggers with logging.DEBUG
62
+// SetDebugLogging calls SetAllLoggers with logrus.DebugLevel
63
func SetDebugLogging() {
69
- SetAllLoggers(logging.DEBUG)
64
+ SetAllLoggers(logrus.DebugLevel)
65
}
66
72
-// SetAllLoggers changes the logging.Level of all loggers to lvl
73
-func SetAllLoggers(lvl logging.Level) {
74
- logging.SetLevel(lvl, "")
75
- for n := range loggers {
76
- logging.SetLevel(lvl, n)
67
+// SetAllLoggers changes the logrus.Level of all loggers to lvl
68
+func SetAllLoggers(lvl logrus.Level) {
69
+ log.Level = lvl
70
+ for _, logger := range loggers {
71
+ logger.Level = lvl
72
}
73
}
74
75
// Logger retrieves a particular logger
81
-func Logger(name string) *logging.Logger {
82
- log := logging.MustGetLogger(name)
83
- log.ExtraCalldepth = 1
84
- loggers[name] = log
85
- return log
76
+func Logger(name string) *logrus.Entry {
77
+ if len(name) == 0 {
78
+ log.Warnf("Missing name parameter")
79
+ name = "undefined"
80
+ }
81
+ if _, ok := loggers[name]; !ok {
82
+ loggers[name] = log.WithField("module", name)
83
+ }
84
+ return loggers[name]
85
}
86
87
// SetLogLevel changes the log level of a specific subsystem
88
// name=="*" changes all subsystems
89
func SetLogLevel(name, level string) error {
91
- lvl, err := logging.LogLevel(level)
90
+ lvl, err := logrus.ParseLevel(level)
91
if err != nil {
92
return err
93
}
@@ -100,13 +99,11 @@ func SetLogLevel(name, level string) error {
99
}
100
101
// Check if we have a logger by that name
103
- // logging.SetLevel() can't tell us...
104
- _, ok := loggers[name]
105
- if !ok {
102
+ if _, ok := loggers[name]; !ok {
103
return ErrNoSuchLogger
104
}
105
109
- logging.SetLevel(lvl, name)
106
+ loggers[name].Level = lvl
107
108
return nil
109
}