log

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2023 License: Apache-2.0 Imports: 11 Imported by: 15

Documentation

Overview

Package log exposes logging utilities based on go.uber.org/zap.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MustGetLogger

func MustGetLogger(name string, opts ...LoggerOption) (*zap.Logger, func())

MustGetLogger creates a named zap logger, typically to inject into a service runtime as the root logger.

This function returns a configured zap.Logger and a closing function to sync logs upon exit.

It panics upon failures, such as invalid log level, or incapacity to build the underlying logger.

Types

type Factory

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

Factory is wrapper for a logger, which creates logger instances, either contextless or for a given context (e.g. to propagate trace spans).

A factory wraps a logger to propagate log entries as trace spans.

Loggers are zap structured loggers: see go.uber.org/zap.

func GetTestLoggerConfig

func GetTestLoggerConfig(opts ...LoggerOption) (Factory, *zap.Logger, error)

GetTestLoggerConfig is intended to be used in test programs, and inject a logger factory or its underlying *zap.Logger into the tested components.

It is configurable from the "DEBUG_TEST" environment variable: if set, logging is enabled. Otherwise, logging is just muted, allowing to keep test verbosity low.

func MustGetTestLoggerConfig

func MustGetTestLoggerConfig() (Factory, *zap.Logger)

MustGetTestLoggerConfig is a wrapper around GetTestLoggerConfig that panics if an error is encountered.

func NewFactory

func NewFactory(logger *zap.Logger, opts ...Option) Factory

NewFactory creates a new logger Factory for an underlying zap logger.

func (Factory) Bg

func (b Factory) Bg() Logger

Bg creates a context-unaware logger, tied to the Background context.

func (Factory) For

func (b Factory) For(ctx context.Context) Logger

For returns a context-aware Logger.

If the context contains a trace span (from go.opencensus.io/trace), all logging calls are also echo-ed to that span.

NOTE: for Datadog trace correlation, extra fields "dd.trace_id" and "dd.span_id" fields are added to the log entry.

func (Factory) With

func (b Factory) With(fields ...zapcore.Field) Factory

With creates a child logger and optionally adds some context fields to that logger.

func (Factory) WithZapOptions

func (b Factory) WithZapOptions(opts ...zap.Option) Factory

WithZapOptions creates a child logger with some zap.Option

func (Factory) Zap added in v1.0.0

func (b Factory) Zap() *zap.Logger

Zap returns the underlying zap logger

type Logger

type Logger interface {
	Debug(msg string, fields ...zapcore.Field)
	Info(msg string, fields ...zapcore.Field)
	Warn(msg string, fields ...zapcore.Field)
	Error(msg string, fields ...zapcore.Field)
	Fatal(msg string, fields ...zapcore.Field)
	With(fields ...zapcore.Field) Logger
	Zap() *zap.Logger
}

Logger is a simplified abstraction of the zap.Logger

type LoggerOption

type LoggerOption func(*loggerOptions)

LoggerOption sets options to tune the behavior of a logger.

func WithCallerSkip

func WithCallerSkip(skipped int) LoggerOption

WithCallerSkip sets the number of frames in the stack to skip.

By default, this is set to 1, so the logging function itself is skipped.

func WithDisableStackTrace

func WithDisableStackTrace(disabled bool) LoggerOption

WithDisableStackTrace disable stack printing for this logger

func WithIgnoreErr added in v1.0.1

func WithIgnoreErr(enabled bool) LoggerOption

WithIgnoreErr ignores errors when parsing an invalid log level, and silently substitutes the default instead.

func WithLevel

func WithLevel(level string) LoggerOption

WithLevel sets the log level.

The level value must parse to a valid zapcore.Level, i.e. one of error, warn, info, debug values. The default level is "info".

func WithRedirectStdLog added in v1.0.1

func WithRedirectStdLog(enabled bool) LoggerOption

func WithReplaceGlobals added in v1.0.1

func WithReplaceGlobals(enabled bool) LoggerOption

type Option

type Option func(*options)

Option to build a logger factory.

func WithDatadog

func WithDatadog(enabled bool) Option

WithDatadog enables datadog-specific correlation fields to link logs to trace spans (requires to export all trace samples).

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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