logger

package module
v0.1.8 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2024 License: MIT Imports: 17 Imported by: 1

README

logger

GoDoc

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 Logging, and Console Logging.

The GoogleCloudExporter will also correlate logs to Cloud Trace if you instrument your code with tracing.

The ConsoleExporter is useful for local development and debugging.

The AWSExporter will also correlate logs to AWS X-Ray if you instrument your code with tracing and have logs sent to Cloudwatch. Note that additional configuration in the tracing is required to enable the correlation. In the tracing configuration, you must set the log group names to where the logs are being sent.

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewCtx added in v0.1.5

func NewCtx(ctx context.Context, l *Logger) context.Context

NewCtx associates the logger with the context and returns the resulting context

func NewRequestLogger

func NewRequestLogger(e Exporter) func(http.Handler) http.Handler

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

type Exporter interface {
	Middleware() func(http.Handler) http.Handler
}

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

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 Ctx

func Ctx(ctx context.Context) *Logger

Ctx returns the logger from the context. If no logger is found, it will write to stderr

func Req

func Req(r *http.Request) *Logger

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

func (l *Logger) AddRequestAttribute(key string, value any) *Logger

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) Debug

func (l *Logger) Debug(v any)

Debug logs a debug message.

func (*Logger) Debugf

func (l *Logger) Debugf(format string, v ...any)

Debugf logs a debug message with format.

func (*Logger) Error

func (l *Logger) Error(v any)

Error logs an error message.

func (*Logger) Errorf

func (l *Logger) Errorf(format string, v ...any)

Errorf logs an error message with format.

func (*Logger) Info

func (l *Logger) Info(v any)

Info logs a info message.

func (*Logger) Infof

func (l *Logger) Infof(format string, v ...any)

Infof logs a info message with format.

func (*Logger) TraceID added in v0.1.6

func (l *Logger) TraceID() string

TraceID returns the trace ID of the request logs

func (*Logger) Warn

func (l *Logger) Warn(v any)

Warn logs a warning message.

func (*Logger) Warnf

func (l *Logger) Warnf(format string, v ...any)

Warnf logs a warning message with format.

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

Jump to

Keyboard shortcuts

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