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

Clean up logging for next major #1070

Merged
merged 1 commit into from
Apr 17, 2020
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,8 @@ retries are safe.

### Configuring Logging

Configure logging using the global `DefaultLeveledLogger` variable:
By default, the library logs error messages only (which are sent to `stderr`).
Configure default logging using the global `DefaultLeveledLogger` variable:

```go
stripe.DefaultLeveledLogger = &stripe.LeveledLogger{
Expand Down
91 changes: 6 additions & 85 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package stripe
import (
"fmt"
"io"
"log"
"os"
)

Expand All @@ -12,6 +11,9 @@ import (
//

const (
// LevelNull sets a logger to show no messages at all.
LevelNull Level = 0

// LevelError sets a logger to show error messages only.
LevelError Level = 1

Expand All @@ -26,11 +28,6 @@ const (
// LevelDebug sets a logger to show informational messages or anything more
// severe.
LevelDebug Level = 4

// Older deprecated levels for Printfer-style logging.
printferLevelError = 1
printferLevelInfo = 2
printferLevelDebug = 3
)

//
Expand All @@ -49,27 +46,9 @@ const (
// This Logger will be inherited by any backends created by default, but will
// be overridden if a backend is created with GetBackendWithConfig with a
// custom LeveledLogger set.
var DefaultLeveledLogger LeveledLoggerInterface

// LogLevel is the logging level for this library.
// 0: no logging
// 1: errors only
// 2: errors + informational (default)
// 3: errors + informational + debug
//
// Deprecated: Logging should be configured with DefaultLeveledLogger instead.
var LogLevel = 2

// Logger controls how stripe performs logging at a package level. It is useful
// to customise if you need it prefixed for your application to meet other
// requirements.
//
// This Logger will be inherited by any backends created by default, but will
// be overridden if a backend is created with GetBackendWithConfig with a
// custom Logger set.
//
// Deprecated: Logging should be configured with DefaultLeveledLogger instead.
var Logger Printfer
var DefaultLeveledLogger LeveledLoggerInterface = &LeveledLogger{
Level: LevelError,
}

//
// Public types
Expand Down Expand Up @@ -161,61 +140,3 @@ type LeveledLoggerInterface interface {
// Warnf logs a warning message using Printf conventions.
Warnf(format string, v ...interface{})
}

// Printfer is an interface to be implemented by Logger.
type Printfer interface {
Printf(format string, v ...interface{})
}

//
// Private types
//

// Level represents a deprecated logging level.
type printferLevel uint32

type leveledLoggerPrintferShim struct {
level printferLevel
logger Printfer
}

// Debugf logs a debug message using Printf conventions.
func (l *leveledLoggerPrintferShim) Debugf(format string, v ...interface{}) {
if l.level >= printferLevelDebug {
l.logger.Printf(format+"\n", v...)
}
}

// Errorf logs a warning message using Printf conventions.
func (l *leveledLoggerPrintferShim) Errorf(format string, v ...interface{}) {
if l.level >= printferLevelError {
l.logger.Printf(format+"\n", v...)
}
}

// Infof logs an informational message using Printf conventions.
func (l *leveledLoggerPrintferShim) Infof(format string, v ...interface{}) {
if l.level >= printferLevelInfo {
l.logger.Printf(format+"\n", v...)
}
}

// Warnf logs a warning message using Printf conventions.
func (l *leveledLoggerPrintferShim) Warnf(format string, v ...interface{}) {
// The original Stripe log level system didn't have a concept for warnings,
// so just reuse the same levels as error.
if l.level >= printferLevelError {
l.logger.Printf(format+"\n", v...)
}
}

//
// Private functions
//

func init() {
// Defaults to logging nothing, but also makes sure that we have a logger
// so that we don't panic on a `nil` when the library tries to log
// something.
Logger = log.New(os.Stderr, "", log.LstdFlags)
}
86 changes: 1 addition & 85 deletions log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package stripe

import (
"bytes"
"log"
"testing"

assert "github.com/stretchr/testify/require"
Expand All @@ -13,14 +12,7 @@ import (
//

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)
_, ok := DefaultLeveledLogger.(*LeveledLogger)
assert.True(t, ok)
}

Expand Down Expand Up @@ -114,82 +106,6 @@ func TestLeveledLoggerErrorf(t *testing.T) {
}
}

//
// 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
//
Expand Down
37 changes: 7 additions & 30 deletions stripe.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,27 +176,13 @@ type BackendConfig struct {
// also provides out-of-the-box compatibility with a Logrus Logger, but may
// require a thin shim for use with other logging libraries that use less
// standard conventions like Zap.
LeveledLogger LeveledLoggerInterface

// LogLevel is the logging level of the library and defined by:
//
// 0: no logging
// 1: errors only
// 2: errors + informational (default)
// 3: errors + informational + debug
//
// Defaults to 0 (no logging), so please make sure to set this if you want
// to see logging output in your custom configuration.
//
// Deprecated: Logging should be configured with LeveledLogger instead.
LogLevel int

// Logger is where this backend will write its logs.
//
// If left unset, it'll be set to Logger.
// Defaults to DefaultLeveledLogger.
//
// Deprecated: Logging should be configured with LeveledLogger instead.
Logger Printfer
// To set a logger that logs nothing, set this to a stripe.LeveledLogger
// with a Level of LevelNull (simply setting this field to nil will not
// work).
LeveledLogger LeveledLoggerInterface

// MaxNetworkRetries sets maximum number of times that the library will
// retry requests that appear to have failed due to an intermittent
Expand Down Expand Up @@ -813,9 +799,7 @@ func GetBackend(backendType SupportedBackend) Backend {
backendType,
&BackendConfig{
HTTPClient: httpClient,
LeveledLogger: DefaultLeveledLogger,
LogLevel: LogLevel,
Logger: Logger,
LeveledLogger: nil, // Set by GetBackendWithConfiguation when nil
MaxNetworkRetries: 0,
URL: "", // Set by GetBackendWithConfiguation when empty
},
Expand All @@ -835,14 +819,7 @@ func GetBackendWithConfig(backendType SupportedBackend, config *BackendConfig) B
}

if config.LeveledLogger == nil {
if config.Logger == nil {
config.Logger = Logger
}

config.LeveledLogger = &leveledLoggerPrintferShim{
level: printferLevel(config.LogLevel),
logger: config.Logger,
}
config.LeveledLogger = DefaultLeveledLogger
}

switch backendType {
Expand Down
Loading