https://github.com/dtgorski/midas
[MODULE] - Fast and cheap HTTP access logger middleware for Go crafted with special care in order to ensure marginal heap memory footprint and low garbage collector pressure.
https://github.com/dtgorski/midas
access decorator go golang handler http log logger middleware request
Last synced: 22 days ago
JSON representation
[MODULE] - Fast and cheap HTTP access logger middleware for Go crafted with special care in order to ensure marginal heap memory footprint and low garbage collector pressure.
- Host: GitHub
- URL: https://github.com/dtgorski/midas
- Owner: dtgorski
- License: mit
- Created: 2019-09-24T07:30:52.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-09-24T07:31:21.000Z (over 6 years ago)
- Last Synced: 2024-06-19T05:45:22.497Z (over 1 year ago)
- Topics: access, decorator, go, golang, handler, http, log, logger, middleware, request
- Language: Go
- Homepage:
- Size: 5.86 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://travis-ci.org/dtgorski/midas)
[](https://coveralls.io/github/dtgorski/midas?branch=master)
## midas
Fast and cheap HTTP access logger middleware for Go.
### Installation
```
go get -u github.com/dtgorski/midas
```
### midas.Logger
... is a fast HTTP middleware for machine-readable response logging (_access.log_).
Special care has been taken in order to ensure marginal heap memory footprint and low garbage collector pressure.
The customized output layout has been chosen due to a specific requirement. If you need a different layout, feel free to fork.
#### Usage with a common router software:
```
import "github.com/dtgorski/midas"
.
.
router.Use(
midas.Logger(os.Stdout),
.
.
)
```
#### Usage with handcrafted routing:
```
package main
import (
"io"
"log"
"net/http"
"os"
"github.com/dtgorski/midas"
)
func main() {
handler := http.HandlerFunc(
func(w http.ResponseWriter, req *http.Request) {
io.WriteString(w, "Hello, world!\n")
},
)
logger := midas.Logger(os.Stdout)
http.Handle("/", logger(handler))
log.Fatal(http.ListenAndServe(":8080", nil))
}
```
#### Log output could look like:
```
0000-00-00T00:00:00+00:00 | 127.0.0.1 | - | GET /hello HTTP/1.1 | 200 | - | - | Mozilla/5.0 (X11; Linux… | - | - | 14 | 0.000 | -
0000-00-00T00:00:00+00:00 | 127.0.0.1 | - | GET / HTTP/1.1 | 200 | - | - | Mozilla/5.0 (X11; Linux… | - | - | 14 | 0.000 | -
0000-00-00T00:00:00+00:00 | 127.0.0.1 | - | GET / HTTP/1.1 | 200 | - | - | Mozilla/5.0 (X11; Linux… | - | - | 14 | 0.000 | -
```
A log line consists of fields separated by a pipe and is suffixed by a newline (```\n```). Non-printable whitespace characters and UTF-8 runes will be replaced by a period sign (```.```). The fields in their order:
* access time
* remote address without port
* remote user, if any
* request method, path, protocol
* response status code
* referer, if any
* forwarded for, if any
* user agent, if any
* SSL protocol, if any
* SSL cipher, if any
* bytes sent in response
* request time in seconds, three fractional digits
* request id, if any
Although targeted for machine-reading (like log aggregators), the field values are padded with spaces for better human perception.
#### Artificial benchmark:
```
$ make bench
CGO_ENABLED=0 go test -run=^$ -bench=. -benchmem
goos: linux
goarch: amd64
pkg: github.com/dtgorski/midas
BenchmarkLoggerConcatFullLine-8 2000000 850 ns/op 48 B/op 2 allocs/op
```
## License
[MIT](https://opensource.org/licenses/MIT) - © dtg [at] lengo [dot] org