Documentation
¶
Overview ¶
Package log provides an interface for a structured logger like the SugaredLogger in go.uber.org/zap or sirupsen/logrus. The application developer decides how to provide such a logger. Libraries use this package to get a logger from their current context (preferred) or use the global default.
This goes beyond the idea proposed in https://blog.gopheracademy.com/advent-2016/context-logging/ where a structured logger implementation is set globally and then used to log values attached to a context. In this package, the logger *itself* is attached to the context. This allows different contexts to use different loggers, for example with different settings.
Index ¶
- func InitSimpleFlags() bool
- func Set(l Logger)
- func SetOutput(writer io.Writer)
- func With(ctx context.Context, keysAndValues ...interface{}) context.Context
- func WithLogger(ctx context.Context, logger Logger) context.Context
- type Field
- type Fields
- type Formatter
- func (f *Formatter) Print(threshold Threshold, fields Fields, args ...interface{}) []byte
- func (f *Formatter) Printf(threshold Threshold, fields Fields, format string, args ...interface{}) []byte
- func (f *Formatter) Printw(threshold Threshold, fields Fields, msg string, keysAndValues ...interface{}) []byte
- type LineBuffer
- type Logger
- type LoggerBase
- func (lb *LoggerBase) Debug(args ...interface{})
- func (lb *LoggerBase) Debugf(format string, args ...interface{})
- func (lb *LoggerBase) Debugw(msg string, keysAndValues ...interface{})
- func (lb *LoggerBase) Error(args ...interface{})
- func (lb *LoggerBase) Errorf(format string, args ...interface{})
- func (lb *LoggerBase) Errorw(msg string, keysAndValues ...interface{})
- func (lb *LoggerBase) Fatal(args ...interface{})
- func (lb *LoggerBase) Fatalf(format string, args ...interface{})
- func (lb *LoggerBase) Fatalw(msg string, keysAndValues ...interface{})
- func (lb *LoggerBase) Info(args ...interface{})
- func (lb *LoggerBase) Infof(format string, args ...interface{})
- func (lb *LoggerBase) Infow(msg string, keysAndValues ...interface{})
- func (lb *LoggerBase) Init(logger Logger)
- func (lb *LoggerBase) Panic(args ...interface{})
- func (lb *LoggerBase) Panicf(format string, args ...interface{})
- func (lb *LoggerBase) Panicw(msg string, keysAndValues ...interface{})
- func (lb *LoggerBase) Warn(args ...interface{})
- func (lb *LoggerBase) Warnf(format string, args ...interface{})
- func (lb *LoggerBase) Warnw(msg string, keysAndValues ...interface{})
- type SimpleConfig
- type Threshold
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func InitSimpleFlags ¶
func InitSimpleFlags() bool
InitSimpleFlags sets up flags that configure a simple logger. Currently there is only one, -log.level <threshold>. Always returns true, which makes it possible to do: var _ = InitSimpleFlags()
func SetOutput ¶
SetOutput is a drop-in replacement for the traditional log.SetOutput:
- it installs a simple logger which prints everything
- it writes to the given writer
- the output of traditional log calls are handled to the simple logger, to get consistent logging
This cannot be undone, because there is no function for getting the old io.Writer.
Types ¶
type Formatter ¶
type Formatter struct{}
Formatter can format fields together with a message and severity threshold as plain text. Currently it cannot be customized. The ordering of fields is: <time> <level> [<at>: ]<message> [<key>: <value>]
type LineBuffer ¶
type LineBuffer []byte
LineBuffer can be used to store a byte slice as a field and convert it to a string only on demand. It strips all trailing newlines.
func (LineBuffer) String ¶
func (s LineBuffer) String() string
type Logger ¶
type Logger interface { // Debug uses fmt.Sprint to construct and log a message. Debug(args ...interface{}) // Debugf uses fmt.Sprintf to log a templated message. Debugf(format string, args ...interface{}) // Debugw logs a message with some additional context. The // variadic key-value pairs are treated as they are in With. Debugw(msg string, keysAndValues ...interface{}) // Info uses fmt.Sprint to construct and log a message. Info(args ...interface{}) // Infof uses fmt.Sprintf to log a templated message. Infof(format string, args ...interface{}) // Infow logs a message with some additional context. The // variadic key-value pairs are treated as they are in With. Infow(msg string, keysAndValues ...interface{}) // Warn uses fmt.Sprint to construct and log a message. Warn(args ...interface{}) // Warnf uses fmt.Sprintf to log a templated message. Warnf(format string, args ...interface{}) // Warnw logs a message with some additional context. The // variadic key-value pairs are treated as they are in With. Warnw(msg string, keysAndValues ...interface{}) // Error uses fmt.Sprint to construct and log a message. Error(args ...interface{}) // Errorf uses fmt.Sprintf to log a templated message. Errorf(format string, args ...interface{}) // Errorw logs a message with some additional context. The // variadic key-value pairs are treated as they are in With. Errorw(msg string, keysAndValues ...interface{}) // Fatal uses fmt.Sprint to construct and log a message. // It then quits by calling os.Exit(1). Fatal(args ...interface{}) // Fatalf uses fmt.Sprintf to log a templated message. // It then quits by calling os.Exit(1). Fatalf(format string, args ...interface{}) // Fatalw logs a message with some additional context. The // variadic key-value pairs are treated as they are in With. // It then quits by calling os.Exit(1). Fatalw(msg string, keysAndValues ...interface{}) // Panic uses fmt.Sprint to construct and log a message. // It then quits by panicking. Panic(args ...interface{}) // Panicf uses fmt.Sprintf to log a templated message. // It then quits by panicking. Panicf(format string, args ...interface{}) // Panicw logs a message with some additional context. The // variadic key-value pairs are treated as they are in With. // It then quits by panicking. Panicw(msg string, keysAndValues ...interface{}) // Output prints a message the same way as the function with // the same name as the threshold, using fmt.Sprint, then // exits or panics if needed. Output(threshold Threshold, args ...interface{}) // Output prints a message the same way as the function with // the same name as the threshold, using fmt.Sprintf, then // exits or panics if needed. Outputf(threshold Threshold, format string, args ...interface{}) // Output prints a message the same way as the function with // the same name as the threshold, using the same key-value // pairs as for With, then exits or panics if needed. Outputw(threshold Threshold, msg string, keysAndValues ...interface{}) // With adds a variadic number of fields to the logging // context. It accepts loosely-typed key-value pairs. When // processing pairs, the first element of the pair is used as // the field key and the second as the field value. With(keysAndValues ...interface{}) Logger }
Logger supports structured logging at different severity levels.
func FromContext ¶
FromContext returns the current logger associated with the context, the global logger if none is set.
func FromContextFallback ¶
FromContextFallback returns the current logger associated with the context, the fallback if none is set.
func L ¶
func L() Logger
L returns the current global logger.
There are intentionally no helper functions (i.e. log.Info = log.L().Info), because those add another stack entry between the caller and the logger, which breaks loggers which record the source code location of their direct caller.
func NewSimpleLogger ¶
func NewSimpleLogger(config SimpleConfig) Logger
NewSimpleLogger constructs a new simple logger. The output of the simple logger is plain text (see Formatter).
type LoggerBase ¶
type LoggerBase struct { // Print logs a message the same way as the function with the // same name as the threshold, using fmt.Sprint, then // exits or panics if needed. Print func(threshold Threshold, args ...interface{}) // Printf logs a message the same way as the function with the // same name as the threshold, using fmt.Sprintf, then exits // or panics if needed. Printf func(threshold Threshold, format string, args ...interface{}) // Printw logs a message the same way as the function with the // same name as the threshold, using the same key-value pairs // as for With, then exits or panics if needed. Printw func(threshold Threshold, msg string, keysAndValues ...interface{}) }
The LoggerBase struct can be embedded to simplify the implementation of a Logger: the implementer then only has to implement the three output functions plus With.
func (*LoggerBase) Debug ¶
func (lb *LoggerBase) Debug(args ...interface{})
Debug uses fmt.Sprint to construct and log a message.
func (*LoggerBase) Debugf ¶
func (lb *LoggerBase) Debugf(format string, args ...interface{})
Debugf uses fmt.Sprintf to log a templated message.
func (*LoggerBase) Debugw ¶
func (lb *LoggerBase) Debugw(msg string, keysAndValues ...interface{})
Debugw logs a message with some additional context. The variadic key-value pairs are treated as they are in With.
func (*LoggerBase) Error ¶
func (lb *LoggerBase) Error(args ...interface{})
Error uses fmt.Sprint to construct and log a message.
func (*LoggerBase) Errorf ¶
func (lb *LoggerBase) Errorf(format string, args ...interface{})
Errorf uses fmt.Sprintf to log a templated message.
func (*LoggerBase) Errorw ¶
func (lb *LoggerBase) Errorw(msg string, keysAndValues ...interface{})
Errorw logs a message with some additional context. The variadic key-value pairs are treated as they are in With.
func (*LoggerBase) Fatal ¶
func (lb *LoggerBase) Fatal(args ...interface{})
Fatal uses fmt.Sprint to construct and log a message.
func (*LoggerBase) Fatalf ¶
func (lb *LoggerBase) Fatalf(format string, args ...interface{})
Fatalf uses fmt.Sprintf to log a templated message.
func (*LoggerBase) Fatalw ¶
func (lb *LoggerBase) Fatalw(msg string, keysAndValues ...interface{})
Fatalw logs a message with some additional context. The variadic key-value pairs are treated as they are in With.
func (*LoggerBase) Info ¶
func (lb *LoggerBase) Info(args ...interface{})
Info uses fmt.Sprint to construct and log a message.
func (*LoggerBase) Infof ¶
func (lb *LoggerBase) Infof(format string, args ...interface{})
Infof uses fmt.Sprintf to log a templated message.
func (*LoggerBase) Infow ¶
func (lb *LoggerBase) Infow(msg string, keysAndValues ...interface{})
Infow logs a message with some additional context. The variadic key-value pairs are treated as they are in With.
func (*LoggerBase) Init ¶
func (lb *LoggerBase) Init(logger Logger)
Init installs pointers to the three output functions in the LoggerBase.
func (*LoggerBase) Panic ¶
func (lb *LoggerBase) Panic(args ...interface{})
Panic uses fmt.Sprint to construct and log a message.
func (*LoggerBase) Panicf ¶
func (lb *LoggerBase) Panicf(format string, args ...interface{})
Panicf uses fmt.Sprintf to log a templated message.
func (*LoggerBase) Panicw ¶
func (lb *LoggerBase) Panicw(msg string, keysAndValues ...interface{})
Panicw logs a message with some additional context. The variadic key-value pairs are treated as they are in With.
func (*LoggerBase) Warn ¶
func (lb *LoggerBase) Warn(args ...interface{})
Warn uses fmt.Sprint to construct and log a message.
func (*LoggerBase) Warnf ¶
func (lb *LoggerBase) Warnf(format string, args ...interface{})
Warnf uses fmt.Sprintf to log a templated message.
func (*LoggerBase) Warnw ¶
func (lb *LoggerBase) Warnw(msg string, keysAndValues ...interface{})
Warnw logs a message with some additional context. The variadic key-value pairs are treated as they are in With.
type SimpleConfig ¶
SimpleConfig contains the configuration for NewSimpleLogger.
func NewSimpleConfig ¶
func NewSimpleConfig() SimpleConfig
NewSimpleConfig returns a configuration for NewSimpleLogger that is populated by command line flags. InitSimpleFlags and flag.Parse must have been called first, otherwise the defaults are returned.
Directories
¶
Path | Synopsis |
---|---|
Package level provides a common definition of the severity of log or trace events.
|
Package level provides a common definition of the severity of log or trace events. |
Package testlog provides a logger that outputs via testing.T.Log.
|
Package testlog provides a logger that outputs via testing.T.Log. |