zaphttplog

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2023 License: MIT Imports: 10 Imported by: 0

README

zaphttplog

GoDoc CI Workflow

zaphttplog provides a structured request/response logging implementation based on zap. It provides much the same functionality and API as Chi's httplog, but backed by zap instead of zerolog.

This is useful for cases where you've standardized on zap for logging and want detailed, structured request logging as part of your HTTP middleware

Usage

package main

import (
	"log"
	"net/http"

	"github.com/Silicon-Ally/zaphttplog"
	"github.com/go-chi/chi/v5"
	"github.com/go-chi/chi/v5/middleware"
	"go.uber.org/zap"
)

func main() {
	// Configure your logger for your environment.
	logger, err := zap.NewProduction()
	if err != nil {
		log.Fatalf("failed to init logger: %v", err)
	}

	// Initialize the router and set basic middleware.
	r := chi.NewRouter()
	r.Use(zaphttplog.NewMiddleware(logger))
	r.Use(middleware.Recoverer)

	// An example handler.
	r.Get("/", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("root."))
	})

	if err := http.ListenAndServe(":8080", r); err != nil {
		log.Fatalf("http.ListenAndServe: %v", err)
	}
}

Running this would look like:

# Run the server
go run ./examples

# In a new terminal, make a request
curl localhost:8080

In the first terminal, you should observe a log line like:

{"level":"info","ts":1689792663.2690268,"caller":"zaphttplog/zaphttplog.go:201","msg":"GET / - 200 OK","httpRequest":{"requestURL":"http://localhost:8080/","requestMethod":"GET","requestPath":"/","remoteIP":"127.0.0.1:57954","proto":"HTTP/1.1","scheme":"http","header":{"user-agent":"curl/8.1.2","accept":"*/*"}},"httpResponse":{"status":200,"bytes":5,"elapsed":0.000014111}}

Contributing

Contribution guidelines can be found on our website.

Documentation

Overview

Package zaplog provides a structured request/response logging implementation based on zap (go.uber.org/zap). It provides much the same functionality and API as github.com/go-chi/httplog, but backed by zap instead of zerolog (github.com/rs/zerolog).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewMiddleware

func NewMiddleware(logger *zap.Logger, options ...Option) func(next http.Handler) http.Handler

Types

type Option

type Option func(*Options)

func WithConcise

func WithConcise(v bool) Option

func WithSkipHeaders

func WithSkipHeaders(headersToSkip []string) Option

type Options

type Options struct {
	// Concise mode includes fewer log details during the request flow. For example
	// excluding details like request content length, user-agent and other details.
	// This is useful if during development your console is too noisy.
	Concise bool

	// SkipHeaders are additional headers which are redacted from the logs
	SkipHeaders []string
}

func (*Options) Clone

func (o *Options) Clone() *Options

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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