Skip to content

Commit

Permalink
Merge pull request #72 from yunkon-kim/240625-21
Browse files Browse the repository at this point in the history
Add skipper on Zerologger middleware
  • Loading branch information
yunkon-kim authored Jun 25, 2024
2 parents 60e0aa9 + 3f6f778 commit 16ec48e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
21 changes: 20 additions & 1 deletion pkg/api/rest/custommiddleware/custom-middleware.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
package custommiddleware

import (
"strings"

"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/rs/zerolog/log"
)

func Zerologger() echo.MiddlewareFunc {
func Zerologger(skipPatterns [][]string) echo.MiddlewareFunc {
return middleware.RequestLoggerWithConfig(middleware.RequestLoggerConfig{
Skipper: func(c echo.Context) bool {
path := c.Request().URL.Path
query := c.Request().URL.RawQuery
for _, patterns := range skipPatterns {
isAllMatched := true
for _, pattern := range patterns {
if !strings.Contains(path+query, pattern) {
isAllMatched = false
break
}
}
if isAllMatched {
return true
}
}
return false
},
LogError: true,
LogRequestID: true,
LogRemoteIP: true,
Expand Down
31 changes: 23 additions & 8 deletions pkg/api/rest/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,25 @@ const (
const (
website = " https://github.com/cloud-barista/mc-terrarium"
banner = `
________________________________________________
██████╗ ███████╗ █████╗ ██████╗ ██╗ ██╗
██╔══██╗██╔════╝██╔══██╗██╔══██╗╚██╗ ██╔╝
██████╔╝█████╗ ███████║██║ ██║ ╚████╔╝
██╔══██╗██╔══╝ ██╔══██║██║ ██║ ╚██╔╝
██║ ██║███████╗██║ ██║██████╔╝ ██║
╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝╚═════╝ ╚═╝
Computing Infrastructure Migration Technology
████████╗███████╗██████╗ ██████╗ █████╗ ██████╗ ██╗██╗ ██╗███╗ ███╗
╚══██╔══╝██╔════╝██╔══██╗██╔══██╗██╔══██╗██╔══██╗██║██║ ██║████╗ ████║
██║ █████╗ ██████╔╝██████╔╝███████║██████╔╝██║██║ ██║██╔████╔██║
██║ ██╔══╝ ██╔══██╗██╔══██╗██╔══██║██╔══██╗██║██║ ██║██║╚██╔╝██║
██║ ███████╗██║ ██║██║ ██║██║ ██║██║ ██║██║╚██████╔╝██║ ╚═╝ ██║
╚═╝ ╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═════╝ ╚═╝ ╚═╝
Mutli-cloud Infrastructure Enrichment Technology
________________________________________________`
)

Expand Down Expand Up @@ -125,8 +135,13 @@ func RunServer(port string) {
// Middleware
// e.Use(middleware.Logger()) // default logger middleware in echo

APILogSkipPatterns := [][]string{
{"/terrarium/api"},
// {"/mcis", "option=status"},
}

// Custom logger middleware with zerolog
e.Use(custommiddleware.Zerologger())
e.Use(custommiddleware.Zerologger(APILogSkipPatterns))

// Recover middleware recovers from panics anywhere in the chain, and handles the control to the centralized HTTP error handler.
e.Use(middleware.Recover())
Expand Down Expand Up @@ -178,12 +193,12 @@ func RunServer(port string) {
}))
}

fmt.Println("\n \n ")
fmt.Print("\n ")
fmt.Print(banner)
fmt.Println("\n ")
fmt.Println("\n ")
fmt.Print("\n\n")
fmt.Println(" Website/repository: ")
fmt.Printf(infoColor, website)
fmt.Println("\n \n ")
fmt.Print("\n\n")

// Route for system management
swaggerRedirect := func(c echo.Context) error {
Expand Down Expand Up @@ -214,7 +229,7 @@ func RunServer(port string) {
apidashboard := " http://" + selfEndpoint + "/terrarium/api"

if enableAuth {
fmt.Println(" Access to API dashboard" + " (username: " + apiUser + " / password: " + apiPass + ")")
fmt.Println(" Access to API dashboard" + " (username: " + apiUser + " / password: " + apiPass + "): ")
}
fmt.Printf(noticeColor, apidashboard)
fmt.Println("\n ")
Expand Down

0 comments on commit 16ec48e

Please sign in to comment.