logger

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2020 License: MIT Imports: 13 Imported by: 0

README

httplog

Powerful structured logging package for HTTP request logging in Go. It's fork of github.com/go-chi/httplog with HTTP body dumps improvement.

Example

(see example/)

package main

import (
	"net/http"

	"github.com/go-chi/chi"
	"github.com/go-chi/chi/middleware"
	httplog "github.com/stn1slv/chi-httplog"
)

func main() {
	// Logger
	logger := httplog.NewLogger("httplog-example", httplog.Options{
		// JSON: true,
		// Concise: true,
		Body: true,
		// Tags: map[string]string{
		// 	"version": "v1.0-81aa4244d9fc8076a",
		// 	"env":     "dev",
		// },
	})

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

	r.Get("/", func(w http.ResponseWriter, r *http.Request) {
		w.Header().Add("Content-Type", "text/plain")
		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 := httplog.LogEntry(r.Context())
		w.Header().Add("Content-Type", "text/plain")
		oplog.Info().Msg("info here")
		w.Write([]byte("info here"))
	})

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

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

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


License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultOptions = Options{
	LogLevel:    "info",
	JSON:        false,
	Body:        false,
	Concise:     false,
	Tags:        nil,
	SkipHeaders: nil,
}

Functions

func Configure

func Configure(opts Options)

Configure will set new global/default options for the httplog and behaviour of underlying zerolog pkg and its global logger.

func Handler

func Handler(logger zerolog.Logger) func(next http.Handler) http.Handler

func LogEntry

func LogEntry(ctx context.Context) zerolog.Logger

func LogEntrySetField

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

func LogEntrySetFields

func LogEntrySetFields(ctx context.Context, fields map[string]interface{})

func NewLogger

func NewLogger(serviceName string, opts ...Options) zerolog.Logger

func RequestLogger

func RequestLogger(logger zerolog.Logger) func(next http.Handler) http.Handler

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

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

Types

type Options

type Options struct {
	// LogLevel defines the minimum level of severity that app should log.
	//
	// Must be one of: ["trace", "debug", "info", "warn", "error", "critical"]
	LogLevel string

	// JSON enables structured logging output in json. Make sure to enable this
	// in production mode so log aggregators can receive data in parsable format.
	//
	// In local development mode, its appropriate to set this value to false to
	// receive pretty output and stacktraces to stdout.
	JSON bool

	//Log request and response bodies or not
	Body bool

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

	// Tags are additional fields included at the root level of all logs.
	// These can be useful for example the commit hash of a build, or an environment
	// name like prod/stg/dev
	Tags map[string]string

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

type RequestLoggerEntry

type RequestLoggerEntry struct {
	Logger zerolog.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