Skip to content

Commit

Permalink
Add zap logger support
Browse files Browse the repository at this point in the history
  • Loading branch information
vkuznet committed Aug 26, 2024
1 parent aed5084 commit 76d5fba
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ import (
"time"

rotatelogs "github.com/lestrrat-go/file-rotatelogs"
zap "go.uber.org/zap"
zapcore "go.uber.org/zap/zapcore"
)

// ZapLogger defines zap logger structure
var ZapLogger string

// CMSMonitType controls CMS Monit log record type
var CMSMonitType string

Expand Down Expand Up @@ -197,6 +202,22 @@ func parseHumanReadableTime(timeStr string) (float64, error) {
// helper function to log every single user request, here we pass pointer to status code
// as it may change through the handler while we use defer logRequest
func LogRequest(w http.ResponseWriter, r *http.Request, start time.Time, cauth string, status *int, tstamp int64, bytesOut int64) {
// config := zap.NewDevelopmentConfig()
config := zap.NewProductionConfig()
config.EncoderConfig = zapcore.EncoderConfig{
MessageKey: "msg", // We just need the message itself
}
if ZapLogger != "" {
config.Encoding = ZapLogger
}
// use unstructured zap logger
logger, e := config.Build()
if e != nil {
log.Fatal(e)
}
defer logger.Sync() // flushes buffer, if any
zapLog := logger.Sugar() // get sugar logger (JSON one)

// our apache configuration
// CustomLog "||@APACHE2_ROOT@/bin/rotatelogs -f @LOGDIR@/access_log_%Y%m%d.txt 86400" \
// "%t %v [client: %a] [backend: %h] \"%r\" %>s [data: %I in %O out %b body %D us ] [auth: %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%{SSL_CLIENT_S_DN}x\" \"%{cms-auth}C\" ] [ref: \"%{Referer}i\" \"%{User-Agent}i\" ]"
Expand Down Expand Up @@ -280,7 +301,12 @@ func LogRequest(w http.ResponseWriter, r *http.Request, start time.Time, cauth s
statusCode = c
}
}
log.Printf("%s %d %s %s %s %s %s %s %s\n", r.Proto, statusCode, r.Method, uri, dataMsg, addr, authMsg, refMsg, respMsg)
if ZapLogger != "" {
tstamp := time.Now().Format("[2006-01-02T15:04:05.000000-07:00]")
zapLog.Infof("%s %s %d %s %s %s %s %s %s %s\n", tstamp, r.Proto, statusCode, r.Method, uri, dataMsg, addr, authMsg, refMsg, respMsg)
} else {
log.Printf("%s %d %s %s %s %s %s %s %s\n", r.Proto, statusCode, r.Method, uri, dataMsg, addr, authMsg, refMsg, respMsg)
}
if CMSMonitType == "" || CMSMonitProducer == "" {
return
}
Expand Down

0 comments on commit 76d5fba

Please sign in to comment.