clzap

package
v0.7.3 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2023 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Observed = fx.Module(moduleName+"-observed",
	fx.Provide(fx.Annotate(parseConfig, fx.ParamTags(`optional:"true"`))),
	fx.Provide(func(cfg Config) zapcore.LevelEnabler { return cfg.Level }),
	fx.Provide(newObservedAndConsole),
	fx.Provide(fx.Annotate(zap.New, fx.OnStop(func(ctx context.Context, l *zap.Logger) error {
		return l.Sync()
	}))),
)

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

View Source
var Prod = fx.Module(moduleName,

	fx.Provide(fx.Annotate(parseConfig, fx.ParamTags(`optional:"true"`))),

	fx.Provide(func(cfg Config) zapcore.LevelEnabler { return cfg.Level }),

	fx.Provide(fx.Annotate(zap.New, fx.OnStop(func(ctx context.Context, l *zap.Logger) error {
		l.Sync()
		return nil
	}))),

	fx.Provide(zapcore.NewCore, zapcore.NewJSONEncoder, zap.NewProductionEncoderConfig),

	fx.Provide(func(cfg Config) (w zapcore.WriteSyncer, err error) {
		w, _, err = zap.Open(cfg.Outputs...)
		return
	}),
)

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

View Source
var Test = fx.Options(Fx(),

	fx.Supply(fx.Annotate(ginkgo.GinkgoWriter, fx.As(new(io.Writer)))),
	Observed)

Test 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.

Functions

func Fx

func Fx() fx.Option

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

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

Types

type Config

type Config struct {
	// Level configures the 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"`
}

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 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.

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