event

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2019 License: Apache-2.0 Imports: 4 Imported by: 0

README

Event Logger

This package enables logging using AccelByte's Event Log Format in go-restful apps.

Usage

Importing
import "github.com/AccelByte/go-restful-plugins/pkg/logger/event"
Log all endpoints
ws := new(restful.WebService)
ws.Filter(event.Log("realm", event.ExtractNull))
Log specific endpoint
ws := new(restful.WebService)
ws.Route(ws.GET("/user/{id}").
    Filter(event.Log("realm", event.ExtractNull)).
    To(func(request *restful.Request, response *restful.Response) {
}))
Actor User ID & Namespace

If you are using IAM Auth Filter, you can use this extractFunc for extracting iam.JWTClaims from the restful.Request to get the actor's user ID and namespace.

extractFunc := func(req *restful.Request) (userID string, clientID string, namespace string) {
    claims := iam.RetrieveJWTClaims(req)
    if claims != nil {
        return claims.Subject, claims.Audience, claims.Namespace
    }
    return "", "", ""
}

However, if you are using your own auth filter, you need to set the attributes to the request when registering the route and you need to implement your custom extractFunc as well. For example:

Set the attributes to the request in the auth filter function

// get token
token := parseToken(request)

// parse the JWT claims
claims := parseClaim(token)

// set the request attribute with claims
request.SetAttribute("userID", claims.Audience)
request.SetAttribute("clientID", claims.Subject)
request.SetAttribute("namespace", claims.Namespace)

The logging filter function

extractFunc := func(req *restful.Request) (userID string, clientID string, namespace string){
	userID, _ = req.Attribute("userID").(string)
	clientID, _ = req.Attribute("clientID").(string)
	namespace, _ = req.Attribute("namespace").(string)
	
	return userID, clientID, namespace
}

ws := new(restful.WebService)
ws.Route(ws.GET("/user/{id}").
    Filter(event.Log("realm", extractFunc)).
    To(func(request *restful.Request, response *restful.Response) {
}))
Target User ID & Namespace

To put target user ID & namespace to the log, call:

event.TargetUser(req *restful.Request, id, namespace string)
Set event ID & log level

To put event ID & level, call one of:

event.Debug(req *restful.Request, eventID int, message ...string)
event.Info(req *restful.Request, eventID int, message ...string)
event.Warn(req *restful.Request, eventID int, message ...string)
event.Error(req *restful.Request, eventID int, message ...string)
event.Fatal(req *restful.Request, eventID int, message ...string)

You can put a log message there too.

Additional log fields

Add any additional log fields using

event.AdditionalFields(req *restful.Request, fields map[string]interface{})

Pay attention on the field key name not to overwrite the existing default fields.

Timestamp format

By importing this package, it will set the logrus timestamp for entire service to use RFC3339 in millisecond precision and will force the timezone to be UTC. This timestamp format is required for the AccelByte Event Log service.

However if you want to use your own timestamp format, you can override it by calling logrus.SetFormatter in the main() function of your code before writing any logs. This is the example of setting timestamp format to use RFC3339Nano.

logrus.SetFormatter(&logrus.TextFormatter{TimestampFormat: time.RFC3339Nano})

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AdditionalFields

func AdditionalFields(req *restful.Request, fields map[string]interface{})

AdditionalFields injects additional log fields to the event in the request

func Debug

func Debug(req *restful.Request, eventID int, message ...interface{})

Debug sets the event level to debug along with the event ID and message

func Error

func Error(req *restful.Request, eventID int, message ...interface{})

Error sets the event level to error along with the event ID and message

func Fatal

func Fatal(req *restful.Request, eventID int, message ...interface{})

Fatal sets the event level to fatal along with the event ID and message

func Info

func Info(req *restful.Request, eventID int, message ...interface{})

Info sets the event level to info along with the event ID and message

func Log

func Log(realm string, fn ExtractAttribute) restful.FilterFunction

Log is a filter that will log incoming request with AccelByte's Event Log Format

func TargetUser

func TargetUser(req *restful.Request, id, namespace string)

TargetUser injects the target user ID and namespace to current event in the request

func Warn

func Warn(req *restful.Request, eventID int, message ...interface{})

Warn sets the event level to warn along with the event ID and message

Types

type ExtractAttribute

type ExtractAttribute func(req *restful.Request) (userID string, clientID string, namespace string)

ExtractAttribute is a function to extract userID, clientID and namespace from restful.Request

var ExtractNull ExtractAttribute = func(_ *restful.Request) (userID string, clientID string, namespace string) {
	return "", "", ""
}

ExtractNull is null function for extracting attribute

type UTCFormatter

type UTCFormatter struct {
	logrus.Formatter
}

UTCFormatter implements logrus Formatter for custom log format

func (UTCFormatter) Format

func (formatter UTCFormatter) Format(log *logrus.Entry) ([]byte, error)

Format implements logrus Format for forcing time to UTC

Jump to

Keyboard shortcuts

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