log

package
v0.0.0-...-ee17956 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2022 License: Apache-2.0 Imports: 10 Imported by: 0

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

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 Set

func Set(l Logger)

Set sets the current global logger.

func SetOutput

func SetOutput(writer io.Writer)

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.

func With

func With(ctx context.Context, keysAndValues ...interface{}) context.Context

With gets the current logger for the context or the global one, then constructs a new logger with additional key/value pairs and returns a context with that logger stored in it.

func WithLogger

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

WithLogger returns a context with the given logger stored in it.

Types

type Field

type Field struct {
	Key, Value interface{}
}

Field holds a single key/value pair.

func (Field) String

func (f Field) String() string

type Fields

type Fields []Field

Fields holds key/value pairs for a structured logger.

func (Fields) Clone

func (f Fields) Clone(keysAndValues ...interface{}) Fields

Clone creates a copy with the current set of fields plus any extra ones that might be passed as parameters.

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>]

func (*Formatter) Print

func (f *Formatter) Print(threshold Threshold, fields Fields, args ...interface{}) []byte

Print formats using fmt.Sprint. It adds a line break at the end.

func (*Formatter) Printf

func (f *Formatter) Printf(threshold Threshold, fields Fields, format string, args ...interface{}) []byte

Printf formats using fmt.Sprintf. It adds a line break at the end.

func (*Formatter) Printw

func (f *Formatter) Printw(threshold Threshold, fields Fields, msg string, keysAndValues ...interface{}) []byte

Printw formats a string message with additional key/value pairs. It adds a line break at the end.

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

func FromContext(ctx context.Context) Logger

FromContext returns the current logger associated with the context, the global logger if none is set.

func FromContextFallback

func FromContextFallback(ctx context.Context, fallback Logger) Logger

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

type SimpleConfig struct {
	Level  Threshold
	Output io.Writer
}

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.

type Threshold

type Threshold = level.Threshold

Threshold defines the severity of a log or trace event. It is a type alias to simplify the usage of the log package.

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.

Jump to

Keyboard shortcuts

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