httplog

package
v0.0.0-...-8cfe228 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2018 License: BSD-3-Clause Imports: 7 Imported by: 0

Documentation

Overview

Example
package main

import (
	"net/http"
	"time"

	"github.com/Hunsin/beaver/httplog"
)

var handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	// do your staff
	w.WriteHeader(http.StatusOK)
	w.Write([]byte("OK"))
})

func main() {
	// create a log instance which writes data to stdout
	l := httplog.New(nil)

	// set output to a specific file, creating it if not existed
	l.File("http.log")

	// or, simply call httplog.File()
	l = httplog.File("http.log")

	// you can chain you configurations
	l.File("new.log").Prefix("my-app").TimeFormat(time.RFC1123)

	// use the logger as HTTP middleware
	http.ListenAndServe(":8000", l.Listen(handler))
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Logger

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

A Logger represents an logging object that records a series of HTTP requests, writing data to an io.Writer. It guarantees to serialize access to the Writer.

func File

func File(name string) *Logger

File returns a new Logger which output destination is set to named file.

func New

func New(w io.Writer) *Logger

New returns an initialized Logger which output destination is w. If w is nil, the os.Stdout is applied. The default time format is set to time.RFC3339Nano.

func (*Logger) File

func (l *Logger) File(name string) *Logger

File sets the output destination to the named file. If the file doesn't exist, a new file with permission mode 0644 is created. If any error occurred when opening the file, it panics.

func (*Logger) Listen

func (l *Logger) Listen(h http.Handler) http.HandlerFunc

Listen is a http middleware that records the request event and calls the given http.Handler.

func (*Logger) Output

func (l *Logger) Output(w io.Writer) *Logger

Output sets the output destination for the logger.

func (*Logger) Prefix

func (l *Logger) Prefix(prefix string) *Logger

Prefix sets the output prefix for the logger.

func (*Logger) Recover

func (l *Logger) Recover(h PanicHandler) *Logger

Recover sets the PanicHandler of l. If h is nil, a default handler is applied. In other words, the handler of l can not be removed once it is set.

func (*Logger) TimeFormat

func (l *Logger) TimeFormat(layout string) *Logger

TimeFormat sets the time format for the logger. The format rule follows the function Time.Format.

type PanicHandler

type PanicHandler func(http.ResponseWriter, *http.Request, interface{})

A PanicHandler handles HTTP requests and panics recovered from http handlers.

Jump to

Keyboard shortcuts

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