forked from bold-commerce/go-shopify
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding WithLogger option (bold-commerce#99)
Co-authored-by: Gord Currie <[email protected]>
- Loading branch information
1 parent
2fe6b77
commit 0398b1e
Showing
8 changed files
with
285 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package goshopify | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
) | ||
|
||
// idea from https://github.com/stripe/stripe-go/blob/master/log.go | ||
|
||
const ( | ||
LevelError = iota + 1 | ||
LevelWarn | ||
LevelInfo | ||
LevelDebug | ||
) | ||
|
||
type LeveledLoggerInterface interface { | ||
Debugf(format string, v ...interface{}) | ||
Errorf(format string, v ...interface{}) | ||
Infof(format string, v ...interface{}) | ||
Warnf(format string, v ...interface{}) | ||
} | ||
|
||
// It prints warnings and errors to `os.Stderr` and other messages to | ||
// `os.Stdout`. | ||
type LeveledLogger struct { | ||
// Level is the minimum logging level that will be emitted by this logger. | ||
// | ||
// For example, a Level set to LevelWarn will emit warnings and errors, but | ||
// not informational or debug messages. | ||
// | ||
// Always set this with a constant like LevelWarn because the individual | ||
// values are not guaranteed to be stable. | ||
Level int | ||
|
||
// Internal testing use only. | ||
stderrOverride io.Writer | ||
stdoutOverride io.Writer | ||
} | ||
|
||
// Debugf logs a debug message using Printf conventions. | ||
func (l *LeveledLogger) Debugf(format string, v ...interface{}) { | ||
if l.Level >= LevelDebug { | ||
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(), "[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(), "[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(), "[WARN] "+format+"\n", v...) | ||
} | ||
} | ||
|
||
func (l *LeveledLogger) stderr() io.Writer { | ||
if l.stderrOverride != nil { | ||
return l.stderrOverride | ||
} | ||
|
||
return os.Stderr | ||
} | ||
|
||
func (l *LeveledLogger) stdout() io.Writer { | ||
if l.stdoutOverride != nil { | ||
return l.stdoutOverride | ||
} | ||
|
||
return os.Stdout | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package goshopify | ||
|
||
import ( | ||
"bytes" | ||
"io/ioutil" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestLeveledLogger(t *testing.T) { | ||
tests := []struct { | ||
level int | ||
input string | ||
stdout string | ||
stderr string | ||
}{ | ||
{ | ||
level: LevelError, | ||
input: "log", | ||
stderr: "[ERROR] error log\n", | ||
stdout: "", | ||
}, | ||
{ | ||
level: LevelWarn, | ||
input: "log", | ||
stderr: "[ERROR] error log\n[WARN] warn log\n", | ||
stdout: "", | ||
}, | ||
{ | ||
level: LevelInfo, | ||
input: "log", | ||
stderr: "[ERROR] error log\n[WARN] warn log\n", | ||
stdout: "[INFO] info log\n", | ||
}, | ||
{ | ||
level: LevelDebug, | ||
input: "log", | ||
stderr: "[ERROR] error log\n[WARN] warn log\n", | ||
stdout: "[INFO] info log\n[DEBUG] debug log\n", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
err := &bytes.Buffer{} | ||
out := &bytes.Buffer{} | ||
log := &LeveledLogger{Level: test.level, stderrOverride: err, stdoutOverride: out} | ||
|
||
log.Errorf("error %s", test.input) | ||
log.Warnf("warn %s", test.input) | ||
log.Infof("info %s", test.input) | ||
log.Debugf("debug %s", test.input) | ||
|
||
stdout := out.String() | ||
stderr := err.String() | ||
|
||
if stdout != test.stdout { | ||
t.Errorf("leveled logger %d expected stdout \"%s\" received \"%s\"", test.level, test.stdout, stdout) | ||
} | ||
if stderr != test.stderr { | ||
t.Errorf("leveled logger %d expected stderr \"%s\" received \"%s\"", test.level, test.stderr, stderr) | ||
} | ||
} | ||
|
||
log := &LeveledLogger{Level: LevelDebug} | ||
if log.stderr() != os.Stderr { | ||
t.Errorf("leveled logger with no stderr override expects os.Stderr") | ||
} | ||
if log.stdout() != os.Stdout { | ||
t.Errorf("leveled logger with no stdout override expects os.Stdout") | ||
} | ||
|
||
} | ||
|
||
func TestDoGetHeadersDebug(t *testing.T) { | ||
err := &bytes.Buffer{} | ||
out := &bytes.Buffer{} | ||
logger := &LeveledLogger{Level: LevelDebug, stderrOverride: err, stdoutOverride: out} | ||
|
||
reqExpected := "[DEBUG] GET: //http:%2F%2Ftest.com/foo/1\n[DEBUG] SENT: request body\n" | ||
resExpected := "[DEBUG] RECV 200: OK\n[DEBUG] RESP: response body\n" | ||
|
||
client := NewClient(app, "fooshop", "abcd", WithLogger(logger)) | ||
|
||
client.logBody(nil, "") | ||
if out.String() != "" { | ||
t.Errorf("logBody expected empty log output but received \"%s\"", out.String()) | ||
} | ||
|
||
client.logRequest(nil) | ||
if out.String() != "" { | ||
t.Errorf("logRequest expected empty log output received \"%s\"", out.String()) | ||
} | ||
|
||
client.logRequest(&http.Request{ | ||
Method: "GET", | ||
URL: &url.URL{Host: "http://test.com", Path: "/foo/1"}, | ||
Body: ioutil.NopCloser(strings.NewReader("request body")), | ||
}) | ||
|
||
if out.String() != reqExpected { | ||
t.Errorf("doGetHeadersDebug expected stdout \"%s\" received \"%s\"", reqExpected, out) | ||
} | ||
|
||
err.Reset() | ||
out.Reset() | ||
|
||
client.logResponse(nil) | ||
if out.String() != "" { | ||
t.Errorf("logResponse expected empty log output received \"%s\"", out.String()) | ||
} | ||
|
||
client.logResponse(&http.Response{ | ||
Status: http.StatusText(http.StatusOK), | ||
StatusCode: http.StatusOK, | ||
Body: ioutil.NopCloser(strings.NewReader("response body")), | ||
}) | ||
|
||
if out.String() != resExpected { | ||
t.Errorf("doGetHeadersDebug expected stdout \"%s\" received \"%s\"", resExpected, out.String()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.