weblogs

package module
v0.0.0-...-68b51cc Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2013 License: BSD-3-Clause Imports: 10 Imported by: 0

README

weblogs

Easily add web access logs to your go http server.

This API is now stable. Any future changes will be bacckward compatible with existing code. However, any future function or data structure in "draft" mode may change in incompatible ways. Such function or data structure will be clearly marked as "draft" in the documentation.

Using

import "github.com/bancek/weblogs"

Installing

go get github.com/bancek/weblogs

Features

If server panics before sending a response, weblogs automatically sends a 500 error to client and logs the panic.

Online Documentation

Online documentation available here.

Dependencies

This package depends on github.com/gorilla/context.

Example Usage

handler := context.ClearHandler(weblogs.Handler(http.DefaultServeMux))
http.ListenAndServe(":80", handler)

Documentation

Overview

Package weblogs provides access logs for webservers written in go.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Handler

func Handler(handler http.Handler) http.Handler

Handler wraps a handler creating access logs. Access logs are written to stderr using SimpleLogger(). Returned handler must be wrapped by context.ClearHandler.

func HandlerWithOptions

func HandlerWithOptions(
	handler http.Handler, options *Options) http.Handler

HandlerWithOptions wraps a handler creating access logs and allows caller to configure how access logs are written. Returned handler must be wrapped by context.ClearHandler.

func Values

func Values(r *http.Request) map[interface{}]interface{}

Values returns the current key-value pairs to be logged. If the handler calling this is not wrapped by the Handler() method, then this method returns nil.

func Writer

func Writer(r *http.Request) io.Writer

Writer returns a writer whereby the caller can add additional information to the current log entry. If the handler calling this is not wrapped by the Handler() method, then writing to the returned io.Writer does nothing.

Types

type Capture

type Capture interface {
	http.ResponseWriter
	// HasStatus returns true if server has sent a status. False means that
	// server failed to send a response.
	HasStatus() bool
}

Capture captures a server response. Implementations delegate to an underlying ResponseWriter.

type LogRecord

type LogRecord struct {
	// The time request was received.
	T time.Time
	// The request snapshot
	R Snapshot
	// The capture of the response
	W Capture
	// Time spent processing the request
	Duration time.Duration
	// Additional information added with the Writer method.
	Extra string
	// Key-value pairs to be logged.
	Values map[interface{}]interface{}
}

LogRecord represents a single entry in the access logs.

type Logger

type Logger interface {
	// NewSnapshot creates a new snapshot of a request.
	NewSnapshot(r *http.Request) Snapshot
	// NewCapture creates a new capture for capturing a response. w is the
	// original ResponseWriter.
	NewCapture(w http.ResponseWriter) Capture
	// Log writes the log record.
	Log(w io.Writer, record *LogRecord)
}

Logger represents an access log format. Clients are free to provide their own implementations.

func ApacheCombinedLogger

func ApacheCombinedLogger() Logger

ApacheCombinedLogger provides access logs in apache combined log format.

func ApacheCommonLogger

func ApacheCommonLogger() Logger

ApacheCommonLogger provides access logs in apache common log format.

func SimpleLogger

func SimpleLogger() Logger

SimpleLogger provides access logs with the following columns: date, remote address, method, URI, status, time elapsed milliseconds, followed by any additional information provided via the Writer method.

type Options

type Options struct {
	// Where to write the web logs. nil means write to stderr,
	Writer io.Writer
	// How to write the web logs. nil means SimpleLogger().
	Logger Logger
	// How to get current time. nil means use time.Now(). This field is used
	// for testing purposes.
	Now func() time.Time
}

Options specifies options for writing to access logs.

type Snapshot

type Snapshot interface{}

Snapshot represents a snapshot of an HTTP request.

Directories

Path Synopsis
Package loggers provides routines for creating weblogs.Logger implementations.
Package loggers provides routines for creating weblogs.Logger implementations.

Jump to

Keyboard shortcuts

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