Skip to content

daangn/accesslog

Repository files navigation

accesslog

GoDoc

accesslog provides detailed informed access logs of requests sent to your services. Each log contains information such as the timestamp of the request created, the client's IP address, latencies, request paths, and server responses. You can use these access logs to analyze traffic patterns and troubleshoot issues.

Installation

go get -u github.com/daangn/accesslog 

Getting started

Here's a basic usage of logging:

package main

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

	"github.com/daangn/accesslog"
	"github.com/daangn/accesslog/middleware"
	"github.com/go-chi/chi/v5"
	"github.com/rs/zerolog"
)

func main() {
	r := chi.NewRouter()
	r.Use(middleware.AccessLog(accesslog.DefaultHTTPLogger))
	r.Get("/ping", func(w http.ResponseWriter, r *http.Request) {
		accesslog.GetLogEntry(r.Context()).Add(func(e *zerolog.Event) {
			e.Bytes("data", json.RawMessage(`{"foo": "bar"}`))
		})
		w.Write([]byte("pong"))
	})

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

go run above code in your terminal, and then execute curl localhost:3000/ping in another terminal. Afterward, you can see some logs in your terminal like below.

{"protocol":"http","path":"/ping","status":"200","ua":"curl/7.64.1","time":"2021-12-09T02:39:46.026696Z","elapsed(ms)":0.033,"data":"{\"foo\": \"bar\"}"}

Check out the examples for more!

Log writers

In this library, the follwing log writers are available.

  • stdout
  • fluentd/fluent-bit

If you want one for yours, it's simple. Just implement the io.Writer.