Permalink
Browse files

Refactor API

This change makes the exported Logger type a struct that wraps an interface.
This makes it possible to only define cosmetic methods on this struct, instead
of polluting the interface definition.
  • Loading branch information...
1 parent 273887d commit 79a2f31a4dc3d87d91e653b76a54f54bab5c9067 Dmitriy Kalinin and Pieter Noordhuis committed Mar 7, 2013
Showing with 176 additions and 174 deletions.
  1. +3 −2 config_test.go
  2. +117 −77 logger.go
  3. +25 −7 logger_test.go
  4. +10 −11 steno.go
  5. +6 −6 steno_test.go
  6. +15 −71 tagged_logger.go
View
@@ -14,7 +14,8 @@ func (s *ConfigSuite) TestReInitLevel(c *C) {
for _, level := range levels {
Init(&Config{Level: level})
- logger := NewLogger("reinit").(*BaseLogger)
- c.Assert(logger.level, Equals, level)
+
+ l := NewLogger("reinit")
+ c.Assert(l.Level(), Equals, level)
}
}
View
194 logger.go
@@ -6,26 +6,13 @@ import (
"log"
)
-type Logger interface {
- json.Marshaler
-
- Log(level LogLevel, m string, d map[string]interface{})
-
- Fatal(m string)
- Error(m string)
- Warn(m string)
- Info(m string)
- Debug(m string)
- Debug1(m string)
- Debug2(m string)
+type L interface {
+ Level() LogLevel
+ Log(x LogLevel, m string, d map[string]interface{})
+}
- Fatalf(f string, a ...interface{})
- Errorf(f string, a ...interface{})
- Warnf(f string, a ...interface{})
- Infof(f string, a ...interface{})
- Debugf(f string, a ...interface{})
- Debug1f(f string, a ...interface{})
- Debug2f(f string, a ...interface{})
+type Logger struct {
+ L
}
type BaseLogger struct {
@@ -34,102 +21,155 @@ type BaseLogger struct {
level LogLevel
}
-func (x *BaseLogger) Log(l LogLevel, m string, d map[string]interface{}) {
- if !x.active(l) {
+func (l *BaseLogger) active(x LogLevel) bool {
+ return l.level.Priority >= x.Priority
+}
+
+func (l *BaseLogger) Level() LogLevel {
+ return l.level
+}
+
+func (l *BaseLogger) Log(x LogLevel, m string, d map[string]interface{}) {
+ if !l.active(x) {
return
}
- record := NewRecord(x.name, l, m, d)
+ r := NewRecord(l.name, x, m, d)
+ for _, s := range l.sinks {
+ s.AddRecord(r)
+ s.Flush()
+ }
- for _, sink := range x.sinks {
- sink.AddRecord(record)
- sink.Flush()
+ if x == LOG_FATAL {
+ panic(m)
}
}
-func (x *BaseLogger) Fatal(m string) {
- x.Log(LOG_FATAL, m, nil)
- panic(m)
+func (l *BaseLogger) MarshalJSON() ([]byte, error) {
+ sinks := "["
+ for i, sink := range l.sinks {
+ m, err := json.Marshal(sink)
+ if err != nil {
+ log.Println(err)
+ }
+ sinks += string(m)
+ if i != len(l.sinks)-1 {
+ sinks += ","
+ }
+ }
+ sinks += "]"
+ msg := fmt.Sprintf("{\"level\": \"%s\", \"sinks\": %s}", l.level.Name, sinks)
+ return []byte(msg), nil
}
-func (x *BaseLogger) Error(m string) {
- x.Log(LOG_ERROR, m, nil)
+func (l Logger) Fatal(m string) {
+ l.Log(LOG_FATAL, m, nil)
}
-func (x *BaseLogger) Warn(m string) {
- x.Log(LOG_WARN, m, nil)
+func (l Logger) Error(m string) {
+ l.Log(LOG_ERROR, m, nil)
}
-func (x *BaseLogger) Info(m string) {
- x.Log(LOG_INFO, m, nil)
+func (l Logger) Warn(m string) {
+ l.Log(LOG_WARN, m, nil)
}
-func (x *BaseLogger) Debug(m string) {
- x.Log(LOG_DEBUG, m, nil)
+func (l Logger) Info(m string) {
+ l.Log(LOG_INFO, m, nil)
}
-func (x *BaseLogger) Debug1(m string) {
- x.Log(LOG_DEBUG1, m, nil)
+func (l Logger) Debug(m string) {
+ l.Log(LOG_DEBUG, m, nil)
}
-func (x *BaseLogger) Debug2(m string) {
- x.Log(LOG_DEBUG2, m, nil)
+func (l Logger) Debug1(m string) {
+ l.Log(LOG_DEBUG1, m, nil)
}
-func (x *BaseLogger) Fatalf(f string, a ...interface{}) {
- x.Fatal(fmt.Sprintf(f, a...))
+func (l Logger) Debug2(m string) {
+ l.Log(LOG_DEBUG2, m, nil)
}
-func (x *BaseLogger) Errorf(f string, a ...interface{}) {
- x.Error(fmt.Sprintf(f, a...))
+func (l Logger) Fatald(d map[string]interface{}, m string) {
+ l.Log(LOG_FATAL, m, d)
}
-func (x *BaseLogger) Warnf(f string, a ...interface{}) {
- x.Warn(fmt.Sprintf(f, a...))
+func (l Logger) Errord(d map[string]interface{}, m string) {
+ l.Log(LOG_ERROR, m, d)
}
-func (x *BaseLogger) Infof(f string, a ...interface{}) {
- x.Info(fmt.Sprintf(f, a...))
+func (l Logger) Warnd(d map[string]interface{}, m string) {
+ l.Log(LOG_WARN, m, d)
}
-func (x *BaseLogger) Debugf(f string, a ...interface{}) {
- x.Debug(fmt.Sprintf(f, a...))
+func (l Logger) Infod(d map[string]interface{}, m string) {
+ l.Log(LOG_INFO, m, d)
}
-func (x *BaseLogger) Debug1f(f string, a ...interface{}) {
- x.Debug1(fmt.Sprintf(f, a...))
+func (l Logger) Debugd(d map[string]interface{}, m string) {
+ l.Log(LOG_DEBUG, m, d)
}
-func (x *BaseLogger) Debug2f(f string, a ...interface{}) {
- x.Debug2(fmt.Sprintf(f, a...))
+func (l Logger) Debug1d(d map[string]interface{}, m string) {
+ l.Log(LOG_DEBUG1, m, d)
}
-func (x *BaseLogger) MarshalJSON() ([]byte, error) {
- sinks := "["
- for i, sink := range x.sinks {
- m, err := json.Marshal(sink)
- if err != nil {
- log.Println(err)
- }
- sinks += string(m)
- if i != len(x.sinks)-1 {
- sinks += ","
- }
- }
- sinks += "]"
- msg := fmt.Sprintf("{\"level\": \"%s\", \"sinks\": %s}", x.level.Name, sinks)
- return []byte(msg), nil
+func (l Logger) Debug2d(d map[string]interface{}, m string) {
+ l.Log(LOG_DEBUG2, m, d)
}
-func (x *BaseLogger) active(level LogLevel) bool {
- if x.level.Priority >= level.Priority {
- return true
- }
+func (l Logger) Fatalf(f string, a ...interface{}) {
+ l.Log(LOG_FATAL, fmt.Sprintf(f, a...), nil)
+}
+
+func (l Logger) Errorf(f string, a ...interface{}) {
+ l.Log(LOG_ERROR, fmt.Sprintf(f, a...), nil)
+}
+
+func (l Logger) Warnf(f string, a ...interface{}) {
+ l.Log(LOG_WARN, fmt.Sprintf(f, a...), nil)
+}
+
+func (l Logger) Infof(f string, a ...interface{}) {
+ l.Log(LOG_INFO, fmt.Sprintf(f, a...), nil)
+}
+
+func (l Logger) Debugf(f string, a ...interface{}) {
+ l.Log(LOG_DEBUG, fmt.Sprintf(f, a...), nil)
+}
+
+func (l Logger) Debug1f(f string, a ...interface{}) {
+ l.Log(LOG_DEBUG1, fmt.Sprintf(f, a...), nil)
+}
+
+func (l Logger) Debug2f(f string, a ...interface{}) {
+ l.Log(LOG_DEBUG2, fmt.Sprintf(f, a...), nil)
+}
+
+func (l Logger) Fataldf(d map[string]interface{}, f string, a ...interface{}) {
+ l.Log(LOG_FATAL, fmt.Sprintf(f, a...), d)
+}
+
+func (l Logger) Errordf(d map[string]interface{}, f string, a ...interface{}) {
+ l.Log(LOG_ERROR, fmt.Sprintf(f, a...), d)
+}
+
+func (l Logger) Warndf(d map[string]interface{}, f string, a ...interface{}) {
+ l.Log(LOG_WARN, fmt.Sprintf(f, a...), d)
+}
+
+func (l Logger) Infodf(d map[string]interface{}, f string, a ...interface{}) {
+ l.Log(LOG_INFO, fmt.Sprintf(f, a...), d)
+}
+
+func (l Logger) Debugdf(d map[string]interface{}, f string, a ...interface{}) {
+ l.Log(LOG_DEBUG, fmt.Sprintf(f, a...), d)
+}
- return false
+func (l Logger) Debug1df(d map[string]interface{}, f string, a ...interface{}) {
+ l.Log(LOG_DEBUG1, fmt.Sprintf(f, a...), d)
}
-// For testing
-func NumLogger() int {
- return len(loggers)
+func (l Logger) Debug2df(d map[string]interface{}, f string, a ...interface{}) {
+ l.Log(LOG_DEBUG2, fmt.Sprintf(f, a...), d)
}
View
@@ -10,6 +10,10 @@ type LoggerSuite struct {
var _ = Suite(&LoggerSuite{})
+func NumLogger() int {
+ return len(loggers)
+}
+
func (s *LoggerSuite) SetUpTest(c *C) {
cfg := Config{}
s.nSink = newNullSink()
@@ -29,13 +33,27 @@ func (s *LoggerSuite) TestLoggersNum(c *C) {
c.Assert(len(loggers), Equals, 0)
}
-func (s *LoggerSuite) TestLoggerLevelActive(c *C) {
- // active is a private method of BaseLogger
- logger := NewLogger("bar").(*BaseLogger)
- logger.level = LOG_INFO
- higherLevels := []LogLevel{LOG_WARN, LOG_ERROR, LOG_FATAL}
- for _, level := range higherLevels {
- c.Assert(logger.active(level), Equals, true)
+func (s *LoggerSuite) TestLogLevel(c *C) {
+ bl := &BaseLogger{
+ name: "bar",
+ level: LOG_INFO,
+ sinks: []Sink{&nullSink{}},
+ }
+
+ higher := []LogLevel{LOG_INFO, LOG_WARN, LOG_ERROR}
+ for _, l := range higher {
+ s := &nullSink{}
+ bl.sinks = []Sink{s}
+ bl.Log(l, "hello", nil)
+ c.Assert(len(s.records), Equals, 1)
+ }
+
+ lower := []LogLevel{LOG_DEBUG, LOG_DEBUG1, LOG_DEBUG2, LOG_ALL}
+ for _, l := range lower {
+ s := &nullSink{}
+ bl.sinks = []Sink{s}
+ bl.Log(l, "hello", nil)
+ c.Assert(len(s.records), Equals, 0)
}
}
View
@@ -46,20 +46,19 @@ func NewLogger(name string) Logger {
loggersMutex.Lock()
defer loggersMutex.Unlock()
- logger := loggers[name]
-
- if logger == nil {
- baseLogger := new(BaseLogger)
-
- baseLogger.name = name
- baseLogger.sinks = config.Sinks
- baseLogger.level = computeLevel(name)
+ l := loggers[name]
+ if l == nil {
+ bl := &BaseLogger{
+ name: name,
+ sinks: config.Sinks,
+ level: computeLevel(name),
+ }
- logger = baseLogger
- loggers[name] = logger
+ loggers[name] = bl
+ l = bl
}
- return logger
+ return Logger{l}
}
func loggersInJson() string {
View
@@ -44,9 +44,9 @@ func (s *StenoSuite) TestLoggersInJson(c *C) {
func (s *StenoSuite) TestSetLoggerRegexp(c *C) {
// level is a field of BaseLogger, hence type cast is needed
- logger1 := NewLogger("test").(*BaseLogger)
- logger2 := NewLogger("test2").(*BaseLogger)
- logger3 := NewLogger("test3").(*BaseLogger)
+ logger1 := NewLogger("test").L.(*BaseLogger)
+ logger2 := NewLogger("test2").L.(*BaseLogger)
+ logger3 := NewLogger("test3").L.(*BaseLogger)
c.Assert(logger1.level, Equals, LOG_INFO)
c.Assert(logger2.level, Equals, LOG_INFO)
@@ -70,9 +70,9 @@ func (s *StenoSuite) TestSetLoggerRegexp(c *C) {
func (s *StenoSuite) TestClearLoggerRegexp(c *C) {
// level is a field of BaseLogger, hence type cast is needed
- logger1 := NewLogger("test").(*BaseLogger)
- logger2 := NewLogger("test2").(*BaseLogger)
- logger3 := NewLogger("test3").(*BaseLogger)
+ logger1 := NewLogger("test").L.(*BaseLogger)
+ logger2 := NewLogger("test2").L.(*BaseLogger)
+ logger3 := NewLogger("test3").L.(*BaseLogger)
c.Assert(logger1.level, Equals, LOG_INFO)
c.Assert(logger2.level, Equals, LOG_INFO)
Oops, something went wrong.

0 comments on commit 79a2f31

Please sign in to comment.