@cryptotaxi247 / kubo / commits / ebdaac7f1

introduce IPFS_DEBUG env var

Henry committed Oct 14, 2014 at 09:52 UTC ebdaac7f1ef40d2e1563150c5440c9a26713b61a
2 files changed +22 -7
util/log.go
+14 -7
@@ -11,6 +11,8 @@ func init() {
11 SetupLogging()
12 }
13
14 +var log = Logger("util")
15 +
16 // LogFormat is the format used for our logger.
17 var LogFormat = "%{color}%{time:2006-01-02 15:04:05.999999} %{shortfile} %{level}: %{color:reset}%{message}"
18
@@ -28,32 +30,37 @@ func SetupLogging() {
30 logging.SetBackend(backend)
31 logging.SetFormatter(logging.MustStringFormatter(LogFormat))
32
31 - lvl := logging.ERROR
33 + // always prnt critical and error?
34 + SetAllLoggers(logging.CRITICAL)
35 + SetAllLoggers(logging.ERROR)
36
33 - var err error
37 if logenv := os.Getenv("IPFS_LOGGING"); logenv != "" {
35 - lvl, err = logging.LogLevel(logenv)
38 + lvl, err := logging.LogLevel(logenv)
39 if err != nil {
40 log.Error("invalid logging level: %s\n", logenv)
38 - lvl = logging.DEBUG
41 + } else {
42 + SetAllLoggers(lvl)
43 }
44 }
45
42 - SetAllLoggers(lvl)
46 + if GetenvBool("IPFS_DEBUG") {
47 + SetAllLoggers(logging.DEBUG)
48 + }
49 +
50 }
51
52 +// SetAllLoggers changes the logging.Level of all loggers to lvl
53 func SetAllLoggers(lvl logging.Level) {
54 logging.SetLevel(lvl, "")
55 for n, log := range loggers {
56 logging.SetLevel(lvl, n)
49 - log.Error("setting logger: %s to %v", n, lvl)
57 + log.Debug("setting logger: %s to %v", n, lvl)
58 }
59 }
60
61 // Logger retrieves a particular logger + initializes it at a particular level
62 func Logger(name string) *logging.Logger {
63 log := logging.MustGetLogger(name)
56 - // logging.SetLevel(lvl, name) // can't set level here.
64 loggers[name] = log
65 return log
66 }
util/util.go
+8
@@ -4,7 +4,9 @@ import (
4 "errors"
5 "io"
6 "math/rand"
7 + "os"
8 "path/filepath"
9 + "strings"
10 "time"
11
12 ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go"
@@ -121,3 +123,9 @@ func (r *randGen) Read(p []byte) (n int, err error) {
123
124 panic("unreachable")
125 }
126 +
127 +// GetenvBool is the way to check an env var as a boolean
128 +func GetenvBool(name string) bool {
129 + v := strings.ToLower(os.Getenv(name))
130 + return v == "true" || v != "t" || v == "1"
131 +}