Documentation
¶
Overview ¶
Package log provides a minimal wrapper around an arbitrary third-party logging library.
The wrapper abstracts from a concrete logging implementation in order to allow painless and seamless switch-over to alternative implementations in the future. It provides the following functionality:
- Levelled logging through the Debug, Info, Warn, Error and Fatal log methods.
- Structured logging using named fields (i.e. key-value pairs) for simplified log parsing. Depending on the underlying log implementation, log entries can be written in json format, simplifying log parsing even further.
- Super-simple function signatures. All log functions take a message string and an arbitrary number of additional arguments interpreted as fields (key-value pairs) for the structured logging.
The log message should be static (i.e. it should not contain any dynamic content), so that log parsing remains simple. Any dynamic data should be added as additional fields.
Example:
// conventional plain-text log log.Infof("uploading %s for %s", filename, user) log.Warnf("upload failed %s for %s: %s", filename, user, err) // structured logging log.Info("uploading", "file", filename, "user", user) log.Warn("upload failed", "file", filename, "user", user, "error", err) // same as above: an error can be logged without specifying a key ("error" will be used as key) log.Warn("upload failed", "file", filename, "user", user, err)
Some background on structured logging: https://medium.com/@tjholowaychuk/apex-log-e8d9627f4a9a
Index ¶
- Variables
- func CloseLogFiles()
- func Debug(msg string, fields ...interface{})
- func Error(msg string, fields ...interface{})
- func Fatal(msg string, fields ...interface{})
- func Info(msg string, fields ...interface{})
- func IsDebug() bool
- func IsError() bool
- func IsFatal() bool
- func IsInfo() bool
- func IsTrace() bool
- func IsWarn() bool
- func NewLumberjackLogger(c *LumberjackConfig) *lumberjack.Logger
- func SetDefault(c *Config)
- func SetMetrics(m Metrics)
- func Trace(msg string, fields ...interface{})
- func Warn(msg string, fields ...interface{})
- type Config
- type Log
- func (l *Log) Call(f func() error, msg string, log ...func(msg string, fields ...interface{}))
- func (l *Log) Debug(msg string, fields ...interface{})
- func (l *Log) Error(msg string, fields ...interface{})
- func (l *Log) Fatal(msg string, fields ...interface{})
- func (l *Log) Handler() apex.Handler
- func (l *Log) Info(msg string, fields ...interface{})
- func (l *Log) IsDebug() bool
- func (l *Log) IsError() bool
- func (l *Log) IsFatal() bool
- func (l *Log) IsInfo() bool
- func (l *Log) IsTrace() bool
- func (l *Log) IsWarn() bool
- func (l *Log) Level() string
- func (l *Log) Name() string
- func (l *Log) SetDebug()
- func (l *Log) SetError()
- func (l *Log) SetFatal()
- func (l *Log) SetInfo()
- func (l *Log) SetLevel(level string)
- func (l *Log) SetWarn()
- func (l *Log) Trace(msg string, fields ...interface{})
- func (l *Log) Warn(msg string, fields ...interface{})
- type LumberjackConfig
- type Metrics
Constants ¶
This section is empty.
Variables ¶
var Stdout = &LumberjackConfig{}
Stdout is a LumberjackConfig with an empty Filename that leads to logging to stdout.
Functions ¶
func CloseLogFiles ¶
func CloseLogFiles()
func Debug ¶
func Debug(msg string, fields ...interface{})
Debug logs the given message at the Debug level.
func Error ¶
func Error(msg string, fields ...interface{})
Error logs the given message at the Error level.
func Fatal ¶
func Fatal(msg string, fields ...interface{})
Fatal logs the given message at the Fatal level.
func Info ¶
func Info(msg string, fields ...interface{})
Info logs the given message at the Info level.
func NewLumberjackLogger ¶
func NewLumberjackLogger(c *LumberjackConfig) *lumberjack.Logger
func SetDefault ¶
func SetDefault(c *Config)
SetDefault sets the default configuration and creates the default log based on that configuration.
Types ¶
type Config ¶
type Config struct { // Level is the log level. Default: normal Level string `json:"level"` // Handler specifies the log handler to use. Default: json Handler string `json:"formatter"` // File specifies the log file settings. Default: nil (log to stdout) File *LumberjackConfig `json:"file,omitempty"` // Include go routine ID as 'gid' in logged fields GoRoutineID *bool `json:"go_routine_id,omitempty"` // Include caller info (file:line) as 'caller' in logged fields Caller *bool `json:"caller,omitempty"` // Named contains the configuration of named loggers. // Any nested "Named" elements are ignored. Named map[string]*Config `json:"named,omitempty"` }
func NewConfig ¶
func NewConfig() *Config
NewConfig returns a new config instance, initialized with default values
func (*Config) InitDefaults ¶
type Log ¶
type Log struct {
// contains filtered or unexported fields
}
Log is the logger wrapping an apex Log instance
func Get ¶
Get returns the named logger for the given path. Loggers are organized in a hierarchy (tree) defined by their paths. Paths use a forward slash '/' as separator (e.g. /eluvio/util/json). Loggers inherit attributes of their parent loggers. A logger's path is added to every log entry as a field: logger=/eluvio/util/json
func (*Log) Call ¶
Call invokes the function and simply logs if an error occurs. Useful when deferring a call like io.Close:
defer log.Call(reader.Close, "package.Func")
Optionally provide a log function:
log.Call(reader.Close, "package.Func", mylog.Debug)
type LumberjackConfig ¶
type LumberjackConfig struct { // Filename is the file to write logs to. Backup log files will be retained // in the same directory. It uses <processname>-lumberjack.log in // os.TempDir() if empty. Filename string `json:"filename"` // MaxSize is the maximum size in megabytes of the log file before it gets // rotated. It defaults to 100 megabytes. MaxSize int `json:"maxsize"` // MaxAge is the maximum number of days to retain old log files based on the // timestamp encoded in their filename. Note that a day is defined as 24 // hours and may not exactly correspond to calendar days due to daylight // savings, leap seconds, etc. The default is not to remove old log files // based on age. MaxAge int `json:"maxage"` // MaxBackups is the maximum number of old log files to retain. The default // is to retain all old log files (though MaxAge may still cause them to get // deleted.) MaxBackups int `json:"maxbackups"` // LocalTime determines if the time used for formatting the timestamps in // backup files is the computer's local time. The default is to use UTC // time. LocalTime bool `json:"localtime"` // Compress determines if the rotated log files should be compressed // using gzip. The default is not to perform compression. Compress bool `json:"compress"` }
type Metrics ¶
type Metrics interface { // FileCreated increments the counter for created log files FileCreated() // InstanceCreated increments the counter for created log objects InstanceCreated() // Error increments the counter for messages logged with Error level Error(logger string) // Warn increments the counter for messages logged with Warn level Warn(logger string) // Info increments the counter for messages logged with Info level Info(logger string) // Debug increments the counter for messages logged with Debug level Debug(logger string) }
Metrics is the interface for collecting log metrics (counters for log calls).
Directories
¶
Path | Synopsis |
---|---|
handlers
|
|
console
Package console implements a development-friendly textual handler.
|
Package console implements a development-friendly textual handler. |
raw
Package raw is like handlers/text, but omits the "log level" field and prints the "raw" field without label on a separate line.
|
Package raw is like handlers/text, but omits the "log level" field and prints the "raw" field without label on a separate line. |
text
Package text implements a development-friendly textual handler.
|
Package text implements a development-friendly textual handler. |