Documentation ¶
Overview ¶
package logger is an HTTP request logger that implements correlated logging to one of several supported platforms. Each HTTP request is logged as the parent log, with all logs generated during the request as child logs.
The Logging destination is configured with an Exporter. This package provides Exporters for Google Cloud Logging, AWS CloudWatch, and Console Logging.
The GoogleCloudExporter will also correlate logs to Cloud Trace if you instrument your code with tracing.
The AWSExporter supports log correlation to AWS X-Ray if you instrument your code with tracing.
Index ¶
- func NewCtx(ctx context.Context, l *Logger) context.Context
- func NewRequestLogger(e Exporter) func(http.Handler) http.Handler
- type AWSExporter
- type AttributerLogger
- type ConsoleExporter
- type Exporter
- type GoogleCloudExporter
- type Logger
- func (l *Logger) AddRequestAttribute(key string, value any) *Logger
- func (l *Logger) Debug(v any)
- func (l *Logger) Debugf(format string, v ...any)
- func (l *Logger) Error(v any)
- func (l *Logger) Errorf(format string, v ...any)
- func (l *Logger) Info(v any)
- func (l *Logger) Infof(format string, v ...any)
- func (l *Logger) TraceID() string
- func (l *Logger) Warn(v any)
- func (l *Logger) Warnf(format string, v ...any)
- func (l *Logger) WithAttributes() *AttributerLogger
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewCtx ¶ added in v0.1.5
NewCtx associates the logger with the context and returns the resulting context
func NewRequestLogger ¶
NewRequestLogger returns a middleware that logs the request and injects a Logger into the context. This Logger can be used during the life of the request, and all logs generated will be correlated to the request log.
If not configured, request logs are sent to stderr by default.
Types ¶
type AWSExporter ¶
type AWSExporter struct {
// contains filtered or unexported fields
}
AWSExporter is an Exporter that logs to stdout in JSON format to be sent to cloudwatch
func NewAWSExporter ¶
func NewAWSExporter(logAll bool) *AWSExporter
NewAWSExporter returns a new AWSExporter
func (*AWSExporter) Middleware ¶
func (e *AWSExporter) Middleware() func(http.Handler) http.Handler
Middleware returns a middleware that logs the request and injects a Logger into the context.
type AttributerLogger ¶ added in v0.1.4
type AttributerLogger struct {
// contains filtered or unexported fields
}
func (*AttributerLogger) AddAttribute ¶ added in v0.1.4
func (a *AttributerLogger) AddAttribute(key string, value any) *AttributerLogger
AddAttribute adds an attribute (kv) for the child (trace) log and returns a reference to the original AttributerLogger for method chaining purposes If the key matches a reserved key, it will be prefixed with "custom_" If the key already exists, its value is overwritten
func (*AttributerLogger) Logger ¶ added in v0.1.4
func (a *AttributerLogger) Logger() *Logger
Logger returns a Logger with the child (trace) attributes embedded
type ConsoleExporter ¶
type ConsoleExporter struct {
// contains filtered or unexported fields
}
ConsoleExporter implements exporting to the console
func NewConsoleExporter ¶
func NewConsoleExporter() *ConsoleExporter
NewConsoleExporter returns a configured ConsoleExporter
func (*ConsoleExporter) Middleware ¶
func (e *ConsoleExporter) Middleware() func(http.Handler) http.Handler
Middleware returns a middleware that exports logs to the console
func (*ConsoleExporter) NoColor ¶
func (e *ConsoleExporter) NoColor(v bool) *ConsoleExporter
NoColor controls if this logger will use color to highlight log level
type Exporter ¶
Exporter is the interface for implementing a middleware to export logs to some destination
type GoogleCloudExporter ¶
type GoogleCloudExporter struct {
// contains filtered or unexported fields
}
GoogleCloudExporter implements exporting to Google Cloud Logging
func NewGoogleCloudExporter ¶
func NewGoogleCloudExporter(client *logging.Client, projectID string, opts ...logging.LoggerOption) *GoogleCloudExporter
NewGoogleCloudExporter returns a configured GoogleCloudExporter
func (*GoogleCloudExporter) LogAll ¶
func (e *GoogleCloudExporter) LogAll(v bool) *GoogleCloudExporter
LogAll controls if this logger will log all requests, or only requests that contain logs written to the request Logger (default: true)
func (*GoogleCloudExporter) Middleware ¶
func (e *GoogleCloudExporter) Middleware() func(http.Handler) http.Handler
Middleware returns a middleware that exports logs to Google Cloud Logging
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger implements logging methods for this package
func Req ¶
Req returns the logger from the http request. If no logger is found, it will write to stderr
func (*Logger) AddRequestAttribute ¶ added in v0.1.4
AddRequestAttribute adds an attribute (kv) for the parent request log and returns a reference to the original logger for method chaining purposes If the key matches a reserved key, it will be prefixed with "custom_" If the key already exists, its value is overwritten
func (*Logger) WithAttributes ¶ added in v0.1.4
func (l *Logger) WithAttributes() *AttributerLogger
WithAttributes returns an AttributerLogger that can be used to add child (trace) log attributes