accesslog

package module
v0.0.0-...-cc25b67 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2017 License: MIT Imports: 10 Imported by: 0

README

GoDoc Go Report Card

AccessLog

Provides simple middleware logging that conforms to Apache Common Log and Apache Combined Log formats. It can also have custom formats using the Apache Log Directives.

Installation

go get github.com/0xa4b/accesslog

Example

import (
    "fmt"
    "log"
    "net/http"
    "os"

    "github.com/0xa4b/accesslog"
)

func heartBeatHandler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintln(w, "beat")
}

func homeHandler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintln(w, "you are home")
}

func main(){

    f, err := os.Create("access.log")
    if err != nil {
        log.Fatalf("access log create error: %v", err)
    }
    defer f.Close()

    // log to stdout
    http.Handle("/heart-beat", accesslog.ApacheCommonLogWith()(heartBeatHandler))
    
    // log to access file
    http.Handle("/home", accesslog.ApacheCommonLogWith(f)(homeHandler))

    log.Fatal(http.ListenAndServe(":8080", nil))
}

License

AccessLog is available under the MIT License.

Documentation

Index

Constants

View Source
const (
	// ApacheCommonLogFormat is the Apache Common Log directives
	ApacheCommonLogFormat = "%h %l %u %t \"%r\" %>s %b"

	// ApacheCombinedLogFormat is the Apache Combined Log directives
	ApacheCombinedLogFormat = "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\""
)

Variables

View Source
var ApacheCombinedLog = Format(ApacheCombinedLogFormat)

ApacheCombinedLog will log HTTP requests using the Apache Combined Log format

View Source
var ApacheCommonLog = Format(ApacheCommonLogFormat)

ApacheCommonLog will log HTTP requests using the Apache Common Log format

Functions

func Format

func Format(format string) func(...optFunc) func(http.Handler) http.Handler

Format accepts a format string using Apache formatting directives and returns a function accepting internal option functions which then returns a function that can handle standard HTTP middleware. This function more convenient to use when saving formatting to a variable, then using with standard HTTP middleware

func FormatWith

func FormatWith(format string, opts ...optFunc) func(http.Handler) http.Handler

FormatWith accepts a format string using Apache formatting directives with option functions and returns a function that can handle standard HTTP middleware.

func WithOutput

func WithOutput(out io.Writer) optFunc

WithOutput sets the io.Writer output for the log file.

Types

This section is empty.

Jump to

Keyboard shortcuts

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