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

Allow logger creation from an existing standard logger #345

Merged
merged 3 commits into from
Apr 15, 2024
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
4 changes: 4 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ func NewLogger(output io.Writer, prefix string, flag int) Logger {
return &logger{l: log.New(output, prefix, flag)}
}

func NewLoggerFromStandardLogger(l *log.Logger) Logger {
return &logger{l: l}
}

func createDefaultLogger() Logger {
return NewLogger(os.Stdout, "", log.Ldate|log.Lmicroseconds)
}
Expand Down
14 changes: 13 additions & 1 deletion logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package req

import (
"bytes"
"github.com/imroc/req/v3/internal/tests"
"log"
"testing"

"github.com/imroc/req/v3/internal/tests"
)

func TestLogger(t *testing.T) {
Expand All @@ -17,3 +18,14 @@ func TestLogger(t *testing.T) {
c.R().SetOutput(nil)
tests.AssertContains(t, buf.String(), "warn", true)
}

func TestLoggerFromStandardLogger(t *testing.T) {
buf := new(bytes.Buffer)
l := NewLoggerFromStandardLogger(log.New(buf, "", log.Ldate|log.Lmicroseconds))
c := tc().SetLogger(l)
c.SetProxyURL(":=\\<>ksfj&*&sf")
tests.AssertContains(t, buf.String(), "error", true)
buf.Reset()
c.R().SetOutput(nil)
tests.AssertContains(t, buf.String(), "warn", true)
}
Loading