log

package
v4.21.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 26, 2024 License: Apache-2.0 Imports: 17 Imported by: 2

README

Access Log Format Logger

Access log mode is used to view the full body detail of all request and response in the endpoints.

Usage

Importing
import "github.com/AccelByte/go-restful-plugins/v4/pkg/logger/log"
Log all endpoints
ws := new(restful.WebService)
ws.Filter(log.AccessLog)
Log specific endpoint
ws := new(restful.WebService)
ws.Route(ws.GET("/user/{id}").
    Filter(log.AccessLog).
    To(func(request *restful.Request, response *restful.Response) {
}))
Environment variables
  • FULL_ACCESS_LOG_ENABLED

    Full access log mode will capture request body and response body. 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

  • 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

  • FULL_ACCESS_LOG_REQUEST_BODY_ENABLED

    Enable capture request body in full access log mode. Default: true

  • FULL_ACCESS_LOG_RESPONSE_BODY_ENABLED

    Enable capture response body in full access log mode. Default: true

Filter sensitive field(s) in request body or response body

Some endpoint might have sensitive field value in its query params, request body or response body. For security reason, those sensitive field value should be masked before it printed as a log.

The log.Attribute filter can be used to define the field(s) that need to be masked.

ws := new(restful.WebService)
ws.Route(ws.GET("/user/{id}").
    Filter(log.AccessLog).
    Filter(log.Attribute(log.Option{
        MaskedQueryParams: "param1,param2",
        MaskedRequestFields: "field1,field2",
        MaskedResponseFields: "field3,field4",
    })).
    To(func(request *restful.Request, response *restful.Response) {
}))
Manually specify log's field value

We could manually set specific field value via request attribute. Please refer to the table below for attribute name of each field.

Field Default Value Attribute Name
namespace Extracted from JWT claims log.NamespaceAttribute
user_id Extracted from JWT claims log.UserIDAttribute
client_id Extracted from JWT claims log.ClientIDAttribute

Example:

// ... your service logic
request.SetAttribute(log.NamespaceAttribute, "myNamespace")
request.SetAttribute(log.UserIDAttribute, "myUserId")
request.SetAttribute(log.ClientIDAttribute, "myClientId")
// ... your service logic

Documentation

Index

Constants

View Source
const (
	MaskedQueryParamsAttribute    = "MaskedQueryParams"
	MaskedRequestFieldsAttribute  = "MaskedRequestFields"
	MaskedResponseFieldsAttribute = "MaskedResponseFields"
	UserIDAttribute               = "LogUserId"
	ClientIDAttribute             = "LogClientId"
	NamespaceAttribute            = "LogNamespace"
)
View Source
const (
	MaskedValue = "******"
)

Variables

View Source
var (
	FullAccessLogEnabled               bool
	FullAccessLogSupportedContentTypes []string
	FullAccessLogMaxBodySize           int
	FullAccessLogRequestBodyEnabled    bool
	FullAccessLogResponseBodyEnabled   bool
)
View Source
var FieldRegexCache = sync.Map{}

Functions

func AccessLog added in v4.10.0

func AccessLog(req *restful.Request, resp *restful.Response, chain *restful.FilterChain)

AccessLog is a filter that will log incoming request into the Access Log format

func Attribute

func Attribute(option Option) restful.FilterFunction

Attribute filter is used to define the log attribute for the endpoint.

func MaskFields

func MaskFields(contentType, content, fields string) string

MaskFields will mask the field value on the content string based on the provided field name(s) in "fields" parameter separated by comma.

func MaskQueryParams

func MaskQueryParams(uri string, fields string) string

MaskQueryParams will mask the field value on the uri based on the provided field name(s) in "fields" parameter separated by comma.

Types

type FieldRegex

type FieldRegex struct {
	FieldName          string
	JsonPattern        *regexp.Regexp
	QueryStringPattern *regexp.Regexp
}

FieldRegex contains regex patterns for field name in varied content-types.

func (*FieldRegex) InitFieldRegex

func (f *FieldRegex) InitFieldRegex(fieldName string)

InitFieldRegex initialize the FieldRegex along with its regex patterns.

type Option

type Option struct {
	// Query param that need to masked in url, separated with comma
	MaskedQueryParams string
	// Field that need to masked in request body, separated with comma
	MaskedRequestFields string
	// Field that need to masked in response body, separated with comma
	MaskedResponseFields string
}

Option contains attribute options for log functionality

type ResponseWriterInterceptor added in v4.10.0

type ResponseWriterInterceptor struct {
	http.ResponseWriter
	// contains filtered or unexported fields
}

ResponseWriterInterceptor is used to decorate http.ResponseWriter, so we can intercept the Write process

func (*ResponseWriterInterceptor) Write added in v4.10.0

func (w *ResponseWriterInterceptor) Write(b []byte) (int, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL