Commit 9ceca989 authored by rht's avatar rht

Decimate go-logging & replace with logrus backend

License: MIT
Signed-off-by: default avatarrht <rhtbot@gmail.com>
parent 84f974ef
......@@ -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"
......@@ -30,65 +28,66 @@ 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
}
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
}
......@@ -100,13 +99,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
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment