accesslog

package module
v0.3.5 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2021 License: MIT Imports: 18 Imported by: 0

README

accesslog

GoDoc

accesslog provides access logs that capture detailed information about requests sent to your services. Each log contains information such as the time the request was created, the client's IP address, latencies, request paths, and server responses. You can use these access logs to analyze traffic patterns and troubleshoot issues.

Installation

go get -u github.com/daangn/accesslog 

Getting started

Here's a basic usage of logging:

package main

import (
	"encoding/json"
	"net/http"

	"github.com/daangn/accesslog"
	"github.com/daangn/accesslog/middleware"
	"github.com/go-chi/chi/v5"
	"github.com/rs/zerolog"
)

func main() {
	r := chi.NewRouter()
	r.Use(middleware.AccessLog(accesslog.DefaultHTTPLogger))
	r.Get("/ping", func(w http.ResponseWriter, r *http.Request) {
		accesslog.GetLogEntry(r.Context()).Add(func(e *zerolog.Event) {
			e.Bytes("data", json.RawMessage(`{"foo": "bar"}`))
		})
		w.Write([]byte("pong"))
	})

	http.ListenAndServe(":3000", r)
}

go run above code in your terminal, and then execute curl localhost:3000/ping in another terminal. After, you can see some logs in your terminal like below.

{"protocol":"http","path":"/ping","status":"200","ua":"curl/7.64.1","time":"2021-12-09T02:39:46.026696Z","elapsed(ms)":0.033,"data":"{\"foo\": \"bar\"}"}

Check out the examples for more!

Log writer

In this library, the follwing log writers are available.

  • stdout
  • fluentd/fluent-bit

If you want one for yours, it's simple. Just implements the io.Writer.

Documentation

Overview

Package accesslog contains a library access logging.

Index

Constants

This section is empty.

Variables

DefaultGRPCLogger is default gRPC Logger.

DefaultHTTPLogger is default HTTP Logger.

View Source
var LogEntryCtxKey = struct{}{}

LogEntryCtxKey is the context key for LogEntry.

Functions

func SetLogEntry

func SetLogEntry(ctx context.Context, le LogEntry) context.Context

SetLogEntry sets LogEntry in context.

func WithClientIP added in v0.2.0

func WithClientIP() httpOption

WithClientIP specifies whether client ip should be captured by the logger.

func WithHeaders

func WithHeaders(hs ...string) httpOption

WithHeaders specifies headers to be captured by the logger. pseudo-headers also can be treated. If you want alias for logging, write like header:alias. e.g. "content-type:ct", this metadata will be logged like "ct": "application/json"

func WithIgnoredMethods

func WithIgnoredMethods(ms ...string) grpcOption

WithIgnoredMethods specifies full methods to be ignored by the server side interceptor. When an incoming request's full method is in ms, the request will not be captured.

func WithIgnoredPaths

func WithIgnoredPaths(ips map[string][]string) httpOption

WithIgnoredPaths specifies methods and paths to be ignored by the logger. See path.Match method how to set path patterns

func WithMetadata

func WithMetadata(ms ...string) grpcOption

WithMetadata specifies metadata to be captured by the logger. pseudo-headers in metadata also can be treated. If you want alias for logging, write like metadata:alias. e.g. "content-type:ct", this metadata will be logged like "ct": "[\"application/grpc\"]"

func WithPeer added in v0.2.0

func WithPeer() grpcOption

WithPeer specifies whether peer address should be captured by the logger.

func WithRequest

func WithRequest() grpcOption

WithRequest specifies whether gRPC requests should be captured by the logger.

func WithResponse

func WithResponse() grpcOption

WithResponse specifies whether gRPC responses should be captured by the logger.

Types

type DefaultGRPCLogEntry

type DefaultGRPCLogEntry struct {
	// contains filtered or unexported fields
}

DefaultGRPCLogEntry is the LogEntry formatted in DefaultGRPCLogFormatter.

func (*DefaultGRPCLogEntry) Add

func (le *DefaultGRPCLogEntry) Add(f func(e *zerolog.Event))

Add adds function for adding fields to log event.

func (*DefaultGRPCLogEntry) Write

func (le *DefaultGRPCLogEntry) Write(t time.Time)

Write writes a log.

type DefaultGRPCLogFormatter

type DefaultGRPCLogFormatter struct {
	// contains filtered or unexported fields
}

DefaultGRPCLogFormatter is default GRPCLogFormatter.

func NewDefaultGRPCLogFormatter

func NewDefaultGRPCLogFormatter(opts ...grpcOption) *DefaultGRPCLogFormatter

NewDefaultGRPCLogFormatter returns a new DefaultGRPCLogFormatter.

func (*DefaultGRPCLogFormatter) NewLogEntry

func (f *DefaultGRPCLogFormatter) NewLogEntry(l *zerolog.Logger, ctx context.Context, req interface{}, res *interface{}, info *grpc.UnaryServerInfo, err *error) LogEntry

NewLogEntry returns a New LogEntry formatted in DefaultGRPCLogFormatter.

type DefaultHTTPLogEntry

type DefaultHTTPLogEntry struct {
	// contains filtered or unexported fields
}

DefaultHTTPLogEntry is the LogEntry formatted in DefaultHTTPLogFormatter.

func (*DefaultHTTPLogEntry) Add

func (le *DefaultHTTPLogEntry) Add(f func(e *zerolog.Event))

Add adds function for adding fields to log event.

func (*DefaultHTTPLogEntry) Write

func (le *DefaultHTTPLogEntry) Write(t time.Time)

Write writes a log.

type DefaultHTTPLogFormatter

type DefaultHTTPLogFormatter struct {
	// contains filtered or unexported fields
}

DefaultHTTPLogFormatter is default HTTPLogFormatter.

func NewDefaultHTTPLogFormatter

func NewDefaultHTTPLogFormatter(opts ...httpOption) *DefaultHTTPLogFormatter

NewDefaultHTTPLogFormatter returns a new DefaultHTTPLogFormatter.

func (*DefaultHTTPLogFormatter) NewLogEntry

NewLogEntry returns a New LogEntry formatted in DefaultHTTPLogFormatter.

type GRPCLogFormatter

type GRPCLogFormatter interface {
	NewLogEntry(l *zerolog.Logger, ctx context.Context, req interface{}, res *interface{}, info *grpc.UnaryServerInfo, err *error) LogEntry
}

GRPCLogFormatter is the interface for NewLogEntry method.

type GRPCLogger

type GRPCLogger struct {
	// contains filtered or unexported fields
}

GRPCLogger is logger for gRPC access logging.

func NewGRPCLogger

func NewGRPCLogger(w io.Writer, f GRPCLogFormatter) *GRPCLogger

NewGRPCLogger returns a new GRPCLogger.

func (*GRPCLogger) NewLogEntry

func (l *GRPCLogger) NewLogEntry(ctx context.Context, req interface{}, res *interface{}, info *grpc.UnaryServerInfo, err *error) LogEntry

NewLogEntry returns a New LogEntry.

type HTTPLogFormatter

type HTTPLogFormatter interface {
	NewLogEntry(l *zerolog.Logger, r *http.Request, ww chi_middleware.WrapResponseWriter) LogEntry
}

HTTPLogFormatter is the interface for NewLogEntry method.

type HTTPLogger

type HTTPLogger struct {
	// contains filtered or unexported fields
}

HTTPLogger is logger for HTTP access logging.

func NewHTTPLogger

func NewHTTPLogger(w io.Writer, f HTTPLogFormatter) *HTTPLogger

NewHTTPLogger returns a new HTTPLogger.

func (*HTTPLogger) NewLogEntry

NewLogEntry returns a New LogEntry.

type LogEntry

type LogEntry interface {
	Write(t time.Time)
	Add(func(e *zerolog.Event))
}

LogEntry is the interface for each log entry.

func GetLogEntry

func GetLogEntry(ctx context.Context) LogEntry

GetLogEntry gets LogEntry in context.

Directories

Path Synopsis
examples
Package middleware contains some middlewares for access logging.
Package middleware contains some middlewares for access logging.
Package writer contains some writers implementation for access logging.
Package writer contains some writers implementation for access logging.

Jump to

Keyboard shortcuts

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