Skip to content

Commit 68b72a8

Browse files
committed
Decimate go-logging & replace with logrus backend
License: MIT Signed-off-by: rht <[email protected]>
1 parent 5ba3ebf commit 68b72a8

File tree

1 file changed

+39
-41
lines changed

1 file changed

+39
-41
lines changed

util/log.go

+39-41
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,21 @@ package util
33
import (
44
"os"
55

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"
77
)
88

99
func init() {
1010
SetupLogging()
1111
}
1212

13-
var log = Logger("util")
14-
15-
var ansiGray = "\033[0;37m"
16-
var ansiBlue = "\033[0;34m"
13+
var log = logrus.New()
1714

1815
// 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},
2321
}
2422
var defaultLogFormat = "color"
2523

@@ -30,65 +28,67 @@ const (
3028
)
3129

3230
// loggers is the set of loggers in the system
33-
var loggers = map[string]*logging.Logger{}
31+
var loggers = map[string]*logrus.Entry{}
3432

3533
// SetupLogging will initialize the logger backend and set the flags.
3634
func SetupLogging() {
3735

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]
4139
}
4240

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
4643

47-
lvl := logging.ERROR
44+
lvl := logrus.ErrorLevel
4845

4946
if logenv := os.Getenv(envLogging); logenv != "" {
5047
var err error
51-
lvl, err = logging.LogLevel(logenv)
48+
lvl, err = logrus.ParseLevel(logenv)
5249
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(?)
5552
}
5653
}
5754

58-
Debug = GetenvBool("IPFS_DEBUG")
59-
if Debug {
60-
lvl = logging.DEBUG
55+
if Debug := GetenvBool("IPFS_DEBUG"); Debug {
56+
lvl = logrus.DebugLevel
6157
}
58+
lvl = logrus.DebugLevel
6259

6360
SetAllLoggers(lvl)
64-
6561
}
6662

67-
// SetDebugLogging calls SetAllLoggers with logging.DEBUG
63+
// SetDebugLogging calls SetAllLoggers with logrus.DebugLevel
6864
func SetDebugLogging() {
69-
SetAllLoggers(logging.DEBUG)
65+
SetAllLoggers(logrus.DebugLevel)
7066
}
7167

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)
68+
// SetAllLoggers changes the logrus.Level of all loggers to lvl
69+
func SetAllLoggers(lvl logrus.Level) {
70+
log.Level = lvl
71+
for _, logger := range loggers {
72+
logger.Level = lvl
7773
}
7874
}
7975

8076
// 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
77+
func Logger(name string) *logrus.Entry {
78+
if len(name) == 0 {
79+
log.Warnf("Missing name parameter")
80+
name = "undefined"
81+
}
82+
if _, ok := loggers[name]; !ok {
83+
loggers[name] = log.WithField("module", name)
84+
}
85+
return loggers[name]
8686
}
8787

8888
// SetLogLevel changes the log level of a specific subsystem
8989
// name=="*" changes all subsystems
9090
func SetLogLevel(name, level string) error {
91-
lvl, err := logging.LogLevel(level)
91+
lvl, err := logrus.ParseLevel(level)
9292
if err != nil {
9393
return err
9494
}
@@ -100,13 +100,11 @@ func SetLogLevel(name, level string) error {
100100
}
101101

102102
// Check if we have a logger by that name
103-
// logging.SetLevel() can't tell us...
104-
_, ok := loggers[name]
105-
if !ok {
103+
if _, ok := loggers[name]; !ok {
106104
return ErrNoSuchLogger
107105
}
108106

109-
logging.SetLevel(lvl, name)
107+
loggers[name].Level = lvl
110108

111109
return nil
112110
}

0 commit comments

Comments
 (0)