httpclerk

package module
v0.0.0-...-2406efe Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2014 License: Apache-2.0 Imports: 7 Imported by: 0

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 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. 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 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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Formatter

type Formatter interface {
	Format(interface{}) (string, error)
}

type HTTPLogger

type HTTPLogger struct {
	// contains filtered or unexported fields
}

func NewHTTPLogger

func NewHTTPLogger(name string, destination LogDestination, formatter Formatter) (*HTTPLogger, error)

NewHTTPLogger constructor

func (*HTTPLogger) Critical

func (log *HTTPLogger) Critical(res http.ResponseWriter, req *http.Request)

func (*HTTPLogger) Debug

func (log *HTTPLogger) Debug(res http.ResponseWriter, req *http.Request)

func (*HTTPLogger) Error

func (log *HTTPLogger) Error(res http.ResponseWriter, req *http.Request)

func (*HTTPLogger) Info

func (log *HTTPLogger) Info(res http.ResponseWriter, req *http.Request)

func (*HTTPLogger) Warning

func (log *HTTPLogger) Warning(res http.ResponseWriter, req *http.Request)

type LogDestination

type LogDestination interface {
	Debug(data string, args ...interface{})
	Info(data string, args ...interface{})
	Warning(data string, args ...interface{})
	Error(data string, args ...interface{})
	Critical(data string, args ...interface{})
}

Implements similar to http://godoc.org/github.com/op/go-logging#Logger

type LogStashFormatter

type LogStashFormatter struct {
	Source string
	Tags   []string
}

func NewLogStashFormatter

func NewLogStashFormatter(source string, tags []string) (*LogStashFormatter, error)

func (*LogStashFormatter) Format

func (formatter *LogStashFormatter) Format(customFields interface{}) (string, error)

type LogStashJSON

type LogStashJSON struct {
	Source    string      `json:"@source"`
	Fields    interface{} `json:"@fields"`
	Tags      []string    `json:"@tags"`
	Timestamp string      `json:"@timestamp"`
}

type TextFormatter

type TextFormatter struct {
	AppName string
}

func NewTextFormatter

func NewTextFormatter(appName string) (*TextFormatter, error)

func (*TextFormatter) Format

func (f *TextFormatter) Format(customFields interface{}) (string, error)

Directories

Path Synopsis
Godeps
_workspace/src/github.com/op/go-logging
Package logging implements a logging infrastructure for Go.
Package logging implements a logging infrastructure for Go.

Jump to

Keyboard shortcuts

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