accesslogger

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2023 License: MIT Imports: 5 Imported by: 2

README

accesslogger

Documentation Latest GitHub tag Github Actions test Go Report Card License

accessloger for golang http handler

Requirements

  • Go 1.18 or higher. support the 3 latest versions of Go.

Usage

sample go code

package main

import (
	"encoding/json"
	"log"
	"net/http"
	"os"

	"github.com/mashiike/accesslogger"
)

func main() {
	err := http.ListenAndServe("localhost:8080", 
		accesslogger.Wrap(
			http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			w.WriteHeader(http.StatusOK)
				e := json.NewEncoder(w)
				e.SetEscapeHTML(true)
				e.SetIndent("", "  ")
				e.Encode(map[string]string{"status":"ok"})
			}),
			accesslogger.CombinedLogger(os.Stderr),
			//accesslogger.CombinedDLogger(os.Stderr),
			//accesslogger.JSONLogger(os.Stderr),
        ),
	)
	if err != nil {
		log.Fatalln(err)
	}
}

output log:

192.0.2.1:1234 - - [26/Dec/2022:15:04:05 +0900] "GET / HTTP/1.1" - 4 "-" "go test client"
192.0.2.1:1234 - hoge [26/Dec/2022:15:04:06 +0900] "GET /hoge HTTP/1.1" - 4 "https://example.com" "go test client"
222.222.333.333 - hoge [26/Dec/2022:15:04:06 +0900] "GET /hoge HTTP/1.1" - 4 "https://example.com" "go test client"

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Clock func() time.Time = func() time.Time { return time.Now().Local() }

Functions

func CombinedDLogFormat

func CombinedDLogFormat(l *AccessLog) string

func CombinedLogFormat

func CombinedLogFormat(l *AccessLog) string

func JSONLogFormat

func JSONLogFormat(l *AccessLog) string

func New

func New(loggers ...Logger) func(http.Handler) http.Handler

func Wrap

func Wrap(next http.Handler, loggers ...Logger) http.Handler

Types

type AccessLog

type AccessLog struct {
	RequestHeader  http.Header `json:"-"`
	ResponseHeader http.Header `json:"-"`
	RemoteAddr     string      `json:"remote_addr,omitempty"`
	AccessedAt     time.Time   `json:"accessed_at,omitempty"`
	UserAgent      string      `json:"user_agent,omitempty"`
	Referer        string      `json:"referer,omitempty"`
	BasicAuthUser  string      `json:"basic_auth_user,omitempty"`
	Request        string      `json:"request,omitempty"`
	StatusCode     int         `json:"status_code,omitempty"`
	BodyByteSent   int         `json:"body_byte_sent,omitempty"`
	FirstSentAt    time.Time   `json:"first_sent_at,omitempty"`
	LastSentAt     time.Time   `json:"last_sent_at,omitempty"`
	FirstSentTime  int64       `json:"first_sent_time,omitempty"`
	ResponseTime   int64       `json:"response_time,omitempty"`
}

func NewAccessLog

func NewAccessLog(r *http.Request) *AccessLog

func (*AccessLog) WriteResponseInfo

func (l *AccessLog) WriteResponseInfo(w *ResponseWriter) *AccessLog

type FormatLogger

type FormatLogger struct {
	io.Writer
	LogFormat
}

func CombinedDLogger

func CombinedDLogger(w io.Writer) FormatLogger

func CombinedLogger

func CombinedLogger(w io.Writer) FormatLogger

func JSONLogger

func JSONLogger(w io.Writer) FormatLogger

func (FormatLogger) WriteAccessLog

func (logger FormatLogger) WriteAccessLog(l *AccessLog)

type LogFormat

type LogFormat func(*AccessLog) string

type Logger

type Logger interface {
	WriteAccessLog(*AccessLog)
}

type ResponseWriter

type ResponseWriter struct {
	http.ResponseWriter
	FirstWriteTime time.Time
	LastWriteTime  time.Time
	StatusCode     int
	BodyByteSent   int
}

func (*ResponseWriter) Write

func (w *ResponseWriter) Write(data []byte) (int, error)

func (*ResponseWriter) WriteHeader

func (w *ResponseWriter) WriteHeader(statusCode int)

Jump to

Keyboard shortcuts

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