babylogger

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2023 License: MIT Imports: 9 Imported by: 1

README

Babylogger

GoDoc Badge

A Go HTTP logger middleware, for babies.

Example image of Babylogger doing its logging

We’ve used it with Goji and the Go standard library, but it should work with any multiplexer worth its salt. And by that we mean any multiplexer compatible with the standard library.

Note that ANSI escape sequences (read: colors) will be stripped from the output when the logger is not running in a terminal. For example, log files won't contain any sort of ANSI intended for color output.

Also note that for accurate response time logging Babylogger should be the first middleware called.

Examples

Standard Library
package main

import (
    "fmt"
    "net/http"
    "github.com/magicnumbers/babylogger"
)

func main() {
    http.Handle("/", babylogger.Middleware(http.HandlerFunc(handler)))
    http.ListenAndServe(":8000", nil)
}

handler(w http.ResponseWriter, r *http.Request) {
    fmt.FPrintln(w, "Oh, hi, I didn’t see you there.")
}
Goji

import (
    "fmt"
    "net/http"
    "github.com/magicnumbers/babylogger"
    "goji.io"
    "goji.io/pat"
)

func main() {
    mux := goji.NewMux()
    mux.Use(babylogger.Middleware)
    mux.HandleFunc(pat.Get("/"), handler)
    http.ListenAndServe(":8000", mux)
}

handler(w http.ResponseWriter, r *http.Request) {
    fmt.FPrintln(w, "Oh hi, I didn’t see you there.")
}

License

MIT

Documentation

Overview

Package babylogger is a simple HTTP logging middleware. It works with any multiplexer compatible with the Go standard library.

When a terminal is present it will log using nice colors. When the output is not in a terminal (for example in logs) ANSI escape sequences (read: colors) will be stripped from the output.

Also note that for accurate response time logging Babylogger should be the first middleware called.

Windows support is not currently implemented, however it would be trivial enough with the help of a couple packages from Mattn: http://github.com/mattn/go-isatty and https://github.com/mattn/go-colorable

Example using the standard library:

package main

import (
	"fmt"
	"net/http"
	"github.com/meowgorithm/babylogger"
)

func main() {
	http.Handle("/", babylogger.Middleware(http.HandlerFunc(handler)))
	http.ListenAndServe(":8000", nil)
}

handler(w http.ResponseWriter, r *http.Request) {
	fmt.FPrintln(w, "Oh, hi, I didn’t see you there.")
}

Example with Goji:

import (
	"fmt"
	"net/http"
	"github.com/meowgorithm/babylogger"
	"goji.io"
	"goji.io/pat"
)

func main() {
	mux := goji.NewMux()
	mux.Use(babylogger.Middleware)
	mux.HandleFunc(pat.Get("/"), handler)
	http.ListenAndServe(":8000", mux)
}

handler(w http.ResponseWriter, r *http.Request) {
	fmt.FPrintln(w, "Oh hi, I didn’t see you there.")
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Middleware

func Middleware(next http.Handler) http.Handler

Middleware is the logging middleware where we log incoming and outgoing requests for a multiplexer. It should be the first middleware called so it can log request times accurately.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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