lambdalogger

package
v1.9.0 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2020 License: AGPL-3.0 Imports: 8 Imported by: 0

README

lambdalogger

Initialize a zap logger for your Lambda function with the request ID and other initial fields.

This makes it possible to easily search for logs from a given invocation and is required for all Panther Go Lambda functions.

Example Usage

package main

import (
    "context"

    "github.com/aws/aws-lambda-go/lambda"
    "go.uber.org/zap"

    "github.com/panther-labs/panther/pkg/lambdalogger"
)

func lambdaHandler(ctx context.Context, event interface{}) {
    // Configure the global zap logger.
    // DEBUG mode is used if strings.lower(os.Getenv("DEBUG")) == "true".
    lambdaContext, logger := lambdalogger.ConfigureGlobal(ctx, nil)

    // The returned logger is the same as the global zap logger: zap.L()
    // The global zap logger can now be called from anywhere in the source code.
    logger.Info("function invoked", zap.Any("event", event))
    zap.L().Info("another way to log", zap.String("arn", lambdaContext.InvokedFunctionArn))
}

func main() {
    lambda.Start(lambdaHandler)
}

Documentation

Overview

Package lambdalogger updates the global zap logger for use in a Lambda function.

Index

Constants

View Source
const (
	FieldRequestID   = "requestId"
	FieldApplication = "application"
	FieldNamespace   = "namespace"
	FieldComponent   = "component"
)

Well-known fields

View Source
const Application = "panther" // tag all logs with "application" -> "panther" (used for audit)

Variables

View Source
var DebugEnabled = strings.ToLower(os.Getenv("DEBUG")) == "true"

DebugEnabled is true if the DEBUG environment variable is set to true.

Functions

func ConfigureGlobal

func ConfigureGlobal(
	ctx context.Context,
	initialFields map[string]interface{},
) (*lambdacontext.LambdaContext, *zap.Logger)

ConfigureGlobal adds the Lambda request ID to the global zap logger.

To add fields to every log message, include them in initialFields (the requestID is added for you).

Returns parsed Lambda context, global zap logger.

func Context added in v1.8.0

func Context(ctx context.Context, logger *zap.Logger) context.Context

func FromContext added in v1.8.0

func FromContext(ctx context.Context) *zap.Logger

func IsDebug added in v1.8.0

func IsDebug(logger *zap.Logger) bool

func Wrap added in v1.8.0

func Wrap(logger *zap.Logger, handler lambda.Handler) lambda.Handler

Types

type Config added in v1.8.0

type Config struct {
	Debug     bool
	Namespace string
	Component string
	Options   []zap.Option
}

func (Config) Build added in v1.8.0

func (c Config) Build() (*zap.Logger, error)

func (*Config) InitialFields added in v1.8.0

func (c *Config) InitialFields() map[string]interface{}

func (Config) MustBuild added in v1.8.0

func (c Config) MustBuild() (logger *zap.Logger)

Jump to

Keyboard shortcuts

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