log

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2020 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	// DefaultConsoleLevel is the default log level for the console.
	DefaultConsoleLevel = "info"
	// DefaultStacktraceLevel is the default log level for which stack traces are included.
	DefaultStacktraceLevel = "error"
)

Variables

This section is empty.

Functions

func CtxWith added in v0.4.0

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

CtxWith returns a new context, based on ctx, that embeds argument logger. The logger can be recovered using GetLogger. Attaching a logger to a context which already contains one will overwrite the existing value.

func Debug

func Debug(msg string, ctx ...interface{})

Debug logs at debug level.

func Discard added in v0.5.0

func Discard()

Discard sets the logger up to discard all log entries. This is useful for testing.

func Error

func Error(msg string, ctx ...interface{})

Error logs at error level.

func Flush

func Flush()

Flush writes the logs to the underlying buffer.

func HandlePanic added in v0.5.0

func HandlePanic()

HandlePanic catches panics and logs them.

func Info

func Info(msg string, ctx ...interface{})

Info logs at info level.

func SafeDebug added in v0.6.0

func SafeDebug(l Logger, msg string, fields ...interface{})

SafeDebug logs to l only if l is not nil.

func SafeError added in v0.6.0

func SafeError(l Logger, msg string, fields ...interface{})

SafeError logs to l only if l is not nil.

func SafeInfo added in v0.6.0

func SafeInfo(l Logger, msg string, fields ...interface{})

SafeInfo logs to l only if l is not nil.

func Setup added in v0.5.0

func Setup(cfg Config) error

Setup configures the logging library with the given config.

Types

type Config added in v0.5.0

type Config struct {
	config.NoValidator
	// Console is the configuration for the console logging.
	Console ConsoleConfig `toml:"console,omitempty"`
}

Config is the configuration for the logger.

func (*Config) ConfigName added in v0.5.0

func (c *Config) ConfigName() string

ConfigName returns the name this config should have in a struct embedding this.

func (*Config) InitDefaults added in v0.5.0

func (c *Config) InitDefaults()

InitDefaults populates unset fields in cfg to their default values (if they have one).

func (*Config) Sample added in v0.5.0

func (c *Config) Sample(dst io.Writer, path config.Path, ctx config.CtxMap)

Sample writes the sample configuration to the dst writer.

type ConsoleConfig added in v0.5.0

type ConsoleConfig struct {
	// Level of console logging (defaults to DefaultConsoleLevel).
	Level string `toml:"level,omitempty"`
	// Format of the console logging. (human|json)
	Format string `toml:"format,omitempty"`
	// StacktraceLevel sets from which level stacktraces are included.
	StacktraceLevel string `toml:"stacktrace_level,omitempty"`
}

ConsoleConfig is the config for the console logger.

func (*ConsoleConfig) InitDefaults added in v0.5.0

func (c *ConsoleConfig) InitDefaults()

InitDefaults populates unset fields in cfg to their default values (if they have one).

type DebugID added in v0.5.0

type DebugID uint32

DebugID is used to correlate behavior in logs. A DebugID is allocated for each outgoing request/response or notify message exchange, and for each handler executed during ListenAndServe.

func NewDebugID added in v0.5.0

func NewDebugID() DebugID

NewDebugID creates a new debug id.

func (DebugID) String added in v0.5.0

func (id DebugID) String() string

type DiscardLogger added in v0.6.0

type DiscardLogger struct{}

DiscardLogger implements the Logger interface and discards all messages. Subloggers created from this logger will also discard all messages and ignore the additional context.

To see how to use this, see the example.

Example
// LITERALINCLUDE ExampleDiscardLogger START
type Server struct {
	// Logger is used by the server to log information about internal events. If nil, logging
	// is disabled.
	Logger Logger
}

// Use a func because this is a godoc example, but this would normally be something like
// s.Run().
Run := func(s *Server) {
	if s.Logger == nil {
		s.Logger = DiscardLogger{}
	}
	s.Logger.Debug("this message is discarded now")
}

Run(&Server{})
// LITERALINCLUDE ExampleDiscardLogger END
Output:

func (DiscardLogger) Debug added in v0.6.0

func (DiscardLogger) Debug(msg string, ctx ...interface{})

func (DiscardLogger) Error added in v0.6.0

func (DiscardLogger) Error(msg string, ctx ...interface{})

func (DiscardLogger) Info added in v0.6.0

func (DiscardLogger) Info(msg string, ctx ...interface{})

func (DiscardLogger) New added in v0.6.0

func (d DiscardLogger) New(ctx ...interface{}) Logger

type Level added in v0.5.0

type Level struct {
	// contains filtered or unexported fields
}

Level allows to interact with the logging level at runtime.

var ConsoleLevel Level

ConsoleLevel allows interacting with the logging level at runtime. It is initialized with after a successful call to Setup.

func (Level) ServeHTTP added in v0.6.0

func (l Level) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP is an endpoint that can report on or change the current logging level.

GET requests return a JSON description of the current logging level. PUT requests change the logging level and expect a payload like:

{"level":"info"}

type Logger

type Logger interface {
	New(ctx ...interface{}) Logger
	Debug(msg string, ctx ...interface{})
	Info(msg string, ctx ...interface{})
	Error(msg string, ctx ...interface{})
}

Logger describes the logger interface.

func FromCtx added in v0.4.0

func FromCtx(ctx context.Context) Logger

FromCtx returns the logger embedded in ctx if one exists, or the root logger otherwise. FromCtx is guaranteed to never return nil.

func New

func New(ctx ...interface{}) Logger

New creates a logger with the given context.

func Root

func Root() Logger

Root returns the root logger. It's a logger without any context.

func SafeNewLogger added in v0.6.0

func SafeNewLogger(l Logger, fields ...interface{}) Logger

SafeNewLogger creates a new logger as a child of l only if l is not nil. If l is nil, then nil is returned.

type Span added in v0.5.0

type Span struct {
	Logger Logger
	Span   opentracing.Span
}

Span is a logger that attaches all logs to the span.

func (Span) Debug added in v0.5.0

func (s Span) Debug(msg string, ctx ...interface{})

Debug logs to the logger and span.

func (Span) Error added in v0.5.0

func (s Span) Error(msg string, ctx ...interface{})

Error logs to the logger and span.

func (Span) Info added in v0.5.0

func (s Span) Info(msg string, ctx ...interface{})

Info logs to the logger and span.

func (Span) New added in v0.6.0

func (s Span) New(ctx ...interface{}) Logger

New creates a new logger with the context attached.

Directories

Path Synopsis
Package mock_log is a generated GoMock package.
Package mock_log is a generated GoMock package.

Jump to

Keyboard shortcuts

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