httpzaplog

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2023 License: MIT Imports: 14 Imported by: 1

README

httpzaplog

A fork of go-chi/httplog that uses uber-go/zap for logging instead of zerolog.

Example

(see _example/)

package main

import (
	"net/http"

	httpzaplog "github.com/illumitacit/httpzaplog"
	"github.com/go-chi/chi/v5"
	"github.com/go-chi/chi/v5/middleware"
	"go.uber.org/zap"
)

func main() {
	// Logger
	opts := &httpzaplog.Options{
		Logger:  zap.Must(zap.NewProduction()),
		Concise: true,
	}

	// Service
	r := chi.NewRouter()
	r.Use(httpzaplog.RequestLogger(opts))
	r.Use(middleware.Heartbeat("/ping"))

	r.Get("/", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("hello world"))
	})

	r.Get("/panic", func(w http.ResponseWriter, r *http.Request) {
		panic("oh no")
	})

	r.Get("/info", func(w http.ResponseWriter, r *http.Request) {
		oplog := httpzaplog.LogEntry(r.Context())
		w.Header().Add("Content-Type", "text/plain")
		oplog.Info("info here")
		w.Write([]byte("info here"))
	})

	r.Get("/warn", func(w http.ResponseWriter, r *http.Request) {
		oplog := httpzaplog.LogEntry(r.Context())
		oplog.Warn("warn here")
		w.WriteHeader(400)
		w.Write([]byte("warn here"))
	})

	r.Get("/err", func(w http.ResponseWriter, r *http.Request) {
		oplog := httpzaplog.LogEntry(r.Context())
		oplog.Error("err here")
		w.WriteHeader(500)
		w.Write([]byte("err here"))
	})

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

Credits

This package is a modified version of go-chi/httplog.

License

MIT

NOTE: This is a derivative package of go-chi/httplog. Refer to the NOTICE file for the license of the original source material.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultOptions = Options{
	Logger:  zap.Must(zap.NewProduction()),
	Concise: false,
}

Functions

func Handler

func Handler(opts *Options) func(next http.Handler) http.Handler

func LogEntry

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

func LogEntrySetField

func LogEntrySetField(ctx context.Context, key, value string)

func LogEntrySetFields

func LogEntrySetFields(ctx context.Context, fields ...zapcore.Field)

func RequestLogger

func RequestLogger(opts *Options) func(next http.Handler) http.Handler

RequestLogger is an http middleware to log http requests and responses.

NOTE: for simplicity, RequestLogger automatically makes use of the chi RequestID and Recoverer middleware.

Types

type Options

type Options struct {
	Logger *zap.Logger

	// Concise determines Whether to log the entries in concise mode.
	Concise bool

	// SkipURLParams determines which get parameters shouldn't be logged.
	SkipURLParams []string

	// SkipHeaders determines which headers shouldn't be logged.
	SkipHeaders []string

	// SkipPaths determines which paths shouldn't be logged.
	SkipPaths []string

	// ErrorMiddleware is a middleware that will be injected between the logger middleware and the Recoverer middleware.
	// This allows you to customize the 500 error page in the case of a panic.
	ErrorMiddleware func(http.Handler) http.Handler
}

type RequestLoggerEntry

type RequestLoggerEntry struct {
	Logger *zap.Logger
	// contains filtered or unexported fields
}

func (*RequestLoggerEntry) Panic

func (l *RequestLoggerEntry) Panic(v interface{}, stack []byte)

func (*RequestLoggerEntry) Write

func (l *RequestLoggerEntry) Write(status, bytes int, header http.Header, elapsed time.Duration, extra interface{})

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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