clog

package module
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2021 License: ISC Imports: 9 Imported by: 0

README

clog

GoDoc

go get github.com/Kochava/clog

clog is a simple package for initializing a Zap logger and attaching it to a context, along with functions for logging from the context-attached logger or associating new fields to the logger.

Generally speaking this is a bad use of the context package, but utility won out over passing both a context and a logger around all the time. In particular, this is useful for passing a request-scoped logger through different http.Handler implementations that otherwise do not support Zap.

Usage

A few examples of basic usage follow.

Initialize a logger
// Create a logger at info level with a production configuration.
level := zap.NewAtomicLevelAt(zap.InfoLevel)
l, err := clog.New(level, false)
if err != nil {
    panic(err)
}
l.Info("Ready")
Attach a logger to a context
// var l *zap.Logger

// Attach the logger, l, to a context:
ctx := clog.WithLogger(context.Background(), l)

// Attach fields to the logger:
ctx = clog.With(ctx, zap.Int("field", 1234))

// Log at info level:
clog.Info(ctx, "Log message")

License

clog is made available under the ISC license. A copy of it can be found in the repository in the COPYING file.

Documentation

Overview

Package clog is a convenience package for passing Zap loggers through contexts.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Check

func Check(ctx context.Context, lvl zapcore.Level, msg string) *zapcore.CheckedEntry

Check is a convenience function for calling Logger(ctx).Check(lvl, msg).

func Config

func Config(level zap.AtomicLevel, isDev bool) (conf zap.Config)

Config returns a zap.Config based on the level given and the json and debug parameters. If json is true, the config uses a JSON encoder. If debug is true, production limits on logging are removed and the development flag is set to true.

func DPanic

func DPanic(ctx context.Context, msg string, fields ...zapcore.Field)

DPanic is a convenience function for calling Logger(ctx).DPanic(msg, fields...).

func Debug

func Debug(ctx context.Context, msg string, fields ...zapcore.Field)

Debug is a convenience function for calling Logger(ctx).Debug(msg, fields...).

func Error

func Error(ctx context.Context, msg string, fields ...zapcore.Field)

Error is a convenience function for calling Logger(ctx).Error(msg, fields...).

func Fatal

func Fatal(ctx context.Context, msg string, fields ...zapcore.Field)

Fatal is a convenience function for calling Logger(ctx).Fatal(msg, fields...).

func GetEnvConfig

func GetEnvConfig(procname string) (level zapcore.Level, isDev bool)

GetEnvConfig returns the environment-configured logging level and whether to use JSON and debug logging for procname. If procname is the empty string, os.Args[0] is used instead.

If PROCNAME_LOG_MODE is set to "dev" (case-insensitive) then log output will be formatted for reading on a console. Otherwise, logging defaults to a production configuration.

If PROCNAME_LOG_LEVEL is set to a valid Zap logging level (info, warn, etc.), then that logging level will be returned. Otherwise, the logging level defaults to zap.InfoLevel for production and zap.DebugLevel for development.

func Info

func Info(ctx context.Context, msg string, fields ...zapcore.Field)

Info is a convenience function for calling Logger(ctx).Info(msg, fields...).

func Logger

func Logger(ctx context.Context) (logger *zap.Logger)

Logger returns the zap.Logger for the given context.

func New

func New(level zap.AtomicLevel, isDev bool) (*zap.Logger, error)

New allocates a new zap.Logger using configuration based on the level given and the json and debug parameters, as interpreted by Config.

Example
package main

import (
	"context"

	"github.com/Kochava/clog"
	"go.uber.org/zap"
)

func main() {
	// Create a logger at info level with a production configuration.
	level := zap.NewAtomicLevelAt(zap.InfoLevel)
	l, err := clog.New(level, false)
	if err != nil {
		panic(err)
	}
	l.Info("Ready")

	// Attach the logger, l, to a context:
	ctx := clog.WithLogger(context.Background(), l)

	// Attach fields to the logger:
	ctx = clog.With(ctx, zap.Int("field", 1234))

	// Log at info level:
	clog.Info(ctx, "Log message")
}
Output:

func NewFromEnv

func NewFromEnv(procname string, level zap.AtomicLevel) (*zap.Logger, error)

NewFromEnv allocates a new zap.Logger using configuration from the environment. This looks for PROCNAME_LOG_MODE and PROCNAME_LOG_LEVEL to configure the logger. If LOG_MODE is not "dev", the development configuration of Zap is used. Otherwise, logging is configured for production.

func Panic

func Panic(ctx context.Context, msg string, fields ...zapcore.Field)

Panic is a convenience function for calling Logger(ctx).Panic(msg, fields...).

func Sugar

func Sugar(ctx context.Context) *zap.SugaredLogger

Sugar is a convenience function for calling Logger(ctx).Sugar().

func Warn

func Warn(ctx context.Context, msg string, fields ...zapcore.Field)

Warn is a convenience function for calling Logger(ctx).Warn(msg, fields...).

func With

func With(ctx context.Context, fields ...zapcore.Field) context.Context

With returns a context parented to ctx with a logger that has the given fields appended to it. The logger of ctx is the one returned by Logger.

func WithFieldGenerators added in v0.2.2

func WithFieldGenerators(ctx context.Context, generators ...FieldGenerator) context.Context

WithFieldGenerators adds the supplied FieldGenerator to those found on the supplied context and surfaces a new context

func WithLogger

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

WithLogger returns a context parented to ctx with the given logger attached as a value.

func WithOptions

func WithOptions(ctx context.Context, opts ...zap.Option) context.Context

WithOptions is a convenience function for calling WithLogger(ctx, Logger(ctx).WithOptions(opts...)).

Types

type FieldGenerator added in v0.2.2

type FieldGenerator func(ctx context.Context) []zapcore.Field

FieldGenerator describes a function that generates zap fields for a given context

func FieldGenerators added in v0.2.2

func FieldGenerators(ctx context.Context) (fieldGenerators []FieldGenerator)

FieldGenerators retrieves the FieldGenerators stored on a context

type StdLogger

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

StdLogger is a logger that implements most methods used by "log" stdlib users. Print-ed logs are recorded at Info level.

func NewStdLogger

func NewStdLogger(logger *zap.Logger) *StdLogger

NewStdLogger creats a new StdLogger attached to the given zap.Logger. If logger is nil, NewStdLogger returns nil as well.

func (*StdLogger) Fatal

func (l *StdLogger) Fatal(args ...interface{})

Fatal writes a fatal-level fmt.Sprint-formatted log message.

func (*StdLogger) Fatalf

func (l *StdLogger) Fatalf(format string, args ...interface{})

Fatalf writes a fatal-level fmt.Sprintf-formatted log message.

func (*StdLogger) Fatalln

func (l *StdLogger) Fatalln(args ...interface{})

Fatalln writes a fatal-level fmt.Sprintln-formatted log message.

func (*StdLogger) Panic

func (l *StdLogger) Panic(args ...interface{})

Panic writes a panic-level fmt.Sprint-formatted log message.

func (*StdLogger) Panicf

func (l *StdLogger) Panicf(format string, args ...interface{})

Panicf writes a panic-level fmt.Sprintf-formatted log message.

func (*StdLogger) Panicln

func (l *StdLogger) Panicln(args ...interface{})

Panicln writes a panic-level fmt.Sprintln-formatted log message.

func (*StdLogger) Print

func (l *StdLogger) Print(args ...interface{})

Print writes an info-level fmt.Sprint-formatted log message.

func (*StdLogger) Printf

func (l *StdLogger) Printf(format string, args ...interface{})

Printf writes an info-level fmt.Sprintf-formatted log message.

func (*StdLogger) Println

func (l *StdLogger) Println(args ...interface{})

Println writes an info-level fmt.Sprintln-formatted log message.

func (*StdLogger) With

func (l *StdLogger) With(fields ...zapcore.Field) *StdLogger

With returns a copy of this StdLogger with additional Zap fields.

func (*StdLogger) WithOptions

func (l *StdLogger) WithOptions(opts ...zap.Option) *StdLogger

WithOptions returns a copy of this StdLogger with additional Zap options.

Jump to

Keyboard shortcuts

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