Godeps: update logger pkg

release-2.1
Xiang Li 2015-06-01 14:51:21 -07:00
parent 8825af47a0
commit 89f6f988cb
8 changed files with 98 additions and 108 deletions

4
Godeps/Godeps.json generated
View File

@ -1,6 +1,6 @@
{ {
"ImportPath": "github.com/coreos/etcd", "ImportPath": "github.com/coreos/etcd",
"GoVersion": "go1.4.2", "GoVersion": "go1.4.1",
"Packages": [ "Packages": [
"./..." "./..."
], ],
@ -35,7 +35,7 @@
}, },
{ {
"ImportPath": "github.com/coreos/pkg/capnslog", "ImportPath": "github.com/coreos/pkg/capnslog",
"Rev": "9d5dd4632f9ece71bdf83d31253593a633e73df5" "Rev": "84b359ff90d62d7b5a5b9dfb96400c08f0cc6642"
}, },
{ {
"ImportPath": "github.com/gogo/protobuf/proto", "ImportPath": "github.com/gogo/protobuf/proto",

View File

@ -1,27 +1,34 @@
package main package main
import ( import (
"flag"
oldlog "log" oldlog "log"
"os" "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 log = capnslog.NewPackageLogger("github.com/coreos/pkg/capnslog/cmd", "main")
var dlog = capnslog.NewPackageLogger("github.com/coreos/pkg/capnslog/cmd", "dolly") var dlog = capnslog.NewPackageLogger("github.com/coreos/pkg/capnslog/cmd", "dolly")
func init() {
flag.Var(&logLevel, "log-level", "Global log level.")
}
func main() { func main() {
rl := capnslog.MustRepoLogger("github.com/coreos/pkg/capnslog/cmd") rl := capnslog.MustRepoLogger("github.com/coreos/pkg/capnslog/cmd")
capnslog.SetFormatter(capnslog.NewStringFormatter(os.Stderr)) capnslog.SetFormatter(capnslog.NewStringFormatter(os.Stderr))
// We can parse the log level configs from the command line // We can parse the log level configs from the command line
if len(os.Args) > 1 { flag.Parse()
cfg, err := rl.ParseLogLevelConfig(os.Args[1]) if flag.NArg() > 1 {
cfg, err := rl.ParseLogLevelConfig(flag.Arg(1))
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
rl.SetLogLevel(cfg) 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 // Send some messages at different levels to the different packages
@ -32,7 +39,7 @@ func main() {
dlog.Tracef("I can tell, Dolly") dlog.Tracef("I can tell, Dolly")
// We also have control over the built-in "log" package. // 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") oldlog.Println("You're still glowin', you're still crowin', you're still lookin' strong")
log.Fatalf("Dolly'll never go away again") log.Fatalf("Dolly'll never go away again")
} }

View File

@ -9,7 +9,7 @@ import (
) )
type Formatter interface { type Formatter interface {
Format(pkg string, level LogLevel, depth int, entries ...LogEntry) Format(pkg string, level LogLevel, depth int, entries ...interface{})
Flush() Flush()
} }
@ -23,7 +23,7 @@ type StringFormatter struct {
w *bufio.Writer 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() now := time.Now()
y, m, d := now.Date() y, m, d := now.Date()
h, min, sec := now.Clock() 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...) 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 != "" { if pkg != "" {
s.w.WriteString(pkg + ": ") s.w.WriteString(pkg + ": ")
} }
endsInNL := false str := fmt.Sprint(entries...)
for i, v := range entries { endsInNL := strings.HasSuffix(str, "\n")
if i != 0 { s.w.WriteString(str)
s.w.WriteByte(' ')
}
str := v.LogString()
endsInNL = strings.HasSuffix(str, "\n")
s.w.WriteString(str)
}
if !endsInNL { if !endsInNL {
s.w.WriteString("\n") s.w.WriteString("\n")
} }

View File

@ -23,7 +23,7 @@ func NewGlogFormatter(w io.Writer) *GlogFormatter {
return g 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.w.Write(GlogHeader(level, depth+1))
g.StringFormatter.Format(pkg, level, depth+1, entries...) g.StringFormatter.Format(pkg, level, depth+1, entries...)
} }

View File

@ -20,6 +20,6 @@ func (p packageWriter) Write(b []byte) (int, error) {
if p.pl.level < INFO { if p.pl.level < INFO {
return 0, nil return 0, nil
} }
p.pl.internalLog(calldepth+2, INFO, BaseLogEntry(string(b))) p.pl.internalLog(calldepth+2, INFO, string(b))
return len(b), nil return len(b), nil
} }

View File

@ -11,19 +11,19 @@ type LogLevel int8
const ( const (
// CRITICAL is the lowest log level; only errors which will end the program will be propagated. // 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 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 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 is for normal but significant conditions.
NOTICE = 2 NOTICE
// INFO is a log level for common, everyday log updates. // 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 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 is for (potentially) call by call tracing of programs.
TRACE = 5 TRACE
) )
// Char returns a single-character representation of the log level. // 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. // ParseLevel translates some potential loglevel strings into their corresponding levels.
func ParseLevel(s string) (LogLevel, error) { func ParseLevel(s string) (LogLevel, error) {
switch s { switch s {
@ -71,13 +104,6 @@ func ParseLevel(s string) (LogLevel, error) {
type RepoLogger map[string]*PackageLogger 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 { type loggerStruct struct {
sync.Mutex sync.Mutex
repoMap map[string]RepoLogger repoMap map[string]RepoLogger
@ -198,9 +224,3 @@ func NewPackageLogger(repo string, pkg string) (p *PackageLogger) {
} }
return return
} }
type BaseLogEntry string
func (b BaseLogEntry) LogString() string {
return string(b)
}

View File

@ -12,7 +12,10 @@ type PackageLogger struct {
const calldepth = 3 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() logger.Lock()
defer logger.Unlock() defer logger.Unlock()
if logger.formatter != nil { if logger.formatter != nil {
@ -24,148 +27,113 @@ func (p *PackageLogger) LevelAt(l LogLevel) bool {
return p.level >= l 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 // log stdlib compatibility
func (p *PackageLogger) Println(args ...interface{}) { func (p *PackageLogger) Println(args ...interface{}) {
if p.level < INFO { p.internalLog(calldepth, INFO, fmt.Sprintln(args...))
return
}
p.internalLog(calldepth, INFO, BaseLogEntry(fmt.Sprintln(args...)))
} }
func (p *PackageLogger) Printf(format string, args ...interface{}) { func (p *PackageLogger) Printf(format string, args ...interface{}) {
if p.level < INFO { p.internalLog(calldepth, INFO, fmt.Sprintf(format, args...))
return
}
p.internalLog(calldepth, INFO, BaseLogEntry(fmt.Sprintf(format, args...)))
} }
func (p *PackageLogger) Print(args ...interface{}) { func (p *PackageLogger) Print(args ...interface{}) {
if p.level < INFO { p.internalLog(calldepth, INFO, fmt.Sprint(args...))
return
}
p.internalLog(calldepth, INFO, BaseLogEntry(fmt.Sprint(args...)))
} }
// Panic and fatal // Panic and fatal
func (p *PackageLogger) Panicf(format string, args ...interface{}) { func (p *PackageLogger) Panicf(format string, args ...interface{}) {
s := fmt.Sprintf(format, args...) s := fmt.Sprintf(format, args...)
p.internalLog(calldepth, CRITICAL, BaseLogEntry(s)) p.internalLog(calldepth, CRITICAL, s)
panic(s) panic(s)
} }
func (p *PackageLogger) Panic(args ...interface{}) { func (p *PackageLogger) Panic(args ...interface{}) {
s := fmt.Sprint(args...) s := fmt.Sprint(args...)
p.internalLog(calldepth, CRITICAL, BaseLogEntry(s)) p.internalLog(calldepth, CRITICAL, s)
panic(s) panic(s)
} }
func (p *PackageLogger) Fatalf(format string, args ...interface{}) { func (p *PackageLogger) Fatalf(format string, args ...interface{}) {
s := fmt.Sprintf(format, args...) s := fmt.Sprintf(format, args...)
p.internalLog(calldepth, CRITICAL, BaseLogEntry(s)) p.internalLog(calldepth, CRITICAL, s)
os.Exit(1) os.Exit(1)
} }
func (p *PackageLogger) Fatal(args ...interface{}) { func (p *PackageLogger) Fatal(args ...interface{}) {
s := fmt.Sprint(args...) s := fmt.Sprint(args...)
p.internalLog(calldepth, CRITICAL, BaseLogEntry(s)) p.internalLog(calldepth, CRITICAL, s)
os.Exit(1) os.Exit(1)
} }
// Error Functions // Error Functions
func (p *PackageLogger) Errorf(format string, args ...interface{}) { func (p *PackageLogger) Errorf(format string, args ...interface{}) {
if p.level < ERROR { p.internalLog(calldepth, ERROR, fmt.Sprintf(format, args...))
return
}
p.internalLog(calldepth, ERROR, BaseLogEntry(fmt.Sprintf(format, args...)))
} }
func (p *PackageLogger) Error(entries ...LogEntry) { func (p *PackageLogger) Error(entries ...interface{}) {
if p.level < ERROR {
return
}
p.internalLog(calldepth, ERROR, entries...) p.internalLog(calldepth, ERROR, entries...)
} }
// Warning Functions // Warning Functions
func (p *PackageLogger) Warningf(format string, args ...interface{}) { func (p *PackageLogger) Warningf(format string, args ...interface{}) {
if p.level < WARNING { p.internalLog(calldepth, WARNING, fmt.Sprintf(format, args...))
return
}
p.internalLog(calldepth, WARNING, BaseLogEntry(fmt.Sprintf(format, args...)))
} }
func (p *PackageLogger) Warning(entries ...LogEntry) { func (p *PackageLogger) Warning(entries ...interface{}) {
if p.level < WARNING {
return
}
p.internalLog(calldepth, WARNING, entries...) p.internalLog(calldepth, WARNING, entries...)
} }
// Notice Functions // Notice Functions
func (p *PackageLogger) Noticef(format string, args ...interface{}) { func (p *PackageLogger) Noticef(format string, args ...interface{}) {
if p.level < NOTICE { p.internalLog(calldepth, NOTICE, fmt.Sprintf(format, args...))
return
}
p.internalLog(calldepth, NOTICE, BaseLogEntry(fmt.Sprintf(format, args...)))
} }
func (p *PackageLogger) Notice(entries ...LogEntry) { func (p *PackageLogger) Notice(entries ...interface{}) {
if p.level < NOTICE {
return
}
p.internalLog(calldepth, NOTICE, entries...) p.internalLog(calldepth, NOTICE, entries...)
} }
// Info Functions // Info Functions
func (p *PackageLogger) Infof(format string, args ...interface{}) { func (p *PackageLogger) Infof(format string, args ...interface{}) {
if p.level < INFO { p.internalLog(calldepth, INFO, fmt.Sprintf(format, args...))
return
}
p.internalLog(calldepth, INFO, BaseLogEntry(fmt.Sprintf(format, args...)))
} }
func (p *PackageLogger) Info(entries ...LogEntry) { func (p *PackageLogger) Info(entries ...interface{}) {
if p.level < INFO {
return
}
p.internalLog(calldepth, INFO, entries...) p.internalLog(calldepth, INFO, entries...)
} }
// Debug Functions // Debug Functions
func (p *PackageLogger) Debugf(format string, args ...interface{}) { func (p *PackageLogger) Debugf(format string, args ...interface{}) {
if p.level < DEBUG { p.internalLog(calldepth, DEBUG, fmt.Sprintf(format, args...))
return
}
p.internalLog(calldepth, DEBUG, BaseLogEntry(fmt.Sprintf(format, args...)))
} }
func (p *PackageLogger) Debug(entries ...LogEntry) { func (p *PackageLogger) Debug(entries ...interface{}) {
if p.level < DEBUG {
return
}
p.internalLog(calldepth, DEBUG, entries...) p.internalLog(calldepth, DEBUG, entries...)
} }
// Trace Functions // Trace Functions
func (p *PackageLogger) Tracef(format string, args ...interface{}) { func (p *PackageLogger) Tracef(format string, args ...interface{}) {
if p.level < TRACE { p.internalLog(calldepth, TRACE, fmt.Sprintf(format, args...))
return
}
p.internalLog(calldepth, TRACE, BaseLogEntry(fmt.Sprintf(format, args...)))
} }
func (p *PackageLogger) Trace(entries ...LogEntry) { func (p *PackageLogger) Trace(entries ...interface{}) {
if p.level < TRACE {
return
}
p.internalLog(calldepth, TRACE, entries...) p.internalLog(calldepth, TRACE, entries...)
} }

View File

@ -1,6 +1,7 @@
package capnslog package capnslog
import ( import (
"fmt"
"log/syslog" "log/syslog"
) )
@ -20,9 +21,9 @@ type syslogFormatter struct {
w *syslog.Writer 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 { for _, entry := range entries {
str := entry.LogString() str := fmt.Sprint(entry)
switch l { switch l {
case CRITICAL: case CRITICAL:
s.w.Crit(str) s.w.Crit(str)