clzap

package
v0.29.2 Latest Latest
Warning

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

Go to latest
Published: May 3, 2024 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package clzap provides logging using the zap logging library

Index

Constants

This section is empty.

Variables

View Source
var LambdaRequestIDKey = "requestId" //nolint:gochecknoglobals

LambdaRequestIDKey determines the logging key for the AWS request id from the lambda context.

Functions

func Fx

func Fx() fx.Option

Fx is a convenient option that configures fx to use the zap logger.

func Log added in v0.9.1

func Log(ctx context.Context, fbl ...*zap.Logger) *zap.Logger

Log retrieves a zap logger from the context. If the optional fallback logger is provided this logger is returned when the context has no logger, else a no-op logger is returned. If the context also has tracing and or span information this will be logged by the logger automatically.

func LoggerFromContext added in v0.9.2

func LoggerFromContext(ctx context.Context) (*zap.Logger, bool)

LoggerFromContext attempts to get a zap logger from the context. Returns nil and false if there is no logger in the context.

func Observed

func Observed() fx.Option

Observed configures a logging module that allows for observing while also writing console output to a io.Writer that needs to be supplied.

func Provide added in v0.17.0

func Provide() fx.Option

Provide logging module. It can be used as a fx Module in production binaries to provide high-performance structured logging.

func TestProvide added in v0.17.0

func TestProvide() fx.Option

TestProvide is a convenient fx option setup that can easily be included in all tests. It observed the logs for assertion and writes console output to the GinkgoWriter so all logs can easily be inspected if tests fail.

func TraceHook added in v0.5.0

func TraceHook() func(ctx context.Context, f []zap.Field) []zap.Field

TraceHook creates a context logger hook that appends trace information.

func WithLogger added in v0.9.1

func WithLogger(ctx context.Context, logs *zap.Logger) context.Context

WithLogger returns a context with the provided logger embedded.

Types

type Config

type Config struct {
	// Level configures the minium logging level that will be captured.
	Level zapcore.Level `env:"LEVEL" envDefault:"info"`
	// Outputs configures the zap outputs that will be opened for logging.
	Outputs []string `env:"OUTPUTS" envDefault:"stderr"`
	// Configure the level at which fx logs are shown, default to debug
	FxLevel zapcore.Level `env:"FX_LEVEL" envDefault:"debug"`
	// By default it logs to lambda format, this
	DisableLambdaEncoding bool `env:"DISABLE_LAMBDA_ENCODING"`
}

Config configures the logging package.

type ContextHook added in v0.4.0

type ContextHook func(ctx context.Context, f []zap.Field) []zap.Field

ContextHook allows adding fields based on the context.

type ContextLogger deprecated added in v0.4.0

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

ContextLogger wraps a zap loger but only allows logging with a context. This allows users to force the logging of contextual fields such as trace-ids that would otherwise cause logs to not be observable per trace. It is not provided as a dependency to fx because it is expected that this logger is initialized inside the components on a case-by-case basis. We want the signature of components constructs NOT to be dependant on a contextual logger, to imporove portability.

Deprecated: the less surprising way to do contextual logging is to embed the logger in the context. This approach can be found in the ctx.go. It is also simpler and doesn't require a home-made type. The ContextLogger was mainly useful to allow named loggers per component.

func NewContextLogger added in v0.4.0

func NewContextLogger(logs *zap.Logger, hook ...ContextHook) *ContextLogger

NewContextLogger inits our contextual with the underlying zapcore logger.

func NewTraceContextLogger added in v0.5.0

func NewTraceContextLogger(logs *zap.Logger) *ContextLogger

NewTraceContextLogger inits a contextual logger that adds trace information to each log line.

func (*ContextLogger) Check added in v0.4.0

func (log *ContextLogger) Check(lvl zapcore.Level, msg string) *zapcore.CheckedEntry

Check returns a CheckedEntry if logging a message at the specified level is enabled. It's a completely optional optimization; in high-performance applications, Check can help avoid allocating a slice to hold fields.

func (*ContextLogger) DPanic added in v0.4.0

func (log *ContextLogger) DPanic(ctx context.Context, msg string, fields ...zap.Field)

DPanic logs a message at DPanicLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger.

If the logger is in development mode, it then panics (DPanic means "development panic"). This is useful for catching errors that are recoverable, but shouldn't ever happen.

func (*ContextLogger) Debug added in v0.4.0

func (log *ContextLogger) Debug(ctx context.Context, msg string, fields ...zap.Field)

Debug logs a message at DebugLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger.

func (*ContextLogger) Error added in v0.4.0

func (log *ContextLogger) Error(ctx context.Context, msg string, fields ...zap.Field)

Error logs a message at ErrorLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger.

func (*ContextLogger) Fatal added in v0.4.0

func (log *ContextLogger) Fatal(ctx context.Context, msg string, fields ...zap.Field)

Fatal logs a message at FatalLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger.

The logger then calls os.Exit(1), even if logging at FatalLevel is disabled.

func (*ContextLogger) Info added in v0.4.0

func (log *ContextLogger) Info(ctx context.Context, msg string, fields ...zap.Field)

Info logs a message at InfoLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger.

func (*ContextLogger) Level added in v0.4.0

func (log *ContextLogger) Level() zapcore.Level

Level reports the minimum enabled level for this logger.

For NopLoggers, this is zapcore.InvalidLevel.

func (*ContextLogger) Log added in v0.4.0

func (log *ContextLogger) Log(ctx context.Context, lvl zapcore.Level, msg string, fields ...zap.Field)

Log logs a message at the specified level. The message includes any fields passed at the log site, as well as any fields accumulated on the logger.

func (*ContextLogger) Named added in v0.4.0

func (log *ContextLogger) Named(s string) *ContextLogger

Named adds a new path segment to the logger's name. Segments are joined by periods. By default, Loggers are unnamed.

func (*ContextLogger) Panic added in v0.4.0

func (log *ContextLogger) Panic(ctx context.Context, msg string, fields ...zap.Field)

Panic logs a message at PanicLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger.

The logger then panics, even if logging at PanicLevel is disabled.

func (*ContextLogger) Sync added in v0.4.0

func (log *ContextLogger) Sync() error

Sync calls the underlying Core's Sync method, flushing any buffered log entries. Applications should take care to call Sync before exiting.

func (*ContextLogger) Warn added in v0.4.0

func (log *ContextLogger) Warn(ctx context.Context, msg string, fields ...zap.Field)

Warn logs a message at WarnLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger.

func (*ContextLogger) With added in v0.4.0

func (log *ContextLogger) With(fields ...zap.Field) *ContextLogger

With creates a child logger and adds structured context to it. Fields added to the child don't affect the parent, and vice versa.

func (*ContextLogger) WithOptions added in v0.4.0

func (log *ContextLogger) WithOptions(opts ...zap.Option) *ContextLogger

WithOptions clones the current Logger, applies the supplied Options, and returns the resulting Logger. It's safe to use concurrently.

Jump to

Keyboard shortcuts

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