Skip to content

Commit

Permalink
custom formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
Tschonti committed Oct 30, 2024
1 parent b690434 commit eb55a07
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ To run the service locally, follow these steps:

- Clone this repository
```sh
git clone https://github.com/tum-calendar-proxy/tum-calendar-proxy.git
git clone https://github.com/TUM-Dev/CalendarProxy.git
```
- Navigate to the project directory:
- Navigate to the project directory:
```sh
cd tum-calendar-proxy
cd CalendarProxy
```
- Run the proxy server:
```sh
Expand Down
41 changes: 40 additions & 1 deletion internal/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"regexp"
"sort"
"strings"
Expand Down Expand Up @@ -71,6 +72,44 @@ func newApp() (*App, error) {
return &a, nil
}

func customLogFormatter(params gin.LogFormatterParams) string {
return fmt.Sprintf("[GIN] %v |%s %3d %s | %13v | %15s |%s %-7s%s %#v\n%s",
params.TimeStamp.Format("2006/01/02 - 15:04:05"),
params.StatusCodeColor(),
params.StatusCode,
params.ResetColor(),
params.Latency,
params.ClientIP,
params.MethodColor(),
params.Method,
params.ResetColor(),
hideTokens(params.Path),
params.ErrorMessage,
)
}

func hideTokens(path string) string {
u, err := url.Parse(path)
if err != nil {
return path
}

pStud := u.Query().Get("pStud")
pPers := u.Query().Get("pPers")
pToken := u.Query().Get("pToken")

if pToken == "" || (pStud == "" && pPers == "") {
return path
}

manyXes := strings.Repeat("X", 12)
tokenReplaced := pToken[:4] + manyXes
if pStud != "" {
return fmt.Sprintf("/?pStud=%s&pToken=%s", pStud[:4]+manyXes, tokenReplaced)
}
return fmt.Sprintf("/?pPers=%s&pToken=%s", pPers[:4]+manyXes, tokenReplaced)
}

func (a *App) Run() error {
if err := sentry.Init(sentry.ClientOptions{
Dsn: "https://[email protected]/4",
Expand All @@ -93,7 +132,7 @@ func (a *App) Run() error {
gin.SetMode("release")
a.engine = gin.New()
a.engine.Use(sentrygin.New(sentrygin.Options{}))
logger := gin.LoggerWithConfig(gin.LoggerConfig{SkipPaths: []string{"/health"}})
logger := gin.LoggerWithConfig(gin.LoggerConfig{SkipPaths: []string{"/health"}, Formatter: customLogFormatter})
a.engine.Use(logger, gin.Recovery())
a.configRoutes()

Expand Down

0 comments on commit eb55a07

Please sign in to comment.