Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/daangn/accesslog
access logger for golang
https://github.com/daangn/accesslog
access-logs fluent-bit golang grpc http logger logging requestlogger
Last synced: about 3 hours ago
JSON representation
access logger for golang
- Host: GitHub
- URL: https://github.com/daangn/accesslog
- Owner: daangn
- License: mit
- Created: 2021-12-03T02:30:56.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2022-06-04T09:09:32.000Z (over 2 years ago)
- Last Synced: 2024-08-04T04:01:33.652Z (3 months ago)
- Topics: access-logs, fluent-bit, golang, grpc, http, logger, logging, requestlogger
- Language: Go
- Homepage:
- Size: 310 KB
- Stars: 20
- Watchers: 21
- Forks: 0
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# accesslog
[![GoDoc](https://godoc.org/github.com/daangn/accesslog?status.svg)](https://godoc.org/github.com/daangn/accesslog)
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
```shell
go get -u github.com/daangn/accesslog
```## Getting started
Here's a basic usage of logging:```go
package mainimport (
"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](examples) for more!
## Log writers
In this library, the follwing log writers are available.- stdout
- fluentd/fluent-bitIf you want one for yours, it's simple. Just implement the io.Writer.