Skip to content
Custom format HTTP access logger in golang
Go
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
example Fix import path, fixes #2 Dec 4, 2017
.gitignore
LICENSE Follow Go and use 3-Clause BSD License Sep 26, 2019
README.markdown
accesslog.go WrapWriter is another way to retrieve the LoggingWriter Oct 9, 2019
accesslog_test.go WrapWriter is another way to retrieve the LoggingWriter Oct 9, 2019
go.mod add go.mod for introducing go modules Sep 26, 2019

README.markdown

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))
}
You can’t perform that action at this time.