Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add log prefixes + test suite #819

Merged
merged 1 commit into from
Mar 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,29 +100,29 @@ type LeveledLogger struct {
// Debugf logs a debug message using Printf conventions.
func (l *LeveledLogger) Debugf(format string, v ...interface{}) {
if l.Level >= LevelDebug {
fmt.Fprintf(l.stdout(), format+"\n", v...)
fmt.Fprintf(l.stdout(), "[DEBUG] "+format+"\n", v...)
}
}

// Errorf logs a warning message using Printf conventions.
func (l *LeveledLogger) Errorf(format string, v ...interface{}) {
// Infof logs a debug message using Printf conventions.
if l.Level >= LevelError {
fmt.Fprintf(l.stderr(), format+"\n", v...)
fmt.Fprintf(l.stderr(), "[ERROR] "+format+"\n", v...)
}
}

// Infof logs an informational message using Printf conventions.
func (l *LeveledLogger) Infof(format string, v ...interface{}) {
if l.Level >= LevelInfo {
fmt.Fprintf(l.stdout(), format+"\n", v...)
fmt.Fprintf(l.stdout(), "[INFO] "+format+"\n", v...)
}
}

// Warnf logs a warning message using Printf conventions.
func (l *LeveledLogger) Warnf(format string, v ...interface{}) {
if l.Level >= LevelWarn {
fmt.Fprintf(l.stderr(), format+"\n", v...)
fmt.Fprintf(l.stderr(), "[WARN] "+format+"\n", v...)
}
}

Expand Down
201 changes: 201 additions & 0 deletions log_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
package stripe

import (
"bytes"
"log"
"testing"

assert "github.com/stretchr/testify/require"
)

//
// Tests
//

func TestDefaultLeveledLogger(t *testing.T) {
// We don't set DefaultLeveledLogger by default for backwards compatibility
// reasons. If we did, then it would override Logger, which many people
// have been setting over the years.
assert.Nil(t, DefaultLeveledLogger)

// Logger continues to be set by default so that we have a non-nil object
// to log against.
_, ok := Logger.(*log.Logger)
assert.True(t, ok)
}

//
// LeveledLogger
//

func TestLeveledLoggerDebugf(t *testing.T) {
var stdout, stderr bytes.Buffer
logger := &LeveledLogger{stdoutOverride: &stdout, stderrOverride: &stderr}

{
clearBuffers(&stdout, &stderr)
logger.Level = LevelDebug

logger.Debugf("test")
assert.Equal(t, "[DEBUG] test\n", stdout.String())
assert.Equal(t, "", stderr.String())
}

// Expect no logging
for _, level := range []Level{LevelInfo, LevelWarn, LevelError} {
clearBuffers(&stdout, &stderr)
logger.Level = level

logger.Debugf("test")
assert.Equal(t, "", stdout.String())
assert.Equal(t, "", stderr.String())
}
}

func TestLeveledLoggerInfof(t *testing.T) {
var stdout, stderr bytes.Buffer
logger := &LeveledLogger{stdoutOverride: &stdout, stderrOverride: &stderr}

for _, level := range []Level{LevelDebug, LevelInfo} {
clearBuffers(&stdout, &stderr)
logger.Level = level

logger.Infof("test")
assert.Equal(t, "[INFO] test\n", stdout.String())
assert.Equal(t, "", stderr.String())
}

// Expect no logging
for _, level := range []Level{LevelWarn, LevelError} {
clearBuffers(&stdout, &stderr)
logger.Level = level

logger.Infof("test")
assert.Equal(t, "", stdout.String())
assert.Equal(t, "", stderr.String())
}
}

func TestLeveledLoggerWarnf(t *testing.T) {
var stdout, stderr bytes.Buffer
logger := &LeveledLogger{stdoutOverride: &stdout, stderrOverride: &stderr}

for _, level := range []Level{LevelDebug, LevelInfo, LevelWarn} {
clearBuffers(&stdout, &stderr)
logger.Level = level

logger.Warnf("test")
assert.Equal(t, "", stdout.String())
assert.Equal(t, "[WARN] test\n", stderr.String())
}

// Expect no logging
{
clearBuffers(&stdout, &stderr)
logger.Level = LevelError

logger.Warnf("test")
assert.Equal(t, "", stdout.String())
assert.Equal(t, "", stderr.String())
}
}

func TestLeveledLoggerErrorf(t *testing.T) {
var stdout, stderr bytes.Buffer
logger := &LeveledLogger{stdoutOverride: &stdout, stderrOverride: &stderr}

for _, level := range []Level{LevelDebug, LevelInfo, LevelWarn, LevelError} {
clearBuffers(&stdout, &stderr)
logger.Level = level

logger.Errorf("test")
assert.Equal(t, "", stdout.String())
assert.Equal(t, "[ERROR] test\n", stderr.String())
}
}

//
// leveledLoggerPrintferShim
//

func TestLeveledLoggerPrintferShimDebugf(t *testing.T) {
var stdout bytes.Buffer
logger := &leveledLoggerPrintferShim{logger: log.New(&stdout, "", 0)}

{
clearBuffers(&stdout)
logger.level = printferLevelDebug

logger.Debugf("test")
assert.Equal(t, "test\n", stdout.String())
}

// Expect no logging
for _, level := range []printferLevel{printferLevelInfo, printferLevelError} {
clearBuffers(&stdout)
logger.level = level

logger.Debugf("test")
assert.Equal(t, "", stdout.String())
}
}

func TestLeveledLoggerPrintferShimInfof(t *testing.T) {
var stdout bytes.Buffer
logger := &leveledLoggerPrintferShim{logger: log.New(&stdout, "", 0)}

for _, level := range []printferLevel{printferLevelDebug, printferLevelInfo} {
clearBuffers(&stdout)
logger.level = level

logger.Infof("test")
assert.Equal(t, "test\n", stdout.String())
}

// Expect no logging
for _, level := range []printferLevel{printferLevelError} {
clearBuffers(&stdout)
logger.level = level

logger.Infof("test")
assert.Equal(t, "", stdout.String())
}
}

// Note: behaves identically to Errorf because historically there was no
// warning level.
func TestLeveledLoggerPrintferShimWarnf(t *testing.T) {
var stdout bytes.Buffer
logger := &leveledLoggerPrintferShim{logger: log.New(&stdout, "", 0)}

for _, level := range []printferLevel{printferLevelDebug, printferLevelInfo, printferLevelError} {
clearBuffers(&stdout)
logger.level = level

logger.Warnf("test")
assert.Equal(t, "test\n", stdout.String())
}
}

func TestLeveledLoggerPrintferShimErrorf(t *testing.T) {
var stdout bytes.Buffer
logger := &leveledLoggerPrintferShim{logger: log.New(&stdout, "", 0)}

for _, level := range []printferLevel{printferLevelDebug, printferLevelInfo, printferLevelError} {
clearBuffers(&stdout)
logger.level = level

logger.Errorf("test")
assert.Equal(t, "test\n", stdout.String())
}
}

//
// Private functions
//

func clearBuffers(buffers ...*bytes.Buffer) {
for _, b := range buffers {
b.Truncate(0)
}
}