zaplog

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2020 License: MIT Imports: 6 Imported by: 0

README

zaplog : A Zap logging middleware for Chi

Overview

zaplog is a middleware for Chi that logs to Uber's Zap logging library.

Install

go get git.sr.ht/~yoink00/zaplog

Example

package main

import (
	"net/http"

	"git.sr.ht/~yoink00/zaplog"

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

func main() {
	logger, _ := zap.NewProduction()
	defer logger.Sync()

	r := chi.NewRouter()
	// A good base middleware stack
	r.Use(middleware.RequestID)
	r.Use(middleware.RealIP)
	r.Use(zaplog.ZapLog(logger))
	r.Use(middleware.Recoverer)

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

	logger.Fatal("server exit", zap.Error(http.ListenAndServe(":8080", r)))
}

Output:

{"level":"info","ts":1595694740.744598,"caller":"test/main.go:28","msg":"listening","listen":":8080"}
{"level":"info","ts":1595694751.9427874,"caller":"zaplog/log.go:27","msg":"header","User-Agent[0]":"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:79.0) Gecko/20100101 Firefox/79.0","Accept[0]":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8","Accept-Language[0]":"en-US,en;q=0.5","Accept-Encoding[0]":"gzip, deflate","Connection[0]":"keep-alive","Cookie[0]":"_ga=GA1.1.993607235.1593166962","Upgrade-Insecure-Requests[0]":"1"}
{"level":"info","ts":1595694751.9429872,"caller":"zaplog/log.go:29","msg":"http request","Method":"GET","Host":"localhost:8080","RequestURI":"/","Proto":"HTTP/1.1","RemoteAddr":"[::1]:35354","ContentLength":0,"UserAgent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:79.0) Gecko/20100101 Firefox/79.0"}
{"level":"info","ts":1595694751.9431272,"caller":"zaplog/log.go:57","msg":"http response","Status":200,"Bytes":11,"Duration":0.0000698}
{"level":"info","ts":1595694752.311804,"caller":"zaplog/log.go:27","msg":"header","Accept-Encoding[0]":"gzip, deflate","Connection[0]":"keep-alive","Cookie[0]":"_ga=GA1.1.993607235.1593166962","User-Agent[0]":"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:79.0) Gecko/20100101 Firefox/79.0","Accept[0]":"image/webp,*/*","Accept-Language[0]":"en-US,en;q=0.5"}
{"level":"info","ts":1595694752.311858,"caller":"zaplog/log.go:29","msg":"http request","Method":"GET","Host":"localhost:8080","RequestURI":"/favicon.ico","Proto":"HTTP/1.1","RemoteAddr":"[::1]:35354","ContentLength":0,"UserAgent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:79.0) Gecko/20100101 Firefox/79.0"}
{"level":"info","ts":1595694752.3118973,"caller":"zaplog/log.go:57","msg":"http response","Status":404,"Bytes":19,"Duration":0.0000163}

Author

Stuart Wallace

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ZapLog

func ZapLog(logger *zap.Logger) func(http.Handler) http.Handler

func ZapLogWithOptions

func ZapLogWithOptions(logger *zap.Logger, opt ...Option) func(http.Handler) http.Handler

Types

type Option

type Option int
const (
	DeterministicOutput Option = iota
)

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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