Skip to content

Commit

Permalink
Merge pull request #3 from slok/simplify-api
Browse files Browse the repository at this point in the history
Remove DebugEnabled from the API
  • Loading branch information
slok authored Sep 23, 2018
2 parents c1f219f + 24471b7 commit ccca7a6
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 40 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
vendor/
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.x.x / 2018-xx-xx

* [FEATURE] Simplify API by removing `DebugEnabled` method.

## 0.1.0 / 2018-09-22

* [FEATURE] Add LoggerFunc, helper to create a Loggers without a new type.
Expand Down
13 changes: 8 additions & 5 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ func Example_loggerFunc() {

// Create our logger using the helper funcs so we don't need to create a new type.
logger := &glog.LoggerFunc{
DebugEnabledFunc: func() bool { return debug },
DebugfFunc: func(format string, args ...interface{}) { fmt.Printf("[DEBUG] "+format, args...) },
InfofFunc: func(format string, args ...interface{}) { fmt.Printf("[INFO] "+format, args...) },
WarnfFunc: func(format string, args ...interface{}) { fmt.Printf("[WARN] "+format, args...) },
ErrorfFunc: func(format string, args ...interface{}) { fmt.Printf("[ERROR] "+format, args...) },
DebugfFunc: func(format string, args ...interface{}) {
if debug {
fmt.Printf("[DEBUG] "+format, args...)
}
},
InfofFunc: func(format string, args ...interface{}) { fmt.Printf("[INFO] "+format, args...) },
WarnfFunc: func(format string, args ...interface{}) { fmt.Printf("[WARN] "+format, args...) },
ErrorfFunc: func(format string, args ...interface{}) { fmt.Printf("[ERROR] "+format, args...) },
}

// Set the custom logger
Expand Down
40 changes: 14 additions & 26 deletions glog.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import (
// needs to implement with the aim of replacing the logger that
// comes with glog.
type Logger interface {
// DebugEnabled returns if the Debug level of the logger is enabled.
DebugEnabled() bool
// Debugf logs with debug.
Debugf(format string, args ...interface{})
// Infof logs with info.
Expand Down Expand Up @@ -51,15 +49,13 @@ func Flush() {}

// V replacement for glog.
func V(_ Level) Verbose {
return Verbose(logger.DebugEnabled())
return Verbose(false) // Doesn't mind.
}

// Info replacement for glog.
func (v Verbose) Info(args ...interface{}) {
if v {
s := fmt.Sprint(args...)
logger.Debugf(s)
}
func (Verbose) Info(args ...interface{}) {
s := fmt.Sprint(args...)
logger.Debugf(s)
}

// Infoln replacement for glog.
Expand All @@ -69,10 +65,8 @@ func (v Verbose) Infoln(args ...interface{}) {
}

// Infof replacement for glog.
func (v Verbose) Infof(format string, args ...interface{}) {
if v {
logger.Debugf(format, args...)
}
func (Verbose) Infof(format string, args ...interface{}) {
logger.Debugf(format, args...)
}

// Info replacement for glog.
Expand Down Expand Up @@ -193,21 +187,16 @@ func Exitf(format string, args ...interface{}) {
// gets the Logger interface required methods as func fields.
// Basically is a helper to not create a custom type.
type LoggerFunc struct {
// DebugEnabledFunc is a function for DebugEnabledFunc `Logger.DebugEnabled() bool`.
DebugEnabledFunc func() bool
// DebugfFunc is a function for DebugEnabledFunc `Logger.Debugf(format string, args ...interface{})`.
// DebugfFunc is a function for DebugfFunc `Logger.Debugf(format string, args ...interface{})`.
DebugfFunc func(format string, args ...interface{})
// InfofFunc is a function for DebugEnabledFunc `Logger.Infof(format string, args ...interface{})`.
// InfofFunc is a function for InfofFunc `Logger.Infof(format string, args ...interface{})`.
InfofFunc func(format string, args ...interface{})
// WarnfFunc is a function for DebugEnabledFunc `Logger.Warnf(format string, args ...interface{})`.
// WarnfFunc is a function for WarnfFunc `Logger.Warnf(format string, args ...interface{})`.
WarnfFunc func(format string, args ...interface{})
// ErrorfFunc is a function for DebugEnabledFunc `Logger.Errorf(format string, args ...interface{})`.
// ErrorfFunc is a function for ErrorfFunc `Logger.Errorf(format string, args ...interface{})`.
ErrorfFunc func(format string, args ...interface{})
}

// DebugEnabled satisfies Logger interface.
func (l *LoggerFunc) DebugEnabled() bool { return l.DebugEnabledFunc() }

// Debugf satisfies Logger interface.
func (l *LoggerFunc) Debugf(format string, args ...interface{}) { l.DebugfFunc(format, args...) }

Expand All @@ -222,9 +211,8 @@ func (l *LoggerFunc) Errorf(format string, args ...interface{}) { l.ErrorfFunc(f

// Dummy is a dummy logger useful for tests and disabling logging.
var Dummy = &LoggerFunc{
DebugEnabledFunc: func() bool { return true },
DebugfFunc: func(format string, args ...interface{}) {},
InfofFunc: func(format string, args ...interface{}) {},
WarnfFunc: func(format string, args ...interface{}) {},
ErrorfFunc: func(format string, args ...interface{}) {},
DebugfFunc: func(format string, args ...interface{}) {},
InfofFunc: func(format string, args ...interface{}) {},
WarnfFunc: func(format string, args ...interface{}) {},
ErrorfFunc: func(format string, args ...interface{}) {},
}
13 changes: 4 additions & 9 deletions glog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ type mockLogger struct {
rw bytes.Buffer
}

func (m *mockLogger) DebugEnabled() bool {
return m.debug
}

func (m *mockLogger) Debugf(format string, args ...interface{}) {
if m.debug {
s := fmt.Sprintf(format, args...)
Expand Down Expand Up @@ -245,11 +241,10 @@ func TestLoggerFunc(t *testing.T) {

// Create our logger using funcs.
mlf := &glog.LoggerFunc{
DebugEnabledFunc: ml.DebugEnabled,
DebugfFunc: ml.Debugf,
InfofFunc: ml.Infof,
WarnfFunc: ml.Warnf,
ErrorfFunc: ml.Errorf,
DebugfFunc: ml.Debugf,
InfofFunc: ml.Infof,
WarnfFunc: ml.Warnf,
ErrorfFunc: ml.Errorf,
}

glog.SetLogger(mlf)
Expand Down

0 comments on commit ccca7a6

Please sign in to comment.