Skip to content

Commit

Permalink
Decimate go-logging & replace with logrus backend
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: rht <[email protected]>
  • Loading branch information
rht committed Jun 12, 2015
1 parent 5ba3ebf commit 68b72a8
Showing 1 changed file with 39 additions and 41 deletions.
80 changes: 39 additions & 41 deletions util/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,21 @@ package util
import (
"os"

logging "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/whyrusleeping/go-logging"
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/Sirupsen/logrus"
)

func init() {
SetupLogging()
}

var log = Logger("util")

var ansiGray = "\033[0;37m"
var ansiBlue = "\033[0;34m"
var log = logrus.New()

// LogFormats is a map of formats used for our logger, keyed by name.
var LogFormats = map[string]string{
"nocolor": "%{time:2006-01-02 15:04:05.000000} %{level} %{module} %{shortfile}: %{message}",
"color": ansiGray + "%{time:15:04:05.000} %{color}%{level:5.5s} " + ansiBlue +
"%{module:10.10s}: %{color:reset}%{message} " + ansiGray + "%{shortfile}%{color:reset}",
// TODO: write custom TextFormatter (don't print module=name explicitly) and
// fork logrus to add shortfile
var LogFormats = map[string]*logrus.TextFormatter{
"nocolor": &logrus.TextFormatter{DisableColors: true, FullTimestamp: true, TimestampFormat: "2006-01-02 15:04:05.000000", DisableSorting: true},
"color": &logrus.TextFormatter{DisableColors: false, FullTimestamp: true, TimestampFormat: "15:04:05:000", DisableSorting: true},
}
var defaultLogFormat = "color"

Expand All @@ -30,65 +28,67 @@ const (
)

// loggers is the set of loggers in the system
var loggers = map[string]*logging.Logger{}
var loggers = map[string]*logrus.Entry{}

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

fmt := LogFormats[os.Getenv(envLoggingFmt)]
if fmt == "" {
fmt = LogFormats[defaultLogFormat]
format, ok := LogFormats[os.Getenv(envLoggingFmt)]
if !ok {
format = LogFormats[defaultLogFormat]
}

backend := logging.NewLogBackend(os.Stderr, "", 0)
logging.SetBackend(backend)
logging.SetFormatter(logging.MustStringFormatter(fmt))
log.Out = os.Stderr
log.Formatter = format

lvl := logging.ERROR
lvl := logrus.ErrorLevel

if logenv := os.Getenv(envLogging); logenv != "" {
var err error
lvl, err = logging.LogLevel(logenv)
lvl, err = logrus.ParseLevel(logenv)
if err != nil {
log.Debugf("logging.LogLevel() Error: %q", err)
lvl = logging.ERROR // reset to ERROR, could be undefined now(?)
log.Debugf("logrus.ParseLevel() Error: %q", err)
lvl = logrus.ErrorLevel // reset to ERROR, could be undefined now(?)
}
}

Debug = GetenvBool("IPFS_DEBUG")
if Debug {
lvl = logging.DEBUG
if Debug := GetenvBool("IPFS_DEBUG"); Debug {
lvl = logrus.DebugLevel
}
lvl = logrus.DebugLevel

SetAllLoggers(lvl)

}

// SetDebugLogging calls SetAllLoggers with logging.DEBUG
// SetDebugLogging calls SetAllLoggers with logrus.DebugLevel
func SetDebugLogging() {
SetAllLoggers(logging.DEBUG)
SetAllLoggers(logrus.DebugLevel)
}

// SetAllLoggers changes the logging.Level of all loggers to lvl
func SetAllLoggers(lvl logging.Level) {
logging.SetLevel(lvl, "")
for n := range loggers {
logging.SetLevel(lvl, n)
// SetAllLoggers changes the logrus.Level of all loggers to lvl
func SetAllLoggers(lvl logrus.Level) {
log.Level = lvl
for _, logger := range loggers {
logger.Level = lvl
}
}

// Logger retrieves a particular logger
func Logger(name string) *logging.Logger {
log := logging.MustGetLogger(name)
log.ExtraCalldepth = 1
loggers[name] = log
return log
func Logger(name string) *logrus.Entry {
if len(name) == 0 {
log.Warnf("Missing name parameter")
name = "undefined"
}
if _, ok := loggers[name]; !ok {
loggers[name] = log.WithField("module", name)
}
return loggers[name]
}

// SetLogLevel changes the log level of a specific subsystem
// name=="*" changes all subsystems
func SetLogLevel(name, level string) error {
lvl, err := logging.LogLevel(level)
lvl, err := logrus.ParseLevel(level)
if err != nil {
return err
}
Expand All @@ -100,13 +100,11 @@ func SetLogLevel(name, level string) error {
}

// Check if we have a logger by that name
// logging.SetLevel() can't tell us...
_, ok := loggers[name]
if !ok {
if _, ok := loggers[name]; !ok {
return ErrNoSuchLogger
}

logging.SetLevel(lvl, name)
loggers[name].Level = lvl

return nil
}

0 comments on commit 68b72a8

Please sign in to comment.