https://github.com/zendesk/go-httpclerk
A simple HTTP request/response logger for Go supporting multiple formatters.
https://github.com/zendesk/go-httpclerk
Last synced: about 1 year ago
JSON representation
A simple HTTP request/response logger for Go supporting multiple formatters.
- Host: GitHub
- URL: https://github.com/zendesk/go-httpclerk
- Owner: zendesk
- License: apache-2.0
- Created: 2014-07-24T09:25:30.000Z (almost 12 years ago)
- Default Branch: master
- Last Pushed: 2014-08-07T07:45:45.000Z (almost 12 years ago)
- Last Synced: 2025-03-25T16:51:41.081Z (over 1 year ago)
- Language: Go
- Homepage: http://godoc.org/github.com/zendesk/go-httpclerk
- Size: 275 KB
- Stars: 50
- Watchers: 426
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
go-httpclerk
==============
## Overview
A simple HTTP request/response logger for Go supporting multiple formatters.
## Rationale
We needed a way to log HTTP requests at Zendesk to different log backends (stdout, syslog etc.) with multiple ways to format them (including logstash). So we created this project to help us.
## Usage
You'll need to create some sort of logger that conforms to the `LogDestination` interface in this package. The [go-logger](https://github.com/op/go-logging) package is recommended.
### Simple example:
```
package main
import (
"fmt"
"github.com/op/go-logging"
"github.com/zendesk/go-httpclerk"
stdlog "log"
"net/http"
"os"
)
var log = logging.MustGetLogger("myApp")
func main() {
// Setup a go-logging logger
stdoutBackend := logging.NewLogBackend(os.Stderr, "", stdlog.LstdFlags|stdlog.Lshortfile)
logging.SetBackend(stdoutBackend) // See go-logging docs for multiple backends
logging.SetLevel(logging.DEBUG, "myApp")
// Boot web server and listen on 8080
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}
func handler(w http.ResponseWriter, r *http.Request) {
formatter, _ := httpclerk.NewTextFormatter("myHandler")
clerk, err := httpclerk.NewHTTPLogger("myHandler", log, formatter)
if err != nil {
log.Fatal("HTTP logger could not be created", err)
}
defer clerk.Info(w, r)
fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
}
```
This will produce logs like so:
```
2014/07/27 07:43:56 http_logger.go:39: myHandler 1974-carcher.local > Method: GET Path: /ciaran Status: Host: localhost:8080 Headers: map[User-Agent:[curl/7.30.0] Accept:[*/*]]
```
### Status Code
You'll notice that the `Status` is blank. This is becuase there is no simple way to get the response status in a HTTP handler without wrapping the `ResponseWriter` type. You can see an example of this [here](https://gist.github.com/ciaranarcher/abccf50cb37645ca27fa). If you do this, and use this type instead of the standard `ResponseWriter` then the `go-httpclerk` package can fetch the status code and include it in logging:
```
2014/07/27 07:43:56 http_logger.go:39: myHandler 1974-carcher.local > Method: GET Path: /ciaran Status: 200 Host: localhost:8080 Headers: map[User-Agent:[curl/7.30.0] Accept:[*/*]]
```
### Other Formatters
Included in the package is a `TextFormatter` (examples above use this) and a `LogStashFormatter` for JSON logging
```
formatter, _ := NewLogStashFormatter("fooApp", []string{"blimp", "foo"})
```
Other loggers can be used in place if they implement the the following interface:
```
type Formatter interface {
Format(interface{}) (string, error)
}
```
## Contributing
Create a Pull Request with your changes, ping someone and we'll look at getting it merged.
## Copyright and license
Copyright 2013 Zendesk
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.