Skip to content

Commit

Permalink
Allow logger creation from an existing standard logger
Browse files Browse the repository at this point in the history
  • Loading branch information
dbhoot committed Apr 10, 2024
1 parent 19bac2c commit 14d332b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
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 NewStandardLogger(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 TestStandardLogger(t *testing.T) {
buf := new(bytes.Buffer)
l := NewStandardLogger(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)
}

0 comments on commit 14d332b

Please sign in to comment.