zlog

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

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

Go to latest
Published: Mar 4, 2020 License: Apache-2.0 Imports: 19 Imported by: 0

README

Caddy-zlog

Overview

zlog is a log middleware for Caddy, it's based on https://github.com/rs/zerolog and https://github.com/liuzl/filestore.

Installation

Rebuild caddy as follows:

  1. git clone https://github.com/liuzl/caddy-zlog
  2. copy caddy-zlog to github.com/caddyserver/caddy/caddyhttp/zlog
  3. add _ "github.com/caddyserver/caddy/caddyhttp/zlog" to file github.com/caddyserver/caddy/caddyhttp/caddyhttp.go
  4. add zlog to the variable directives in file github.com/caddyserver/caddy/caddyhttp/httpserver/plugin.go
  5. cd github.com/caddyserver/caddy/caddy && go build

Caddyfile syntax

127.0.0.1 {
    zlog {
        log_dir ./server_zerolog
        split_by hour
    }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AccessHandler

func AccessHandler(f func(r *http.Request, status, size int, duration time.Duration)) func(next httpserver.Handler) httpserver.Handler

AccessHandler returns a handler that call f after each request.

func DelResponseHeaderHandler

func DelResponseHeaderHandler(headerName string) func(next httpserver.Handler) httpserver.Handler

func DumpRequestHandler

func DumpRequestHandler(fieldKey string) func(next httpserver.Handler) httpserver.Handler

func DumpResponseHandler

func DumpResponseHandler(fieldKey string) func(next httpserver.Handler) httpserver.Handler

func FromRequest

func FromRequest(r *http.Request) *zerolog.Logger

FromRequest gets the logger in the request's context. This is a shortcut for log.Ctx(r.Context())

func HeaderHandler

func HeaderHandler(headerName string) func(next httpserver.Handler) httpserver.Handler

HeaderHandler adds the request's headerName from Header as a field to the context's logger using headerName as field key.

func IDFromRequest

func IDFromRequest(r *http.Request, headerName string) (id xid.ID, err error)

IDFromRequest returns the unique id associated to the request if any.

func MethodHandler

func MethodHandler(fieldKey string) func(next httpserver.Handler) httpserver.Handler

MethodHandler adds the request method as a field to the context's logger using fieldKey as field key.

func NewHandler

func NewHandler(log zerolog.Logger) func(httpserver.Handler) httpserver.Handler

NewHandler injects log into requests context.

func RefererHandler

func RefererHandler(fieldKey string) func(next httpserver.Handler) httpserver.Handler

RefererHandler adds the request's referer as a field to the context's logger using fieldKey as field key.

func RemoteAddrHandler

func RemoteAddrHandler(fieldKey string) func(next httpserver.Handler) httpserver.Handler

RemoteAddrHandler adds the request's remote address as a field to the context's logger using fieldKey as field key.

func RequestHandler

func RequestHandler(fieldKey string) func(next httpserver.Handler) httpserver.Handler

RequestHandler adds the request method and URL as a field to the context's logger using fieldKey as field key.

func RequestIDHandler

func RequestIDHandler(fieldKey, headerName string) func(next httpserver.Handler) httpserver.Handler

RequestIDHandler returns a handler setting a unique id to the request which can be gathered using IDFromRequest(req). This generated id is added as a field to the logger using the passed fieldKey as field name. The id is also added as a response header if the headerName is not empty.

The generated id is a URL safe base64 encoded mongo object-id-like unique id. Mongo unique id generation algorithm has been selected as a trade-off between size and ease of use: UUID is less space efficient and snowflake requires machine configuration.

func ResponseHeaderHandler

func ResponseHeaderHandler(headerName, valType string) func(next httpserver.Handler) httpserver.Handler

func URLHandler

func URLHandler(fieldKey string) func(next httpserver.Handler) httpserver.Handler

URLHandler adds the requested URL as a field to the context's logger using fieldKey as field key.

func UserAgentHandler

func UserAgentHandler(fieldKey string) func(next httpserver.Handler) httpserver.Handler

UserAgentHandler adds the request's user-agent as a field to the context's logger using fieldKey as field key.

func WithLog

func WithLog(h httpserver.Handler,
	dir, splitBy string, once sync.Once) httpserver.Handler

Types

type Chain

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

Chain acts as a list of httpserver.Handler constructors. Chain is effectively immutable: once created, it will always hold the same set of constructors in the same order.

func NewChain

func NewChain(constructors ...Constructor) Chain

New creates a new chain, memorizing the given list of middleware constructors. New serves no other function, constructors are only called upon a call to Then().

func (Chain) Append

func (c Chain) Append(constructors ...Constructor) Chain

Append extends a chain, adding the specified constructors as the last ones in the request flow.

Append returns a new chain, leaving the original one untouched.

stdChain := NewChain(m1, m2)
extChain := stdChain.Append(m3, m4)
// requests in stdChain go m1 -> m2
// requests in extChain go m1 -> m2 -> m3 -> m4

func (Chain) Extend

func (c Chain) Extend(chain Chain) Chain

Extend extends a chain by adding the specified chain as the last one in the request flow.

Extend returns a new chain, leaving the original one untouched.

stdChain := NewChain(m1, m2)
ext1Chain := NewChain(m3, m4)
ext2Chain := stdChain.Extend(ext1Chain)
// requests in stdChain go  m1 -> m2
// requests in ext1Chain go m3 -> m4
// requests in ext2Chain go m1 -> m2 -> m3 -> m4

Another example:

 aHtmlAfterNosurf := NewChain(m2)
	aHtml := NewChain(m1, func(h httpserver.Handler) httpserver.Handler {
		csrf := nosurf.New(h)
		csrf.SetFailureHandler(aHtmlAfterNosurf.ThenFunc(csrfFail))
		return csrf
	}).Extend(aHtmlAfterNosurf)
		// requests to aHtml hitting nosurfs success handler go m1 -> nosurf -> m2 -> target-handler
		// requests to aHtml hitting nosurfs failure handler go m1 -> nosurf -> m2 -> csrfFail

func (Chain) Then

Then chains the middleware and returns the final httpserver.Handler.

New(m1, m2, m3).Then(h)

is equivalent to:

m1(m2(m3(h)))

When the request comes in, it will be passed to m1, then m2, then m3 and finally, the given handler (assuming every middleware calls the following one).

A chain can be safely reused by calling Then() several times.

stdStack := NewChain(ratelimitHandler, csrfHandler)
indexPipe = stdStack.Then(indexHandler)
authPipe = stdStack.Then(authHandler)

Note that constructors are called on every call to Then() and thus several instances of the same middleware will be created when a chain is reused in this way. For proper middleware, this should cause no problems.

func (Chain) ThenFunc

ThenFunc works identically to Then, but takes a HandlerFunc instead of a Handler.

The following two statements are equivalent:

c.Then(httpserver.HandlerFunc(fn))
c.ThenFunc(fn)

ThenFunc provides all the guarantees of Then.

type Config

type Config map[string]string

type Constructor

type Constructor func(httpserver.Handler) httpserver.Handler

A constructor for a piece of middleware. Some middleware use this constructor out of the box, so in most cases you can just pass somepackage.New

type ResponseLog

type ResponseLog struct {
	Request    *http.Request
	StatusCode int    `json:"staus_code"`
	Body       string `json:"body"`
	Header     string `json:"header"`
}

func (ResponseLog) DumpResponse

func (rl ResponseLog) DumpResponse() string

type ResponseProxyWriter

type ResponseProxyWriter struct {
	Body []byte
	Code int

	SourceHeader http.Header
	// contains filtered or unexported fields
}

func NewRespProxyWriter

func NewRespProxyWriter(w http.ResponseWriter) *ResponseProxyWriter

func (*ResponseProxyWriter) Header

func (w *ResponseProxyWriter) Header() http.Header

func (*ResponseProxyWriter) Write

func (w *ResponseProxyWriter) Write(bytes []byte) (int, error)

func (*ResponseProxyWriter) WriteHeader

func (w *ResponseProxyWriter) WriteHeader(i int)

type ZLog

type ZLog struct {
	Next httpserver.Handler
}

func (ZLog) ServeHTTP

func (z ZLog) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error)

ServeHTTP implements the httpserver.Handler interface.

Jump to

Keyboard shortcuts

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