logging

package
v0.9.6 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2024 License: BSD-3-Clause Imports: 13 Imported by: 2

Documentation

Index

Constants

View Source
const HandlerKey = "handler"

HandlerKey is a log key for the handler name added by HandlerLogger function.

Variables

This section is empty.

Functions

func ApplicationLogWriteCloser

func ApplicationLogWriteCloser(dir, name string, fallback io.Writer) io.WriteCloser

ApplicationLogWriteCloser returns a writer which is a daily rotated file constructed using NewDailyReplaceableWriterConstructor.

func HandlerLogger added in v0.8.2

func HandlerLogger(r *http.Request, handlerName string) *slog.Logger

HandlerLogger provides a logger from HTTP request with attached name of the handler.

func NewAccessLogHandler

func NewAccessLogHandler(h http.Handler, logger *slog.Logger, o *AccessLogOptions) http.Handler

NewHandler returns a handler that logs HTTP requests. It logs information about remote address, X-Forwarded-For or X-Real-Ip, HTTP method, request URI, HTTP protocol, HTTP response status, total bytes written to http.ResponseWriter, response duration, HTTP referrer and HTTP client user agent.

func NewApplicationLoggerCloser added in v0.8.3

func NewApplicationLoggerCloser(dir, name string, newHandler func(io.Writer) slog.Handler, fallback io.Writer) (l *slog.Logger, closeFunc func() error)

NewApplicationLoggerCloser construct a logger and returns a closer of its writer. It uses ApplicationLogWriteCloser with iostuff.NewDailyReplaceableWriterConstructor for log rotation.

func NewContextLoggerHandler added in v0.8.2

func NewContextLoggerHandler(h http.Handler, l *slog.Logger) http.Handler

NewContextLoggerHandler injects logger into HTTP request Context. HandlerLogger function can be used to get the logger and attach a handler name.

func NewDailyReplaceableWriterConstructor

func NewDailyReplaceableWriterConstructor(dir, name string) func(flag string) (f io.Writer, today string, err error)

NewDailyReplaceableWriterConstructor creates a writer constructor that can be used for daily rotated files for iostuff.NewReplaceableWriter. The files are named using pattern dir/2006/01/02/name.log where dir and name are passed arguments and the date parameters are year, month and day values of the current time.

func NewJSONHandler added in v0.8.3

func NewJSONHandler(w io.Writer, opts *slog.HandlerOptions) slog.Handler

NewJSONHandler calls slog.NewJSONHandler but returns the Logger interface to be used as an argument in NewApplicationLoggerCloser.

func NewSlogContext added in v0.8.9

func NewSlogContext(ctx context.Context, l *slog.Logger) context.Context

NewSlogContext returns a context that contains the given Logger. Use FromContext to retrieve the Logger.

func NewTextHandler added in v0.8.3

func NewTextHandler(w io.Writer, opts *slog.HandlerOptions) slog.Handler

NewTextHandler calls slog.NewTextHandler but returns the Logger interface to be used as an argument in NewApplicationLoggerCloser.

func SlogFromContext added in v0.8.9

func SlogFromContext(ctx context.Context) *slog.Logger

SlogFromContext returns the Logger stored in ctx by NewContext, or the default Logger if there is none.

Types

type AccessLogOptions

type AccessLogOptions struct {
	RealIPHeaderName string
	PreHook          http.HandlerFunc
	PostHook         func(code int, duration time.Duration, written int64)
	LogMessage       string
}

type Metrics

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

Metrics holds Prometheus counters.

func NewMetrics

func NewMetrics(options *MetricsOptions) (c *Metrics)

NewMetrics creates new Counter instance. Options value can be nil.

func (*Metrics) Inc

func (c *Metrics) Inc(loggerName string, level slog.Level)

func (*Metrics) Metrics

func (c *Metrics) Metrics() (cs []prometheus.Collector)

Metrics returns all Prometheus metrics that should be registered.

type MetricsOptions

type MetricsOptions struct {
	Namespace string
	Subsystem string
	Name      string
	Help      string
}

MetricsOptions holds options for NewCounter constructor.

Jump to

Keyboard shortcuts

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