logevent

package module
v1.6.1 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2021 License: Apache-2.0 Imports: 12 Imported by: 4

README

logevent - A structured event logger abstraction

GoDoc Build Status codecov.io

Usage

Defining Events

There are no log events defined within this project. Instead, developers are free to create and maintain their own events structures and schema as preferred. This project relies on the tag annotation feature, like JSON, to render an event to the log stream.

Each event emitted must be defined as a struct. Each attribute of the struct must be annotated with a logevent tag that identifies the desired field name as it appears in the logs. Each struct must contain a Message attribute. Optionally, fields may include a default tag to auto-populate field values. For example, an event describing an a rate-limited call might appear as:

type UserOverLimit struct {
  UserID string `logevent:"user_id"`
  TenantID string `logevent:"tenant_id"`
  Message string `logevent:"message,default=user-over-limit"`
  AttemptsOverLimit int `logevent:"attempts_over_limit"`
}

Logging Events
logevent.FromContext(ctx).Info(myEvent{}) // Log the event
logevent.FromContext(ctx).SetField("key", "value")
logevent.FromContext(ctx).Warn("uh oh") // Fall back to string logging if not an event.
var newCtx = logevent.NewContext(context.Background(), logger.Copy())

Transaction IDs

You can add a transaction_id field to your logs by following the example below. Once a transaction id is set, all future logs written will automatically contain the transaction id. This is incredibly usefulfor tracing requests through a microservice and/or across multiple microservices. Note: if the transactionID parameter is left empty, a uuid will be randomly generated for you.

logger := logevent.New(logevent.Config{Level: "INFO"})
ctx := logevent.NewContext(context.Background(), logger)
logevent.SetTransactionID(ctx, &logger, "1234")

To retrieve a previously set transaction id, follow this example:

txid := logevent.GetTransactionID(ctx)

Adding Adapters

Contributing

License

This project is licensed under Apache 2.0. See LICENSE.txt for details.

Contributing Agreement

Atlassian requires signing a contributor's agreement before we can accept a patch. If you are an individual you can fill out the individual CLA. If you are contributing on behalf of your company then please fill out the corporate CLA.

Documentation

Index

Constants

View Source
const (
	// TransactionIDKey is the key name being set in the logger
	TransactionIDKey = "transaction_id"
)

Variables

This section is empty.

Functions

func GetTransactionID added in v1.6.0

func GetTransactionID(ctx context.Context) string

GetTransactionID retrieves the transaction id after `SetTransactionID` has been called. It will return an empty string if no transaction id has been set.

func NewContext

func NewContext(ctx context.Context, logger Logger) context.Context

NewContext installs a Logger.

func SetTransactionID added in v1.6.0

func SetTransactionID(ctx context.Context, logger *Logger, transactionID string) context.Context

SetTransactionID sets a transaction id string in the logger and context. If an empty string is passed in, then a randomly generated uuid will be used as the transaction id.

Types

type Config

type Config struct {
	// Level at which to log. Defaults to DEBUG.
	// Acceptable are ERROR, WARN, INFO, and DEBUG.
	Level string
	// HumanReadable toggles the JSON format off in favor of a colorised
	// log formatted for human readers.
	HumanReadable bool
	// Output defines to where logs are written. The default is os.Stdout.
	Output io.Writer
}

Config records the requested settings for a logger for use with New().

type Logger

type Logger interface {
	// Debug will emit the event with level DEBUG.
	Debug(event interface{})
	// Info will emit the event with level INFO.
	Info(event interface{})
	// Warn will emit the event with level WARN
	Warn(event interface{})
	// Error will emit the event with level ERROR.
	Error(event interface{})
	// SetField applies a contextual annotation to all
	// future events logged with this logger.
	SetField(name string, value interface{})
	// Copy the logger of use in some other context.
	Copy() Logger
}

Logger is a logging system abstraction that supports leveled, strictly structured log emissions.

func FromContext

func FromContext(ctx context.Context) Logger

FromContext fetches a Logger.

func New

func New(c Config) Logger

New creates an instance of a Logger using the default backend.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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