-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement structured logging with zerolog
Merge branch 'zerolog' logs will be pretty-printed to the console by development builds compiled with main.releaseMode=false and printed to stderr as JSON in release builds where main.releaseMode=true
- Loading branch information
Showing
7 changed files
with
166 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package logging | ||
|
||
import ( | ||
"fmt" | ||
|
||
"os" | ||
"time" | ||
"strings" | ||
|
||
// Structured logging | ||
"github.com/rs/zerolog" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
var Logger zerolog.Logger | ||
|
||
func InitLogger(level string, releaseMode bool) { | ||
zerolog.TimeFieldFormat = time.RFC3339 | ||
// Use colorful non-JSON log output during development builds | ||
if releaseMode { | ||
Logger = zerolog.New(os.Stderr).With().Timestamp().Logger() | ||
} else { | ||
Logger = zerolog.New( | ||
zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: "15:04:05", FormatLevel: func(i interface{}) string { | ||
return strings.ToUpper(fmt.Sprintf("%-5s", i)) | ||
}}, | ||
).With().Timestamp().Logger() | ||
} | ||
|
||
switch strings.ToLower(level) { | ||
case "error": | ||
zerolog.SetGlobalLevel(zerolog.ErrorLevel) | ||
case "warn": | ||
zerolog.SetGlobalLevel(zerolog.WarnLevel) | ||
case "info": | ||
zerolog.SetGlobalLevel(zerolog.InfoLevel) | ||
case "debug": | ||
zerolog.SetGlobalLevel(zerolog.DebugLevel) | ||
case "trace": | ||
zerolog.SetGlobalLevel(zerolog.TraceLevel) | ||
case "disable": | ||
zerolog.SetGlobalLevel(zerolog.Disabled) | ||
default: | ||
Logger.Fatal().Msgf("error parsing commandline arguments: invalid value \"%v\" for flag -logLevel: pass one of: disable, error, warn, info, debug, trace", level) | ||
} | ||
} | ||
|
||
// https://learninggolang.com/it5-gin-structured-logging.html | ||
func GinLogger() gin.HandlerFunc { | ||
return func(c *gin.Context) { | ||
path := c.Request.URL.Path | ||
raw := c.Request.URL.RawQuery | ||
|
||
// Process request | ||
c.Next() | ||
|
||
// Fill the params | ||
param := gin.LogFormatterParams{} | ||
|
||
param.TimeStamp = time.Now() // Stop timer | ||
|
||
param.ClientIP = c.ClientIP() | ||
param.Method = c.Request.Method | ||
param.StatusCode = c.Writer.Status() | ||
param.ErrorMessage = c.Errors.ByType(gin.ErrorTypePrivate).String() | ||
//param.Latency = duration | ||
param.BodySize = c.Writer.Size() | ||
if raw != "" { | ||
path = path + "?" + raw | ||
} | ||
param.Path = path | ||
|
||
// Log using the params | ||
var logEvent *zerolog.Event | ||
if c.Writer.Status() >= 500 { | ||
logEvent = Logger.Error() | ||
} else { | ||
logEvent = Logger.Info() | ||
} | ||
|
||
logEvent.Str("client_id", param.ClientIP). | ||
Str("method", param.Method). | ||
Int("status_code", param.StatusCode). | ||
Int("body_size", param.BodySize). | ||
Str("path", param.Path). | ||
//Str("latency", param.Latency.String()). | ||
Msg(param.ErrorMessage) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.