8000 middleware: add LoggerConfig with Skipper func by kraem · Pull Request #9 · modfin/eal · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

middleware: add LoggerConfig with Skipper func #9

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
23 changes: 22 additions & 1 deletion middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@ const (
contextName = "mfContextLogFields"
)

type Skipper func(c echo.Context) bool

func DefaultSkipper(echo.Context) bool {
return false
}

type LoggerConfig struct {
Skipper Skipper
}

var DefaultLoggerConfig = LoggerConfig{
Skipper: DefaultSkipper,
}

// ContextLogFunc can be implemented to be able to add log fields from an echo context.
type ContextLogFunc func(c echo.Context, fields Fields)

Expand Down Expand Up @@ -63,13 +77,16 @@ var DefaultContextLogFunc = func(c echo.Context, fields Fields) {
// If an error is returned from the handlerFunc, the middleware will look at the complete error-chain to find the
// earliest echo.HTTPError, and return the status code and message from that to the frontend.
// If the error-chain don't contain an echo.HTTPError, a new echo.HTTPError will be created that wrap the returned error.
func CreateLoggerMiddleware(logFunctions ...ContextLogFunc) echo.MiddlewareFunc {
func CreateLoggerMiddleware(config LoggerConfig, logFunctions ...ContextLogFunc) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) (err error) {
// Init
if len(logFunctions) == 0 {
logFunctions = []ContextLogFunc{DefaultContextLogFunc}
}
if config.Skipper == nil {
config.Skipper = DefaultSkipper
}
logFields := Fields{}
for _, f := range logFunctions {
f(c, logFields)
Expand Down Expand Up @@ -112,6 +129,10 @@ func CreateLoggerMiddleware(logFunctions ...ContextLogFunc) echo.MiddlewareFunc
msg = "access"
}

if config.Skipper(c) {
return nil
}

if _, ok := logEntry.Data[errorMessage]; ok {
logEntry.Error(msg)
} else {
Expand Down
0