Documentation ¶
Index ¶
- Constants
- Variables
- func CtxWith(ctx context.Context, logger Logger) context.Context
- func Debug(msg string, ctx ...interface{})
- func Discard()
- func Error(msg string, ctx ...interface{})
- func Flush()
- func HandlePanic()
- func Info(msg string, ctx ...interface{})
- func SafeDebug(l Logger, msg string, fields ...interface{})
- func SafeError(l Logger, msg string, fields ...interface{})
- func SafeInfo(l Logger, msg string, fields ...interface{})
- func Setup(cfg Config, opts ...Option) error
- type Config
- type ConsoleConfig
- type DebugID
- type DiscardLogger
- type EntriesCounter
- type Level
- type Logger
- type Option
- type Span
Examples ¶
Constants ¶
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 = "none" )
Variables ¶
var ConsoleLevel httpLevel
ConsoleLevel allows interacting with the logging level at runtime. It is initialized with after a successful call to Setup.
Functions ¶
func CtxWith ¶ added in v0.4.0
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 Discard ¶ added in v0.5.0
func Discard()
Discard sets the logger up to discard all log entries. This is useful for testing.
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
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).
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"` // DisableCaller stops annotating logs with the calling function's file // name and line number. By default, all logs are annotated. DisableCaller bool `toml:"disable_caller,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.
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) Enabled ¶ added in v0.7.0
func (DiscardLogger) Enabled(lvl Level) bool
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 EntriesCounter ¶ added in v0.7.0
EntriesCounter defines the metrics that are incremented when emitting a log entry.
type Logger ¶
type Logger interface { New(ctx ...interface{}) Logger Debug(msg string, ctx ...interface{}) Info(msg string, ctx ...interface{}) Error(msg string, ctx ...interface{}) Enabled(lvl Level) bool }
Logger describes the logger interface.
func FromCtx ¶ added in v0.4.0
FromCtx returns the logger embedded in ctx if one exists, or the root logger otherwise. FromCtx is guaranteed to never return nil.
func SafeNewLogger ¶ added in v0.6.0
SafeNewLogger creates a new logger as a child of l only if l is not nil. If l is nil, then nil is returned.
func WithLabels ¶ added in v0.7.0
WithLabels returns context with additional labels added to the logger. For convenience it also returns the logger itself.
func WithOptions ¶ added in v0.7.0
WithOptions returns the logger with the options applied.
type Option ¶ added in v0.7.0
type Option func(o *options)
Option is a function that sets an option.
func AddCallerSkip ¶ added in v0.7.0
AddCallerSkip increases the number of callers skipped by caller annotation. When building wrappers around the Logger, supplying this Option prevents the Logger from always reporting the wrapper code as the caller.
func WithEntriesCounter ¶ added in v0.7.0
func WithEntriesCounter(m EntriesCounter) Option
WithEntriesCounter configures a metric counters that are incremented with every emitted log entry.
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.