zapray

package module
v0.0.27 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2022 License: MIT Imports: 5 Imported by: 0

README

ZapRay - XRay logger that wraps uber/zap

GoDoc

ZapRay is a logger for Go. To be used in conjunction with AWS X-Ray and Zap.

Install

go get -u github.com/joerdav/zapray

Example

package main

import (
	"github.com/joerdav/zapray"
	"github.com/aws/aws-xray-sdk-go/xray"
	"go.uber.org/zap"
	)

func main() {
	// Create a zapray logger
	logger, err := zapray.NewProduction()
	if err != nil {
		panic(err)
	}

	// zapray requires the context to have been created by an xray segment
	// if this isn't the case then zapray will log without appending trace information
	ctx, seg := xray.BeginSegment(context.Background(), "someSegment")
	defer seg.Close(nil)

	// Trace returns a copy of the logger but will log trace id with any logs chained onto it
	logger.Trace(ctx).Info("my zap log")
}

Web Example

package main

import (
	"net/http"
	"github.com/joerdav/zapray"
	"github.com/aws/aws-xray-sdk-go/xray"
	"go.uber.org/zap"
	)
	
var logger *zapray.ZaprayLogger
	
func HandleRequest(w http.ResponseWriter, r *http.Request) {
	logger.TraceRequest(r).Info("some log")
}

func main() {
	// Create a zapray logger
	logger = zapray.NewProduction()
	handler := http.HandlerFunc(HandleRequest)
	segmentedHandler := xray.Handler(handler)
	// Uncomment to make a subsegment rather than a segment
	// segmentedHandler := zapray.NewMiddleware(handler)
	panic(http.ListenAndServe(":8000", segmentedHandler)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AppVersion = ""

AppVersion is exposed to allow build time versions to be set. go build -ldflags="-X 'github.com/joerdav/zapray.AppVersion.AppVersion=v1.0.0'"

Functions

func NewMiddleware

func NewMiddleware(appName string, next http.Handler) http.Handler

NewMiddlware creates X-Ray middleware that creates a subsegment for each HTTP request.

Types

type Logger

type Logger struct {
	*zap.Logger
	SuppressMissingSegmentWarning bool
}

Logger is a wrapper for zap.Logger, exposes the zap.Logger functions and adds the ability to Trace logs.

func New

func New(core zapcore.Core, options ...zap.Option) *Logger

New creates a new instance of zap.Logger and wraps it in a zapray.Logger

func NewDevelopment

func NewDevelopment() (*Logger, error)

NewDevelopment creates a new instance of *Logger and includes a zap.NewDevelopment().

log := zapray.NewDevelopment()

func NewLogger

func NewLogger(zapLogger *zap.Logger) *Logger

NewLogger creates a new instance of *Logger and wraps the logger provided.

z, _ := zap.NewProduction()
log := zapray.NewLogger(z)

func NewNop

func NewNop() *Logger

NewNop creates a new instance of *Logger and includes a zap.NewNop().

log := zapray.NewNop()

func NewProduction

func NewProduction() (*Logger, error)

NewProduction creates a new instance of *Logger and includes a zap.NewProduction().

log := zapray.NewProduction()

func (*Logger) Named

func (log *Logger) Named(s string) *Logger

Named delegates to zap.Logger.Named and wraps the resulting logger into a zapray.Logger

func (*Logger) Trace

func (zprl *Logger) Trace(ctx context.Context) (res *Logger)

Trace creates a new zap.Logger but with the xrayTraceId and xraySegmentId baked in.

log.Trace(ctx).Info("myLog")

Once trace is called you can use it as a zap.Logger.

tracedLogger := log.Trace(ctx)
tracedLogger.Info("Log one")
tracedLogger.Info("Log two")

This means as above you can trace once and use the provided logger.

func (*Logger) TraceRequest

func (zprl *Logger) TraceRequest(r *http.Request) *Logger

TraceRequest creates a new zap.Logger but with the xrayTraceId and xraySegmentId baked in.

log.TraceRequest(r).Info("myLog")

Once trace is called you can use it as a zap.Logger.

tracedLogger := log.Trace(r)
tracedLogger.Info("Log one")
tracedLogger.Info("Log two")

This means as above you can trace once and use the provided logger.

func (*Logger) With

func (log *Logger) With(fields ...zap.Field) *Logger

With delegates to zap.Logger.With and wraps the resulting logger into a zapray.Logger

func (*Logger) WithOptions

func (log *Logger) WithOptions(opts ...zap.Option) *Logger

WithOptions delegates to zap.Logger.WithOptions and wraps the resulting logger into a zapray.Logger

type Middleware

type Middleware struct {
	Next http.Handler
	// contains filtered or unexported fields
}

Middlware applies X-Ray segements to the wrapped handler.

func (Middleware) ServeHTTP

func (h Middleware) ServeHTTP(w http.ResponseWriter, r *http.Request)

Jump to

Keyboard shortcuts

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