From 9b53e0d7f2f2a5cff7403759720853fd7395a18b Mon Sep 17 00:00:00 2001 From: Marsel Sampe Asang Date: Fri, 3 Sep 2021 05:02:14 -0800 Subject: [PATCH] chore: access log format contains logrus field when printed to stdout (#45) * chore: access log format contains logrus field when printed to stdout CORE-4694 * fix: Full access log timestamp not printed in UTC * chore: Create custom logrus for full access log format Co-authored-by: marselab --- pkg/logger/common/README.md | 6 +++++- pkg/logger/common/common.go | 27 +++++++++++++++++++++++---- pkg/trace/README.md | 1 - 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/pkg/logger/common/README.md b/pkg/logger/common/README.md index a867d7a..41bcd1f 100644 --- a/pkg/logger/common/README.md +++ b/pkg/logger/common/README.md @@ -33,4 +33,8 @@ Enable full access log mode. Default: false. #### FULL_ACCESS_LOG_SUPPORTED_CONTENT_TYPES Supported content types to shown in request_body and response_body log. -Default: application/json,application/xml,application/x-www-form-urlencoded,text/plain,text/html. \ No newline at end of file +Default: application/json,application/xml,application/x-www-form-urlencoded,text/plain,text/html. + +#### FULL_ACCESS_LOG_MAX_BODY_SIZE +Maximum size of request body or response body that will be processed, will be ignored if exceed more than it. +Default: 10240 bytes \ No newline at end of file diff --git a/pkg/logger/common/common.go b/pkg/logger/common/common.go index 9eb9e79..3864f5a 100644 --- a/pkg/logger/common/common.go +++ b/pkg/logger/common/common.go @@ -36,6 +36,8 @@ var ( FullAccessLogEnabled bool FullAccessLogSupportedContentTypes []string FullAccessLogMaxBodySize int + + fullAccessLogLogger *logrus.Logger ) const ( @@ -43,6 +45,15 @@ const ( fullAccessLogFormat = `time=%s log_type=access method=%s path="%s" status=%d duration=%d length=%d source_ip=%s user_agent="%s" referer="%s" trace_id=%s namespace=%s user_id=%s client_id=%s request_content_type="%s" request_body=AB[%s]AB response_content_type="%s" response_body=AB[%s]AB` ) +// fullAccessLogFormatter represent logrus.Formatter, +// this is used to print the custom format for access log. +type fullAccessLogFormatter struct { +} + +func (f *fullAccessLogFormatter) Format(entry *logrus.Entry) ([]byte, error) { + return []byte(entry.Message + "\n"), nil +} + func init() { if s, exists := os.LookupEnv("FULL_ACCESS_LOG_ENABLED"); exists { value, err := strconv.ParseBool(s) @@ -58,14 +69,13 @@ func init() { FullAccessLogSupportedContentTypes = []string{"application/json", "application/xml", "application/x-www-form-urlencoded", "text/plain", "text/html"} } + FullAccessLogMaxBodySize = 10 << 10 // 10KB if s, exists := os.LookupEnv("FULL_ACCESS_LOG_MAX_BODY_SIZE"); exists { value, err := strconv.ParseInt(s, 0, 64) if err != nil { logrus.Errorf("Parse FULL_ACCESS_LOG_MAX_BODY_SIZE env error: %v", err) } FullAccessLogMaxBodySize = int(value) - } else { - FullAccessLogMaxBodySize = 1 << 20 // 1MB } } @@ -107,6 +117,15 @@ func simpleAccessLogFilter(req *restful.Request, resp *restful.Response, chain * // fullAccessLogFilter will print the access log in complete log format func fullAccessLogFilter(req *restful.Request, resp *restful.Response, chain *restful.FilterChain) { + // initialize custom logger for full access log + if fullAccessLogLogger == nil { + fullAccessLogLogger = &logrus.Logger{ + Out: os.Stdout, + Level: logrus.GetLevel(), + Formatter: &fullAccessLogFormatter{}, + } + } + start := time.Now() sourceIP := publicsourceip.PublicIP(&http.Request{Header: req.Request.Header}) @@ -134,8 +153,8 @@ func fullAccessLogFilter(req *restful.Request, resp *restful.Response, chain *re duration := time.Since(start) - logrus.Infof(fullAccessLogFormat, - time.Now().Format("2006-01-02T15:04:05.000Z"), + fullAccessLogLogger.Infof(fullAccessLogFormat, + time.Now().UTC().Format("2006-01-02T15:04:05.000Z"), req.Request.Method, req.Request.URL.RequestURI(), resp.StatusCode(), diff --git a/pkg/trace/README.md b/pkg/trace/README.md index 8e68466..27ee6d6 100644 --- a/pkg/trace/README.md +++ b/pkg/trace/README.md @@ -16,7 +16,6 @@ Filter is restful.FilterFunction for generating traceID (X-Ab-TraceID header fie #### Example usage of filter for all endpoints -#### Example usage of filter for all endpoints Simple initialization: ```go ws := new(restful.WebService)