Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Disable ¶
func Disable() parapet.Middleware
Disable disables log
Example ¶
Suppress the access log for matched requests — useful behind a health-check or other high-volume, low-value endpoint to keep logs quiet.
package main
import (
"github.com/moonrhythm/parapet"
"github.com/moonrhythm/parapet/pkg/logger"
)
func main() {
s := parapet.New()
s.Use(logger.Stdout())
s.Use(logger.Disable()) // requests reaching here are not logged
}
Output:
func Set ¶
Set sets log record field
Example ¶
Enrich the log record for the current request from a downstream handler by setting an extra field on the request context; it is encoded alongside the built-in fields when the request completes.
package main
import (
"net/http"
"github.com/moonrhythm/parapet/pkg/logger"
)
func main() {
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
logger.Set(r.Context(), "userId", "u-12345")
w.WriteHeader(http.StatusNoContent)
})
_ = h
}
Output:
Types ¶
type Logger ¶
type Logger struct {
Writer io.Writer
// OmitEmpty, when true, drops fields with empty/zero values (e.g. empty
// referer, realIp, forwardedFor) from the emitted log record. The Stdout
// and Stderr constructors default this to true; a bare Logger{} (or
// &Logger{Writer: x}) defaults it to false, so a zero-value logger emits
// empty fields rather than dropping them.
OmitEmpty bool
}
Logger middleware
Example ¶
Send the access log to a custom destination instead of stdout/stderr — here an in-memory buffer (any io.Writer works, e.g. a file or a log shipper).
package main
import (
"bytes"
"github.com/moonrhythm/parapet"
"github.com/moonrhythm/parapet/pkg/logger"
)
func main() {
var buf bytes.Buffer
s := parapet.New()
s.Use(&logger.Logger{
Writer: &buf,
})
}
Output:
func Stdout ¶
func Stdout() *Logger
Stdout creates new stdout logger
Example ¶
Emit a structured JSON access log line per request to stdout. Each entry carries timing, status, sizes and the client IP derived from the proxy headers; empty/zero fields are dropped from the line.
package main
import (
"github.com/moonrhythm/parapet"
"github.com/moonrhythm/parapet/pkg/logger"
)
func main() {
s := parapet.New()
s.Use(logger.Stdout())
// s.Use(upstream.SingleHost("10.0.0.1:8080")) — the handler whose
// requests get logged.
}
Output: