Skip to content

Commit

Permalink
chore: access log format contains logrus field when printed to stdout (
Browse files Browse the repository at this point in the history
…#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 <[email protected]>
  • Loading branch information
marselsampe and marselab authored Sep 3, 2021
1 parent 5381164 commit 9b53e0d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
6 changes: 5 additions & 1 deletion pkg/logger/common/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
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
27 changes: 23 additions & 4 deletions pkg/logger/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,24 @@ var (
FullAccessLogEnabled bool
FullAccessLogSupportedContentTypes []string
FullAccessLogMaxBodySize int

fullAccessLogLogger *logrus.Logger
)

const (
commonLogFormat = `%s - %s [%s] "%s %s %s" %d %d %d`
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)
Expand All @@ -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
}
}

Expand Down Expand Up @@ -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})
Expand Down Expand Up @@ -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(),
Expand Down
1 change: 0 additions & 1 deletion pkg/trace/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 9b53e0d

Please sign in to comment.