Documentation ¶
Overview ¶
Package logger exposes a unique interface that hides the underlying implementation and exposes useful function to switch back to a writer, or to a standard logger.
Index ¶
- Constants
- func CaptureOutput(writeFunc func()) (string, error)
- func LogAtLevelFunc(log Logger, l Level) func(...interface{})
- func LogFAtLevelFunc(log Logger, l Level) func(string, ...interface{})
- func RedirectStdLog(l Logger, at Level) func()
- func StdLog(logger Logger, at Level) *stdlog.Logger
- func WriteCloserLevel(l Logger, close func() error, at Level) io.WriteCloser
- func WriterLevel(l Logger, at Level) io.Writer
- type Config
- type InMemory
- func (n *InMemory) Debug(args ...interface{})
- func (n *InMemory) Debugf(format string, args ...interface{})
- func (n *InMemory) Error(args ...interface{})
- func (n *InMemory) Errorf(format string, args ...interface{})
- func (n *InMemory) Info(args ...interface{})
- func (n *InMemory) Infof(format string, args ...interface{})
- func (n *InMemory) Reset()
- func (n *InMemory) SetLevel(lvl Level) error
- func (n *InMemory) Warn(args ...interface{})
- func (n *InMemory) Warnf(format string, args ...interface{})
- func (n *InMemory) WithError(err error) Logger
- func (n *InMemory) WithField(key string, value interface{}) Logger
- func (n *InMemory) WithFields(fields map[string]interface{}) Logger
- type InMemoryEntry
- type Level
- type Logger
- type Noop
- func (Noop) Debug(...interface{})
- func (Noop) Debugf(string, ...interface{})
- func (Noop) Error(...interface{})
- func (Noop) Errorf(string, ...interface{})
- func (Noop) Info(...interface{})
- func (Noop) Infof(string, ...interface{})
- func (Noop) SetLevel(Level) error
- func (Noop) Warn(...interface{})
- func (Noop) Warnf(string, ...interface{})
- func (Noop) WithError(error) Logger
- func (Noop) WithField(string, interface{}) Logger
- func (Noop) WithFields(map[string]interface{}) Logger
Constants ¶
const FieldErrorKey = "error"
FieldErrorKey is the name of the field set by WithError.
Variables ¶
This section is empty.
Functions ¶
func CaptureOutput ¶
CaptureOutput catpures and merge stdout and stderr. This function is voluntary not thread-safe as it belongs to the caller to make sure there should not be two logs that are written from two different goroutines. Moreover os.Std{out,err} can't be modified from two different goroutines, because behavior in this case is undefined.
func LogAtLevelFunc ¶
LogAtLevelFunc returns a function that can log to the provided level.
func LogFAtLevelFunc ¶
LogFAtLevelFunc is the same as LogAtLevelFunc but with log formatting.
func RedirectStdLog ¶
RedirectStdLog redirects standard logger calls to the underlying logger. This is heavily inspired by zap's way of doing the same thing.
func WriteCloserLevel ¶ added in v1.2.0
func WriteCloserLevel(l Logger, close func() error, at Level) io.WriteCloser
WriteCloserLevel returns a writer that writes as if LogAtLevelFunc(l) were called, with a closer.
Types ¶
type Config ¶
type Config struct { Verbosity string `json:"verbosity" yaml:"verbosity"` Formatter string `json:"formatter" yaml:"formatter"` WithColor bool `json:"with-color" yaml:"with-color"` Output string `json:"output" yaml:"output"` }
Config defines all the configurable options for the logger.
func (*Config) SetDefault ¶
func (c *Config) SetDefault()
SetDefault set sane default for logger's config.
type InMemory ¶
type InMemory struct { Entries []InMemoryEntry // contains filtered or unexported fields }
InMemory defines a memory logger. It is designed for tests purposes only.
func NewInMemory ¶
NewInMemory returns a logger that stores log in memory.
func (*InMemory) Debug ¶
func (n *InMemory) Debug(args ...interface{})
Debug implements Logger for Memory.
func (*InMemory) Error ¶
func (n *InMemory) Error(args ...interface{})
Error implements Logger for Memory.
func (*InMemory) Info ¶
func (n *InMemory) Info(args ...interface{})
Info implements Logger for Memory.
func (*InMemory) Warn ¶
func (n *InMemory) Warn(args ...interface{})
Warn implements Logger for Memory.
func (*InMemory) WithFields ¶
WithFields implements Logger for Memory.
type InMemoryEntry ¶
type InMemoryEntry struct { Level Level Format string Args []interface{} Fields map[string]interface{} }
InMemoryEntry stores a log entry for the Memory logger.
type Level ¶
type Level int8
A Level is a logging priority. Higher levels are more important.
const ( // LevelDebug logs are typically voluminous, and are usually disabled in production. LevelDebug Level = iota // LevelInfo is the default logging priority. LevelInfo // LevelWarn logs are more important than Info, and may need individual human review. LevelWarn // LevelError logs are high-priority and should require a human review. LevelError // LevelQuiet hide everything. LevelQuiet )
func ParseLevel ¶
ParseLevel converts a string representation of a level to a level type.
type Logger ¶
type Logger interface { // Update apply the configuration on the logger. SetLevel(Level) error // Debug logs a message at the 'debug' level. Debug(args ...interface{}) Debugf(format string, args ...interface{}) // Info logs a message at the 'info' level. Info(args ...interface{}) Infof(format string, args ...interface{}) // Info logs a message at the 'warn' level. Warn(args ...interface{}) Warnf(format string, args ...interface{}) // Error logs a message at the 'error' level. Error(args ...interface{}) Errorf(format string, args ...interface{}) // WithField adds a field to the logging context. WithField(key string, value interface{}) Logger // WithFields adds multiple fields to the logging context. WithFields(fields map[string]interface{}) Logger // WithError adds an error field to the logging context. WithError(err error) Logger }
Logger defines the way logs can be handled.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package logmid is a net/http middleware that uses httpinfo and logger.Logger to log HTTP requests.
|
Package logmid is a net/http middleware that uses httpinfo and logger.Logger to log HTTP requests. |
Package logrus implements the logger.Logger interface using sirupsen/logrus implementation.
|
Package logrus implements the logger.Logger interface using sirupsen/logrus implementation. |
Package zap implements the logger.Logger interface using uber/zap implementation.
|
Package zap implements the logger.Logger interface using uber/zap implementation. |