dlog

package
v1.8.0-rc.4 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2020 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package dlog implements a generic logger facade.

There are three first-class things of value in this package:

First: The Logger interface. This is a simple structured logging interface that is mostly trivial to implement on top of most logging backends, and allows library code to not need to care about what specific logging system the calling program uses.

Second: The WithLogger and WithField functions for affecting the logging for a context.

Third: The actual logging functions. If you are writing library code and want to log things, then you should take a context.Context as an argument, and then call dlog.{Level}{,f,ln}(ctx, args) to log.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug added in v1.8.0

func Debug(c context.Context, a ...interface{})

func Debugf added in v1.8.0

func Debugf(c context.Context, f string, a ...interface{})

func Debugln added in v1.8.0

func Debugln(c context.Context, a ...interface{})

func Error added in v1.8.0

func Error(c context.Context, a ...interface{})

func Errorf added in v1.8.0

func Errorf(c context.Context, f string, a ...interface{})

func Errorln added in v1.8.0

func Errorln(c context.Context, a ...interface{})

func Info added in v1.8.0

func Info(c context.Context, a ...interface{})

func Infof added in v1.8.0

func Infof(c context.Context, f string, a ...interface{})

func Infoln added in v1.8.0

func Infoln(c context.Context, a ...interface{})

func Print added in v1.8.0

func Print(c context.Context, a ...interface{})

func Printf added in v1.8.0

func Printf(c context.Context, f string, a ...interface{})

func Println added in v1.8.0

func Println(c context.Context, a ...interface{})

func SetFallbackLogger added in v1.8.0

func SetFallbackLogger(l Logger)

SetFallbackLogger sets the Logger that is returned for a context that doesn't have a Logger associated with it. A nil fallback Logger will cause dlog calls on a context without a Logger to panic, which would be good for detecting places where contexts are not passed around correctly. However, the default fallback logger is Logrus and will behave reasonably; in order to make using dlog a safe "no brainer".

func StdLogger added in v1.8.0

func StdLogger(ctx context.Context, level LogLevel) *log.Logger

StdLogger returns a stdlib *log.Logger that uses the Logger associated with ctx and logs at the specified loglevel.

Avoid using this functions if at all possible; prefer to use the {Trace,Debug,Info,Print,Warn,Error}{f,ln,}() functions.

func Trace added in v1.8.0

func Trace(c context.Context, a ...interface{})

func Tracef added in v1.8.0

func Tracef(c context.Context, f string, a ...interface{})

func Traceln added in v1.8.0

func Traceln(c context.Context, a ...interface{})

func Warn added in v1.8.0

func Warn(c context.Context, a ...interface{})

func Warnf added in v1.8.0

func Warnf(c context.Context, f string, a ...interface{})

func Warning added in v1.8.0

func Warning(c context.Context, a ...interface{})

func Warningf added in v1.8.0

func Warningf(c context.Context, f string, a ...interface{})

func Warningln added in v1.8.0

func Warningln(c context.Context, a ...interface{})

func Warnln added in v1.8.0

func Warnln(c context.Context, a ...interface{})

func WithField added in v1.8.0

func WithField(ctx context.Context, key string, value interface{}) context.Context

WithField returns a copy of ctx with the logger field key=value associated with it, for future calls to {Trace,Debug,Info,Print,Warn,Error}{f,ln,}() and StdLogger().

func WithLogger

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

WithLogger returns a copy of ctx with logger associated with it, for future calls to {Trace,Debug,Info,Print,Warn,Error}{f,ln,}() and StdLogger().

You should only really ever call WithLogger from the initial process set up (i.e. directly inside your 'main()' function).

Types

type LogLevel

type LogLevel uint32

LogLevel is an abstracted common log-level type for for Logger.StdLogger().

const (
	// LogLevelError is for errors that should definitely be noted.
	LogLevelError LogLevel = iota
	// LogLevelWarn is for non-critical entries that deserve eyes.
	LogLevelWarn
	// LogLevelInfo is for general operational entries about what's
	// going on inside the application.
	LogLevelInfo
	// LogLevelDebug is for debugging.  Very verbose logging.
	LogLevelDebug
	// LogLevelTrace is for extreme debugging.  Even finer-grained
	// informational events than the Debug.
	LogLevelTrace
)

type Logger

type Logger interface {
	Helper()
	WithField(key string, value interface{}) Logger
	StdLogger(LogLevel) *log.Logger

	Tracef(format string, args ...interface{})
	Debugf(format string, args ...interface{})
	Infof(format string, args ...interface{})
	Printf(format string, args ...interface{})
	Warnf(format string, args ...interface{})
	Warningf(format string, args ...interface{})
	Errorf(format string, args ...interface{})

	Trace(args ...interface{})
	Debug(args ...interface{})
	Info(args ...interface{})
	Print(args ...interface{})
	Warn(args ...interface{})
	Warning(args ...interface{})
	Error(args ...interface{})

	Traceln(args ...interface{})
	Debugln(args ...interface{})
	Infoln(args ...interface{})
	Println(args ...interface{})
	Warnln(args ...interface{})
	Warningln(args ...interface{})
	Errorln(args ...interface{})
}

Logger is a generic logging interface that most loggers implement, so that consumers don't need to care about the actual log implementation.

Note that unlike logrus.FieldLogger, it does not include Fatal or Panic logging options. Do proper error handling! Return those errors!

func WrapLogrus

func WrapLogrus(in *logrus.Logger) Logger

WrapLogrus converts a logrus *Logger into a generic Logger.

You should only really ever call WrapLogrus from the initial process set up (i.e. directly inside your 'main()' function), and you should pass the result directly to WithLogger.

func WrapTB

func WrapTB(in testing.TB, failOnError bool) Logger

WrapTB converts a testing.TB (that is: either a *testing.T or a *testing.B) into a generic Logger.

Naturally, you should only use this from inside of your *_test.go files.

Jump to

Keyboard shortcuts

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