logger

package module
v2.4.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2018 License: ISC Imports: 8 Imported by: 1

README

go-logger

This package contains shared logic for Go-based structured logging.

This package uses the zap logging package under the hood. See its README for more details on the logging API.

Usage

Add to your main.go:

import logger "github.com/blendle/go-logger"

func main() {
  logger := logger.Must(logger.New("my-service", "cf89f839"))
}

Then use it throughout your application:

logger.Warn("Something happened!")
Custom Zap Options

You can also provide custom Zap options on initialization, if you need it:

sampler := zap.WrapCore(func(core zapcore.Core) zapcore.Core {
  return zapcore.NewSampler(core, time.Second, 100, 100)
})

fields := zap.Fields(zap.String("alwaysAdd", "this"))

logger := logger.Must(logger.New("my-service", "cf89f839", sampler, fields))
Stackdriver logging

You can optionally add Stackdriver specific fields to your logs. These can be used by Stackdriver to improve log readability/grouping.

import zapdriver "github.com/blendle/zapdriver"
logger.Info("Hello", zapdriver.Label("hello", "world"))

See here for all available Stackdriver fields.

Debugging

You can send the USR1 signal to your application to switch the log level between the default INFO and DEBUG level on runtime.

This allows you to capture debug logs during anomalies and find the problem.

You can also set the DEBUG environment variable to true to have the application launch with the default log level set to DEBUG instead of INFO.

Again, you can send USR1 to toggle back to INFO as well.

Testing

This package contains a public testing API you can use if you need to assert a log entry exists.

// TestNew calls New, but returns both the logger, and an observer that can be
// used to fetch and compare delivered logs.
TestNew(tb testing.TB, options ...zap.Option) (*zap.Logger, *observer.ObservedLogs)
// TestNewWithLevel is equal to TestNew, except that it takes an extra argument,
// dictating the minimum log level required to record an entry in the recorder.
TestNewWithLevel(tb testing.TB, level zapcore.LevelEnabler, options ...zap.Option) (*zap.Logger, *observer.ObservedLogs)

see testing.go for more details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Must

func Must(zaplog *zap.Logger, err error) *zap.Logger

Must is a convenience function that takes a zaplog and error as input, panics if the error is not nil, and returns the passed in logger.

This can be used for example with `Must(zap.NewProduction())`

func New

func New(service, version string, options ...zap.Option) (*zap.Logger, error)

New returns a new logger, ready to use in our services.

func TestNew

func TestNew(tb testing.TB, options ...zap.Option) (*zap.Logger, *observer.ObservedLogs)

TestNew calls New, but returns both the logger, and an observer that can be used to fetch and compare delivered logs.

func TestNewWarn

func TestNewWarn(tb testing.TB, options ...zap.Option) (*zap.Logger, *observer.ObservedLogs)

TestNewWarn is equal to TestNew, except that it sets the minimum log level required to record an entry in the recorder to "Warn". This convenience function can be used to validate that no warnings, or errors are logged when testing a unit of code.

func TestNewWithLevel

func TestNewWithLevel(tb testing.TB, level zapcore.LevelEnabler, options ...zap.Option) (*zap.Logger, *observer.ObservedLogs)

TestNewWithLevel is equal to TestNew, except that it takes an extra argument, dictating the minimum log level required to record an entry in the recorder.

Types

This section is empty.

Jump to

Keyboard shortcuts

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