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

Add ability to set the logger on echo.Context #1377

Merged
merged 1 commit into from
Nov 11, 2019
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
13 changes: 13 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ type (
// Logger returns the `Logger` instance.
Logger() Logger

// Set the logger
SetLogger(l Logger)

// Echo returns the `Echo` instance.
Echo() *Echo

Expand All @@ -199,6 +202,7 @@ type (
handler HandlerFunc
store Map
echo *Echo
logger Logger
lock sync.RWMutex
}
)
Expand Down Expand Up @@ -590,9 +594,17 @@ func (c *context) SetHandler(h HandlerFunc) {
}

func (c *context) Logger() Logger {
res := c.logger
if res != nil {
return res
}
return c.echo.Logger
}

func (c *context) SetLogger(l Logger) {
c.logger = l
}

func (c *context) Reset(r *http.Request, w http.ResponseWriter) {
c.request = r
c.response.reset(w)
Expand All @@ -601,6 +613,7 @@ func (c *context) Reset(r *http.Request, w http.ResponseWriter) {
c.store = nil
c.path = ""
c.pnames = nil
c.logger = nil
// NOTE: Don't reset because it has to have length c.echo.maxParam at all times
// c.pvalues = nil
}
12 changes: 11 additions & 1 deletion context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/xml"
"errors"
"fmt"
"github.com/labstack/gommon/log"
"io"
"math"
"mime/multipart"
Expand Down Expand Up @@ -800,7 +801,16 @@ func TestContext_Logger(t *testing.T) {
e := New()
c := e.NewContext(nil, nil)

testify.NotNil(t, c.Logger())
log1 := c.Logger()
testify.NotNil(t, log1)

log2 := log.New("echo2")
c.SetLogger(log2)
testify.Equal(t, log2, c.Logger())

// Resetting the context returns the initial logger
c.Reset(nil, nil)
testify.Equal(t, log1, c.Logger())
}

func TestContext_RealIP(t *testing.T) {
Expand Down