Skip to content

Commit

Permalink
Merge pull request #1477 from yunkon-kim/yunkon-kim-240329-1
Browse files Browse the repository at this point in the history
Add Zerolog-based logger and update dependencies
  • Loading branch information
seokho-son authored Apr 1, 2024
2 parents 65126d1 + 8aed936 commit a8184e7
Show file tree
Hide file tree
Showing 10 changed files with 286 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,6 @@ src/rttmap.csv
.vscode/

log/
*.log
*.history

10 changes: 9 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,15 @@ ENV CBTUMBLEBUG_ROOT=/app \
API_PASSWORD=default \
AUTOCONTROL_DURATION_MS=10000 \
SELF_ENDPOINT=localhost:1323 \
API_DOC_PATH=/app/src/api/rest/docs/swagger.json
API_DOC_PATH=/app/src/api/rest/docs/swagger.json \
LOGFILE_PATH=$CBTUMBLEBUG_ROOT/log/tumblebug.log \
LOGFILE_MAXSIZE=10 \
LOGFILE_MAXBACKUPS=3 \
LOGFILE_MAXAGE=30 \
LOGFILE_COMPRESS=false \
LOGLEVEL=debug \
LOGWRITER=both \
NODE_ENV=development

# Setting the entrypoint for the application
ENTRYPOINT [ "/app/src/cb-tumblebug" ]
Expand Down
14 changes: 14 additions & 0 deletions conf/setup.env
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,17 @@ export AUTOCONTROL_DURATION_MS=10000
# Environment variables that you don't need to touch
## Swagger UI API document file path
export API_DOC_PATH=$CBTUMBLEBUG_ROOT/src/api/rest/docs/swagger.json

## Logger configuration
# Set log file path (default logfile path: ./log/tumblebug.log)
export LOGFILE_PATH=$CBTUMBLEBUG_ROOT/log/tumblebug.log
export LOGFILE_MAXSIZE=10
export LOGFILE_MAXBACKUPS=3
export LOGFILE_MAXAGE=30
export LOGFILE_COMPRESS=false
# Set log level, such as trace, debug info, warn, error, fatal, and panic
export LOGLEVEL=debug
# Set log writer, such as file, stdout, or both
export LOGWRITER=both
# Set execution environment, such as development or production
export NODE_ENV=development
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ require (
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/rs/zerolog v1.32.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
Expand All @@ -91,7 +92,7 @@ require (
go.uber.org/zap v1.26.0 // indirect
golang.org/x/exp v0.0.0-20240112132812-db7319d0e0e3 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.17.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hd
github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
Expand Down Expand Up @@ -232,6 +233,8 @@ github.com/rs/xid v1.5.0 h1:mKX4bl4iPYJtEIxp6CYiUuLQ/8DYMoz0PUdtGgMFRVc=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.13.0/go.mod h1:YbFCdg8HfsridGWAh22vktObvhZbQsZXe4/zB0OKkWU=
github.com/rs/zerolog v1.15.0/go.mod h1:xYTKnLHcpfU2225ny5qZjxnj9NvkumZYjJHlAThCjNc=
github.com/rs/zerolog v1.32.0 h1:keLypqrlIjaFsbmJOBdB/qvyF8KEtCWHwobLp5l/mQ0=
github.com/rs/zerolog v1.32.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
Expand Down Expand Up @@ -415,9 +418,12 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
Expand Down
2 changes: 2 additions & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -406,11 +406,13 @@ github.com/prometheus/client_golang v1.11.1/go.mod h1:Z6t4BnS23TR94PD6BsDNk8yVqr
github.com/prometheus/client_model v0.2.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/common v0.26.0/go.mod h1:M7rCNAaPfAosfx8veZJCuw84e35h3Cfd9VFqTh1DIvc=
github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
github.com/rs/zerolog v1.15.0 h1:uPRuwkWF4J6fGsJ2R0Gn2jB1EQiav9k3S6CSdygQJXY=
github.com/sagikazarmark/crypt v0.17.0/go.mod h1:SMtHTvdmsZMuY/bpZoqokSoChIrcJ/epOxZN58PbZDg=
github.com/swaggo/files v0.0.0-20220728132757-551d4a08d97a h1:kAe4YSu0O0UFn1DowNo2MY5p6xzqtJ/wQ7LZynSvGaY=
github.com/swaggo/files v0.0.0-20220728132757-551d4a08d97a/go.mod h1:lKJPbtWzJ9JhsTN1k1gZgleJWY/cqq0psdoMmaThG3w=
go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo=
go.uber.org/atomic v1.10.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
golang.org/x/oauth2 v0.15.0/go.mod h1:q48ptWNTY5XWf+JNten23lcvHpLJ0ZSxF5ttTHKVCAM=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2/go.mod h1:K8+ghG5WaK9qNqU5K3HdILfMLy1f3aNYFI/wnl100a8=
google.golang.org/appengine v1.6.8/go.mod h1:1jJ3jBArFh5pcgW8gCtRJnepW8FzD1V44FJffLiz/Ds=
57 changes: 57 additions & 0 deletions src/api/rest/server/middlewares/custom-middleware.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package middlewares

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

func Zerologger() echo.MiddlewareFunc {
return middleware.RequestLoggerWithConfig(middleware.RequestLoggerConfig{
LogError: true,
LogRequestID: true,
LogRemoteIP: true,
LogHost: true,
LogMethod: true,
LogURI: true,
LogUserAgent: true,
LogStatus: true,
LogLatency: true,
LogContentLength: true,
LogResponseSize: true,
// HandleError: true, // forwards error to the global error handler, so it can decide appropriate status code
LogValuesFunc: func(c echo.Context, v middleware.RequestLoggerValues) error {
if v.Error == nil {
log.Info().
Str("id", v.RequestID).
Str("remote_ip", v.RemoteIP).
Str("host", v.Host).
Str("method", v.Method).
Str("URI", v.URI).
Str("user_agent", v.UserAgent).
Int("status", v.Status).
Int64("latency", v.Latency.Nanoseconds()).
Str("latency_human", v.Latency.String()).
Str("bytes_in", v.ContentLength).
Int64("bytes_out", v.ResponseSize).
Msg("request")
} else {
log.Error().
Err(v.Error).
Str("id", v.RequestID).
Str("remote_ip", v.RemoteIP).
Str("host", v.Host).
Str("method", v.Method).
Str("URI", v.URI).
Str("user_agent", v.UserAgent).
Int("status", v.Status).
Int64("latency", v.Latency.Nanoseconds()).
Str("latency_human", v.Latency.String()).
Str("bytes_in", v.ContentLength).
Int64("bytes_out", v.ResponseSize).
Msg("request error")
}
return nil
},
})
}
16 changes: 11 additions & 5 deletions src/api/rest/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,20 @@ package server

import (
"context"
"log"
// "log"
"os/signal"
"sync"
"syscall"
"time"

"github.com/cloud-barista/cb-tumblebug/src/core/common"

"github.com/rs/zerolog/log"

rest_common "github.com/cloud-barista/cb-tumblebug/src/api/rest/server/common"
rest_mcir "github.com/cloud-barista/cb-tumblebug/src/api/rest/server/mcir"
rest_mcis "github.com/cloud-barista/cb-tumblebug/src/api/rest/server/mcis"
"github.com/cloud-barista/cb-tumblebug/src/api/rest/server/middlewares"
rest_netutil "github.com/cloud-barista/cb-tumblebug/src/api/rest/server/util"

"crypto/subtle"
Expand Down Expand Up @@ -79,10 +82,13 @@ const (
// RunServer func start Rest API server
func RunServer(port string) {

log.Info().Msg("REST API Server is starting")

e := echo.New()

// Middleware
e.Use(middleware.Logger())
// e.Use(middleware.Logger())
e.Use(middlewares.Zerologger())
e.Use(middleware.Recover())
// limit the application to 20 requests/sec using the default in-memory store
e.Use(middleware.RateLimiter(middleware.NewRateLimiterMemoryStore(20)))
Expand All @@ -109,7 +115,7 @@ func RunServer(port string) {

allowedOrigins := os.Getenv("ALLOW_ORIGINS")
if allowedOrigins == "" {
log.Fatal("ALLOW_ORIGINS env variable for CORS is " + allowedOrigins +
log.Fatal().Msgf("ALLOW_ORIGINS env variable for CORS is " + allowedOrigins +
". Please provide a proper value and source setup.env again. EXITING...")
// allowedOrigins = "*"
}
Expand Down Expand Up @@ -449,14 +455,14 @@ func RunServer(port string) {
defer cancel()

if err := e.Shutdown(ctx); err != nil {
log.Println("Error starting the server: ", err)
log.Error().Err(err).Msg("Error starting the server")
e.Logger.Panic(err)
}
}(&wg)

port = fmt.Sprintf(":%s", port)
if err := e.Start(port); err != nil && err != http.ErrServerClosed {
log.Println("Error starting the server: ", err)
log.Error().Err(err).Msg("Error starting the server")
e.Logger.Panic("Shuttig down the server: ", err)
}

Expand Down
Loading

0 comments on commit a8184e7

Please sign in to comment.