diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index 21e0cf149..1e79cfa4a 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -1,6 +1,6 @@ { "ImportPath": "github.com/coreos/etcd", - "GoVersion": "go1.4.2", + "GoVersion": "go1.4.1", "Packages": [ "./..." ], @@ -35,7 +35,7 @@ }, { "ImportPath": "github.com/coreos/pkg/capnslog", - "Rev": "9d5dd4632f9ece71bdf83d31253593a633e73df5" + "Rev": "84b359ff90d62d7b5a5b9dfb96400c08f0cc6642" }, { "ImportPath": "github.com/gogo/protobuf/proto", diff --git a/Godeps/_workspace/src/github.com/coreos/pkg/capnslog/example/hello_dolly.go b/Godeps/_workspace/src/github.com/coreos/pkg/capnslog/example/hello_dolly.go index 243b45a2e..91390f23b 100644 --- a/Godeps/_workspace/src/github.com/coreos/pkg/capnslog/example/hello_dolly.go +++ b/Godeps/_workspace/src/github.com/coreos/pkg/capnslog/example/hello_dolly.go @@ -1,27 +1,34 @@ package main import ( + "flag" oldlog "log" "os" - "github.com/coreos/etcd/Godeps/_workspace/src/github.com/coreos/pkg/capnslog" + "github.com/coreos/pkg/capnslog" ) +var logLevel = capnslog.INFO var log = capnslog.NewPackageLogger("github.com/coreos/pkg/capnslog/cmd", "main") var dlog = capnslog.NewPackageLogger("github.com/coreos/pkg/capnslog/cmd", "dolly") +func init() { + flag.Var(&logLevel, "log-level", "Global log level.") +} + func main() { rl := capnslog.MustRepoLogger("github.com/coreos/pkg/capnslog/cmd") capnslog.SetFormatter(capnslog.NewStringFormatter(os.Stderr)) // We can parse the log level configs from the command line - if len(os.Args) > 1 { - cfg, err := rl.ParseLogLevelConfig(os.Args[1]) + flag.Parse() + if flag.NArg() > 1 { + cfg, err := rl.ParseLogLevelConfig(flag.Arg(1)) if err != nil { log.Fatal(err) } rl.SetLogLevel(cfg) - log.Infof("Setting output to %s", os.Args[1]) + log.Infof("Setting output to %s", flag.Arg(1)) } // Send some messages at different levels to the different packages @@ -32,7 +39,7 @@ func main() { dlog.Tracef("I can tell, Dolly") // We also have control over the built-in "log" package. - capnslog.SetGlobalLogLevel(capnslog.INFO) + capnslog.SetGlobalLogLevel(logLevel) oldlog.Println("You're still glowin', you're still crowin', you're still lookin' strong") log.Fatalf("Dolly'll never go away again") } diff --git a/Godeps/_workspace/src/github.com/coreos/pkg/capnslog/formatters.go b/Godeps/_workspace/src/github.com/coreos/pkg/capnslog/formatters.go index 30cee6036..291372f55 100644 --- a/Godeps/_workspace/src/github.com/coreos/pkg/capnslog/formatters.go +++ b/Godeps/_workspace/src/github.com/coreos/pkg/capnslog/formatters.go @@ -9,7 +9,7 @@ import ( ) type Formatter interface { - Format(pkg string, level LogLevel, depth int, entries ...LogEntry) + Format(pkg string, level LogLevel, depth int, entries ...interface{}) Flush() } @@ -23,7 +23,7 @@ type StringFormatter struct { w *bufio.Writer } -func (s *StringFormatter) Format(pkg string, l LogLevel, i int, entries ...LogEntry) { +func (s *StringFormatter) Format(pkg string, l LogLevel, i int, entries ...interface{}) { now := time.Now() y, m, d := now.Date() h, min, sec := now.Clock() @@ -31,19 +31,13 @@ func (s *StringFormatter) Format(pkg string, l LogLevel, i int, entries ...LogEn s.writeEntries(pkg, l, i, entries...) } -func (s *StringFormatter) writeEntries(pkg string, _ LogLevel, _ int, entries ...LogEntry) { +func (s *StringFormatter) writeEntries(pkg string, _ LogLevel, _ int, entries ...interface{}) { if pkg != "" { s.w.WriteString(pkg + ": ") } - endsInNL := false - for i, v := range entries { - if i != 0 { - s.w.WriteByte(' ') - } - str := v.LogString() - endsInNL = strings.HasSuffix(str, "\n") - s.w.WriteString(str) - } + str := fmt.Sprint(entries...) + endsInNL := strings.HasSuffix(str, "\n") + s.w.WriteString(str) if !endsInNL { s.w.WriteString("\n") } diff --git a/Godeps/_workspace/src/github.com/coreos/pkg/capnslog/glog_formatter.go b/Godeps/_workspace/src/github.com/coreos/pkg/capnslog/glog_formatter.go index f668e7e3b..18692ebd6 100644 --- a/Godeps/_workspace/src/github.com/coreos/pkg/capnslog/glog_formatter.go +++ b/Godeps/_workspace/src/github.com/coreos/pkg/capnslog/glog_formatter.go @@ -23,7 +23,7 @@ func NewGlogFormatter(w io.Writer) *GlogFormatter { return g } -func (g GlogFormatter) Format(pkg string, level LogLevel, depth int, entries ...LogEntry) { +func (g GlogFormatter) Format(pkg string, level LogLevel, depth int, entries ...interface{}) { g.w.Write(GlogHeader(level, depth+1)) g.StringFormatter.Format(pkg, level, depth+1, entries...) } diff --git a/Godeps/_workspace/src/github.com/coreos/pkg/capnslog/log_hijack.go b/Godeps/_workspace/src/github.com/coreos/pkg/capnslog/log_hijack.go index 25369eec3..c075e47ff 100644 --- a/Godeps/_workspace/src/github.com/coreos/pkg/capnslog/log_hijack.go +++ b/Godeps/_workspace/src/github.com/coreos/pkg/capnslog/log_hijack.go @@ -20,6 +20,6 @@ func (p packageWriter) Write(b []byte) (int, error) { if p.pl.level < INFO { return 0, nil } - p.pl.internalLog(calldepth+2, INFO, BaseLogEntry(string(b))) + p.pl.internalLog(calldepth+2, INFO, string(b)) return len(b), nil } diff --git a/Godeps/_workspace/src/github.com/coreos/pkg/capnslog/logmap.go b/Godeps/_workspace/src/github.com/coreos/pkg/capnslog/logmap.go index 59d21dab1..60c113416 100644 --- a/Godeps/_workspace/src/github.com/coreos/pkg/capnslog/logmap.go +++ b/Godeps/_workspace/src/github.com/coreos/pkg/capnslog/logmap.go @@ -11,19 +11,19 @@ type LogLevel int8 const ( // CRITICAL is the lowest log level; only errors which will end the program will be propagated. - CRITICAL LogLevel = -1 + CRITICAL LogLevel = iota - 1 // ERROR is for errors that are not fatal but lead to troubling behavior. - ERROR = 0 + ERROR // WARNING is for errors which are not fatal and not errors, but are unusual. Often sourced from misconfigurations. - WARNING = 1 + WARNING // NOTICE is for normal but significant conditions. - NOTICE = 2 + NOTICE // INFO is a log level for common, everyday log updates. - INFO = 3 + INFO // DEBUG is the default hidden level for more verbose updates about internal processes. - DEBUG = 4 + DEBUG // TRACE is for (potentially) call by call tracing of programs. - TRACE = 5 + TRACE ) // Char returns a single-character representation of the log level. @@ -48,6 +48,39 @@ func (l LogLevel) Char() string { } } +// String returns a multi-character representation of the log level. +func (l LogLevel) String() string { + switch l { + case CRITICAL: + return "CRITICAL" + case ERROR: + return "ERROR" + case WARNING: + return "WARNING" + case NOTICE: + return "NOTICE" + case INFO: + return "INFO" + case DEBUG: + return "DEBUG" + case TRACE: + return "TRACE" + default: + panic("Unhandled loglevel") + } +} + +// Update using the given string value. Fulfills the flag.Value interface. +func (l *LogLevel) Set(s string) error { + value, err := ParseLevel(s) + if err != nil { + return err + } + + *l = value + return nil +} + // ParseLevel translates some potential loglevel strings into their corresponding levels. func ParseLevel(s string) (LogLevel, error) { switch s { @@ -71,13 +104,6 @@ func ParseLevel(s string) (LogLevel, error) { type RepoLogger map[string]*PackageLogger -// LogEntry is the generic interface for things which can be logged. -// Implementing the single method LogString() on your objects allows you to -// format them for logs/debugging as necessary. -type LogEntry interface { - LogString() string -} - type loggerStruct struct { sync.Mutex repoMap map[string]RepoLogger @@ -198,9 +224,3 @@ func NewPackageLogger(repo string, pkg string) (p *PackageLogger) { } return } - -type BaseLogEntry string - -func (b BaseLogEntry) LogString() string { - return string(b) -} diff --git a/Godeps/_workspace/src/github.com/coreos/pkg/capnslog/pkg_logger.go b/Godeps/_workspace/src/github.com/coreos/pkg/capnslog/pkg_logger.go index 6eebde473..d79f7a4e4 100644 --- a/Godeps/_workspace/src/github.com/coreos/pkg/capnslog/pkg_logger.go +++ b/Godeps/_workspace/src/github.com/coreos/pkg/capnslog/pkg_logger.go @@ -12,7 +12,10 @@ type PackageLogger struct { const calldepth = 3 -func (p *PackageLogger) internalLog(depth int, inLevel LogLevel, entries ...LogEntry) { +func (p *PackageLogger) internalLog(depth int, inLevel LogLevel, entries ...interface{}) { + if inLevel != CRITICAL && p.level < inLevel { + return + } logger.Lock() defer logger.Unlock() if logger.formatter != nil { @@ -24,148 +27,113 @@ func (p *PackageLogger) LevelAt(l LogLevel) bool { return p.level >= l } +// Log a formatted string at any level between ERROR and TRACE +func (p *PackageLogger) Logf(l LogLevel, format string, args ...interface{}) { + p.internalLog(calldepth, l, fmt.Sprintf(format, args...)) +} + +// Log a message at any level between ERROR and TRACE +func (p *PackageLogger) Log(l LogLevel, args ...interface{}) { + p.internalLog(calldepth, l, fmt.Sprint(args...)) +} + // log stdlib compatibility func (p *PackageLogger) Println(args ...interface{}) { - if p.level < INFO { - return - } - p.internalLog(calldepth, INFO, BaseLogEntry(fmt.Sprintln(args...))) + p.internalLog(calldepth, INFO, fmt.Sprintln(args...)) } func (p *PackageLogger) Printf(format string, args ...interface{}) { - if p.level < INFO { - return - } - p.internalLog(calldepth, INFO, BaseLogEntry(fmt.Sprintf(format, args...))) + p.internalLog(calldepth, INFO, fmt.Sprintf(format, args...)) } func (p *PackageLogger) Print(args ...interface{}) { - if p.level < INFO { - return - } - p.internalLog(calldepth, INFO, BaseLogEntry(fmt.Sprint(args...))) + p.internalLog(calldepth, INFO, fmt.Sprint(args...)) } // Panic and fatal func (p *PackageLogger) Panicf(format string, args ...interface{}) { s := fmt.Sprintf(format, args...) - p.internalLog(calldepth, CRITICAL, BaseLogEntry(s)) + p.internalLog(calldepth, CRITICAL, s) panic(s) } func (p *PackageLogger) Panic(args ...interface{}) { s := fmt.Sprint(args...) - p.internalLog(calldepth, CRITICAL, BaseLogEntry(s)) + p.internalLog(calldepth, CRITICAL, s) panic(s) } func (p *PackageLogger) Fatalf(format string, args ...interface{}) { s := fmt.Sprintf(format, args...) - p.internalLog(calldepth, CRITICAL, BaseLogEntry(s)) + p.internalLog(calldepth, CRITICAL, s) os.Exit(1) } func (p *PackageLogger) Fatal(args ...interface{}) { s := fmt.Sprint(args...) - p.internalLog(calldepth, CRITICAL, BaseLogEntry(s)) + p.internalLog(calldepth, CRITICAL, s) os.Exit(1) } // Error Functions func (p *PackageLogger) Errorf(format string, args ...interface{}) { - if p.level < ERROR { - return - } - p.internalLog(calldepth, ERROR, BaseLogEntry(fmt.Sprintf(format, args...))) + p.internalLog(calldepth, ERROR, fmt.Sprintf(format, args...)) } -func (p *PackageLogger) Error(entries ...LogEntry) { - if p.level < ERROR { - return - } +func (p *PackageLogger) Error(entries ...interface{}) { p.internalLog(calldepth, ERROR, entries...) } // Warning Functions func (p *PackageLogger) Warningf(format string, args ...interface{}) { - if p.level < WARNING { - return - } - p.internalLog(calldepth, WARNING, BaseLogEntry(fmt.Sprintf(format, args...))) + p.internalLog(calldepth, WARNING, fmt.Sprintf(format, args...)) } -func (p *PackageLogger) Warning(entries ...LogEntry) { - if p.level < WARNING { - return - } +func (p *PackageLogger) Warning(entries ...interface{}) { p.internalLog(calldepth, WARNING, entries...) } // Notice Functions func (p *PackageLogger) Noticef(format string, args ...interface{}) { - if p.level < NOTICE { - return - } - p.internalLog(calldepth, NOTICE, BaseLogEntry(fmt.Sprintf(format, args...))) + p.internalLog(calldepth, NOTICE, fmt.Sprintf(format, args...)) } -func (p *PackageLogger) Notice(entries ...LogEntry) { - if p.level < NOTICE { - return - } +func (p *PackageLogger) Notice(entries ...interface{}) { p.internalLog(calldepth, NOTICE, entries...) } // Info Functions func (p *PackageLogger) Infof(format string, args ...interface{}) { - if p.level < INFO { - return - } - p.internalLog(calldepth, INFO, BaseLogEntry(fmt.Sprintf(format, args...))) + p.internalLog(calldepth, INFO, fmt.Sprintf(format, args...)) } -func (p *PackageLogger) Info(entries ...LogEntry) { - if p.level < INFO { - return - } +func (p *PackageLogger) Info(entries ...interface{}) { p.internalLog(calldepth, INFO, entries...) } // Debug Functions func (p *PackageLogger) Debugf(format string, args ...interface{}) { - if p.level < DEBUG { - return - } - p.internalLog(calldepth, DEBUG, BaseLogEntry(fmt.Sprintf(format, args...))) + p.internalLog(calldepth, DEBUG, fmt.Sprintf(format, args...)) } -func (p *PackageLogger) Debug(entries ...LogEntry) { - if p.level < DEBUG { - return - } +func (p *PackageLogger) Debug(entries ...interface{}) { p.internalLog(calldepth, DEBUG, entries...) } // Trace Functions func (p *PackageLogger) Tracef(format string, args ...interface{}) { - if p.level < TRACE { - return - } - p.internalLog(calldepth, TRACE, BaseLogEntry(fmt.Sprintf(format, args...))) + p.internalLog(calldepth, TRACE, fmt.Sprintf(format, args...)) } -func (p *PackageLogger) Trace(entries ...LogEntry) { - if p.level < TRACE { - return - } +func (p *PackageLogger) Trace(entries ...interface{}) { p.internalLog(calldepth, TRACE, entries...) } diff --git a/Godeps/_workspace/src/github.com/coreos/pkg/capnslog/syslog_formatter.go b/Godeps/_workspace/src/github.com/coreos/pkg/capnslog/syslog_formatter.go index 40afa6220..b0ec62087 100644 --- a/Godeps/_workspace/src/github.com/coreos/pkg/capnslog/syslog_formatter.go +++ b/Godeps/_workspace/src/github.com/coreos/pkg/capnslog/syslog_formatter.go @@ -1,6 +1,7 @@ package capnslog import ( + "fmt" "log/syslog" ) @@ -20,9 +21,9 @@ type syslogFormatter struct { w *syslog.Writer } -func (s *syslogFormatter) Format(pkg string, l LogLevel, _ int, entries ...LogEntry) { +func (s *syslogFormatter) Format(pkg string, l LogLevel, _ int, entries ...interface{}) { for _, entry := range entries { - str := entry.LogString() + str := fmt.Sprint(entry) switch l { case CRITICAL: s.w.Crit(str)