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