Skip to content

Commit

Permalink
allow logger to be specified in options (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
izolight authored May 26, 2023
1 parent 5c2b877 commit e90f167
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ type Options struct {
OptionsSuccessStatus int
// Debugging flag adds additional output to debug server side CORS issues
Debug bool
// Adds a custom logger, implies Debug is true
Logger Logger
}

// Logger generic interface for logger
Expand Down Expand Up @@ -120,6 +122,7 @@ func New(options Options) *Cors {
allowPrivateNetwork: options.AllowPrivateNetwork,
maxAge: options.MaxAge,
optionPassthrough: options.OptionsPassthrough,
Log: options.Logger,
}
if options.Debug && c.Log == nil {
c.Log = log.New(os.Stdout, "[cors] ", log.LstdFlags)
Expand Down
25 changes: 25 additions & 0 deletions cors_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cors

import (
"bytes"
"fmt"
"net/http"
"net/http/httptest"
"regexp"
Expand Down Expand Up @@ -505,6 +507,29 @@ func TestDebug(t *testing.T) {
}
}

type testLogger struct {
buf *bytes.Buffer
}

func (l *testLogger) Printf(format string, v ...interface{}) {
fmt.Fprintf(l.buf, format, v...)
}

func TestLogger(t *testing.T) {
logger := &testLogger{buf: &bytes.Buffer{}}
s := New(Options{
Logger: logger,
})

if s.Log == nil {
t.Error("Logger not created when Logger is set")
}
s.logf("test")
if logger.buf.String() != "test" {
t.Error("Logger not used")
}
}

func TestDefault(t *testing.T) {
s := Default()
if s.Log != nil {
Expand Down

0 comments on commit e90f167

Please sign in to comment.