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

Replace Echo logger middleware from default to Zerolog-based one #50

Merged
merged 3 commits into from
Dec 21, 2023
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
57 changes: 1 addition & 56 deletions cmd/cm-beetle/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,58 +30,11 @@ import (
restServer "github.com/cloud-barista/cm-beetle/pkg/api/rest/server"

// Black import (_) is for running a package's init() function without using its other contents.
_ "github.com/cloud-barista/cm-beetle/pkg/config"
_ "github.com/cloud-barista/cm-beetle/pkg/logger"
"github.com/rs/zerolog/log"
)

// // setConfig get cloud settings from a config file
// func setConfig(profile string) {
// viper.AddConfigPath(".") // optionally look for config in the working directory
// viper.AddConfigPath("./conf/") // optionally look for config in the working directory/conf/
// viper.AddConfigPath("../conf/")
// viper.SetConfigName(profile)
// viper.SetConfigType("yaml")
// err := viper.ReadInConfig()
// if err != nil { // Handle errors reading the config filemak
// panic(fmt.Errorf("fatal error config file: %w", err))
// }
// err = viper.Unmarshal(&common.RuntimeConf)
// if err != nil {
// panic(err)
// }

// // const mrttArrayXMax = 300
// // const mrttArrayYMax = 300
// // common.RuntimeLatancyMap = make([][]string, mrttArrayXMax)

// // cloudlatencymap.csv
// file, fileErr := os.Open("../assets/cloudlatencymap.csv")
// if fileErr != nil {
// common.CBLog.Error(fileErr)
// panic(fileErr)
// }
// defer file.Close()

// rdr := csv.NewReader(bufio.NewReader(file))
// common.RuntimeLatancyMap, _ = rdr.ReadAll()

// for i, v := range common.RuntimeLatancyMap {
// if i == 0 {
// continue
// }
// if v[0] == "" {
// break
// }
// common.RuntimeLatancyMapIndex[v[0]] = i
// }

// //fmt.Printf("RuntimeLatancyMap: %v\n\n", common.RuntimeLatancyMap)
// //fmt.Printf("[RuntimeLatancyMapIndex]\n %v\n", common.RuntimeLatancyMapIndex)

// }

// Main Body

func main() {

log.Info().Msg("starting CM-Beetle server")
Expand Down Expand Up @@ -148,13 +101,5 @@ func main() {
wg.Done()
}()

// Note: Deprecated gRPC server
// Start gRPC Server
// go func() {
// grpcServer.RunServer()
// wg.Done()
// }()
// fmt.Println("RuntimeConf: ", common.RuntimeConf.Cloud)

wg.Wait()
}
57 changes: 54 additions & 3 deletions pkg/api/rest/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,57 @@ func RunServer(port string) {
e := echo.New()

// Middleware
e.Use(middleware.Logger())
// e.Use(middleware.Logger()) // default logger middleware in echo

// Custom logger middleware with zerolog
e.Use(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
},
}))

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 Down Expand Up @@ -148,6 +198,7 @@ func RunServer(port string) {

// Route for system management
e.GET("/beetle/swagger/*", echoSwagger.WrapHandler)

// e.GET("/beetle/swaggerActive", rest_common.RestGetSwagger)
e.GET("/beetle/health", rest_common.RestGetHealth)
e.GET("/beetle/httpVersion", rest_common.RestCheckHTTPVersion)
Expand Down Expand Up @@ -221,8 +272,8 @@ func RunServer(port string) {
// Block until a signal is triggered
<-gracefulShutdownContext.Done()

fmt.Println("\n[Stop] CM-Beetle REST Server")
log.Info().Msg("stopping CM-Beetle REST Server")
fmt.Println("\n[Stop] CM-Beetle REST API server")
log.Info().Msg("stopping CM-Beetle REST API server")
ctx, cancel := context.WithTimeout(context.TODO(), 3*time.Second)
defer cancel()

Expand Down
8 changes: 4 additions & 4 deletions pkg/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,6 @@ func getLogFileConfig() (string, int, int, int, bool) {

// Set config values
logFilePath := viper.GetString("logfile.path")
maxSize, err := strconv.Atoi(viper.GetString("logfile.maxsize"))
maxBackups, err := strconv.Atoi(viper.GetString("logfile.maxbackups"))
maxAge, err := strconv.Atoi(viper.GetString("logfile.maxage"))
compress, err := strconv.ParseBool(viper.GetString("logfile.compress"))

// Default: cm-beetle.log
if logFilePath == "" {
Expand All @@ -148,24 +144,28 @@ func getLogFileConfig() (string, int, int, int, bool) {
}

// Default: 10 MB
maxSize, err := strconv.Atoi(viper.GetString("logfile.maxsize"))
if err != nil {
log.Warn().Msgf("Invalid LOGFILE_MAXSIZE value: %s. Using default value: 10 MB", viper.GetString("logfile.maxsize"))
maxSize = 10
}

// Default: 3 backups
maxBackups, err := strconv.Atoi(viper.GetString("logfile.maxbackups"))
if err != nil {
log.Warn().Msgf("Invalid LOGFILE_MAXBACKUPS value: %s. Using default value: 3 backups", viper.GetString("logfile.maxbackups"))
maxBackups = 3
}

// Default: 30 days
maxAge, err := strconv.Atoi(viper.GetString("logfile.maxage"))
if err != nil {
log.Warn().Msgf("Invalid LOGFILE_MAXAGE value: %s. Using default value: 30 days", viper.GetString("logfile.maxage"))
maxAge = 30
}

// Default: false
compress, err := strconv.ParseBool(viper.GetString("logfile.compress"))
if err != nil {
log.Warn().Msgf("Invalid LOGFILE_COMPRESS value: %s. Using default value: false", viper.GetString("logfile.compress"))
compress = false
Expand Down