accesslog

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2021 License: BSD-3-Clause Imports: 7 Imported by: 21

README

Custom format HTTP access logger in golang

Description

A library to build your own HTTP access logger.

Usage

Provide a class that implements accesslog.Logger interface to make a logging HTTP handler.

type LogRecord struct {
	Time                                time.Time
	Ip, Method, Uri, Protocol, Username string
	Status                              int
	Size                                int64
	ElapsedTime                         time.Duration
	CustomRecords                       map[string]string
}

type Logger interface {
	Log(record LogRecord)
}

Example

import (
	"log"
	"net/http"

	accesslog "github.com/mash/go-accesslog"
)

type logger struct {
}

func (l logger) Log(record accesslog.LogRecord) {
	log.Println(record.Method + " " + record.Uri)
}

func main() {
	l := logger{}
	handler := http.FileServer(http.Dir("."))
	http.ListenAndServe(":8080", accesslog.NewLoggingHandler(handler, l))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewAroundLoggingHandler

func NewAroundLoggingHandler(handler http.Handler, logger Logger) http.Handler

func NewAroundLoggingMiddleware

func NewAroundLoggingMiddleware(logger Logger) func(http.Handler) http.Handler

func NewLoggingHandler

func NewLoggingHandler(handler http.Handler, logger Logger) http.Handler

func NewLoggingMiddleware

func NewLoggingMiddleware(logger Logger) func(http.Handler) http.Handler

Types

type ContextLogger added in v1.2.0

type ContextLogger interface {
	Logger
	LogContext(context.Context, LogRecord)
}

type LogRecord

type LogRecord struct {
	Time                                      time.Time
	Ip, Method, Uri, Protocol, Username, Host string
	Status                                    int
	Size                                      int64
	ElapsedTime                               time.Duration
	RequestHeader                             http.Header
	CustomRecords                             map[string]string
}

type Logger

type Logger interface {
	Log(record LogRecord)
}

type LoggingHandler

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

func (*LoggingHandler) ServeHTTP

func (h *LoggingHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request)

type LoggingWriter

type LoggingWriter struct {
	http.ResponseWriter
	// contains filtered or unexported fields
}

func GetLoggingWriter added in v1.1.0

func GetLoggingWriter(ctx context.Context) *LoggingWriter

func (*LoggingWriter) CloseNotify

func (r *LoggingWriter) CloseNotify() <-chan bool

http.CloseNotifier interface

func (*LoggingWriter) Flush

func (r *LoggingWriter) Flush()

http.Flusher

func (*LoggingWriter) GetCustomLogRecord added in v1.3.0

func (r *LoggingWriter) GetCustomLogRecord(key string) string

w.(accesslogger.LoggingWriter).GetCustomLogRecord("X-User-Id")

func (*LoggingWriter) Hijack

func (r *LoggingWriter) Hijack() (net.Conn, *bufio.ReadWriter, error)

func (*LoggingWriter) Push

func (r *LoggingWriter) Push(target string, opts *http.PushOptions) error

http.Pusher

func (*LoggingWriter) SetCustomLogRecord

func (r *LoggingWriter) SetCustomLogRecord(key, value string)

SetCustomLogRecord and GetCustomLogRecord functions provide accessors to the logRecord.CustomRecords. You can use it to store arbitrary strings that are relevant to this request.

Alternative method would be to store the value in context. Which doesn't work when you want to retrieve the value from a HTTP middleware that is earlier in the middleware chain, eg: accesslog, recovery.

w.(accesslogger.LoggingWriter).SetCustomLogRecord("X-User-Id", "3")

func (*LoggingWriter) WrappedWriter added in v1.1.0

func (r *LoggingWriter) WrappedWriter() http.ResponseWriter

WrapWriter interface

func (*LoggingWriter) Write

func (r *LoggingWriter) Write(p []byte) (int, error)

func (*LoggingWriter) WriteHeader

func (r *LoggingWriter) WriteHeader(status int)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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