You are viewing development documentation built from the latest commit on main (last tag v0.2.0-rc.2, source commit 25e1a892).
APIs and features are subject to change.
Logger
The logger package provides a small printf-style logger with four levels. The library packages do not import it: the only non-test importer in this repository is examples/getting-started/main.go. It is there for programs that build on Kure and want a logger to hand around.
Overview
The logger provides a simple printf-style interface with support for different log levels. It wraps an underlying logger and provides convenience functions.
Usage
logger.New takes Options for the output, the minimum level, a prefix and whether to print a
timestamp; logger.Default() is logger.New(logger.DefaultOptions()).
The block below is the body of an Example function in example_test.go, which go test runs:
it imports this package as logger, github.com/go-kure/kure/pkg/errors, and os.
Log Levels
From least to most severe. A logger drops every message below its minimum level; the default
minimum is LevelInfo.
| Level constant | Method | Usage |
|---|---|---|
LevelDebug | Debug | Detailed debugging information |
LevelInfo | Info | Normal operational messages |
LevelWarn | Warn | Warning conditions |
LevelError | Error | Error conditions |
Prefixes, levels and helpers
WithPrefix(prefix)returns a new logger whose prefix is the old prefix followed byprefix, and a trailing space is added if there is none.SetLevel(level)changes the minimum level of that logger.logger.FormatBytes(n)formats a byte count (1536becomes1.5 KB), andlogger.FormatDuration(nanos)formats a duration given in nanoseconds (1500000becomes1.50ms).
Conventions
- Use printf-style format strings:
log.Info("processed %d items in %s", n, name) - Use
logger.Noop()when verbose output is disabled - Pass the logger through function parameters or options structs
- Use
logger.Default()only at initialization points (CLI entry, tests)
Related Packages
See the errors package for error handling patterns.