Documentation
¶
Overview ¶
Package log is a simple logging framework with severity levels
Index ¶
- Constants
- func Critical(v ...interface{})
- func Criticalf(format string, v ...interface{})
- func Debug(v ...interface{})
- func Debugf(format string, v ...interface{})
- func Error(v ...interface{})
- func Errorf(format string, v ...interface{})
- func Info(v ...interface{})
- func Infof(format string, v ...interface{})
- func Level() int
- func LevelName(level int) string
- func LevelSeverity(name string) int
- func LoggerLevel(name string) (int, error)
- func LoggerLevelName(name string) (string, error)
- func LoggerLevelNames() map[string]string
- func LoggerLevels() map[string]int
- func SetDefaultLogger(o Options) error
- func SetLevel(level int)
- func SetLoggerLevel(name string, level int) error
- func SetLoggerLevels(loggerLevels map[string]int) map[string]int
- func Trace(v ...interface{})
- func Tracef(format string, v ...interface{})
- func Warning(v ...interface{})
- func Warningf(format string, v ...interface{})
- type Appender
- type AppendersLogger
- type LogEntry
- type Logger
- type Options
- type StandardWriter
Examples ¶
Constants ¶
const ( // ErrEmptyLoggerName Error raised when trying to refer to a logger with an empty name ErrEmptyLoggerName = err.Error("logger name may not be empty") // ErrReinitializingExistingLogger Error raised when trying to initialize an existing logger with different options ErrReinitializingExistingLogger = err.Error("trying to initialize an already initialized logger with different options") // ErrLoggerDoesNotExist Error raised when referring to a non-existing logger ErrLoggerDoesNotExist = err.Error("logger with given name doesn't exist") // ErrUnknownLoggerType Error raised when creating a new logger of an unknown type (shouldn't happen) ErrUnknownLoggerType = err.Error("logger type is not known") )
const ( CRITICAL = iota // Critical log level (Always logged) ERROR // Error log level WARNING // Warning log level (Default level) INFO // Info log level DEBUG // Debug log level TRACE // Trace log level UNKNOWN = -1 // used as error log type for unknown levels )
Log Severity levels
const ( Ldate = iota // the date in the local time zone: 2009/01/23 Ltime // the time in the local time zone: 01:23:23 Lmicroseconds // microsecond resolution: 01:23:23.123123. assumes Ltime. LUTC // if Ldate or Ltime is set, use UTC rather than the local time zone )
constants for date format. Borrowing the same names from standard log package
const ( Time = iota Name Source LongSource Separator LogLevel )
Log message format constants to use in message header format pattern
const DEFAULT = "DEFAULT"
DEFAULT is the name of the default logger
Variables ¶
This section is empty.
Functions ¶
func Critical ¶
func Critical(v ...interface{})
Critical logs a critical log entry through the default logger
Example ¶
_ = log.SetDefaultLogger(log.Standard().WithStartingLevel(log.CRITICAL)) log.Critical("CRT") log.Error("ERR") log.Warning("WRN") log.Info("INF") log.Debug("DBG") log.Trace("TRC")
Output: CRT
func Criticalf ¶
func Criticalf(format string, v ...interface{})
Criticalf logs a formatted critical log entry through the default logger
Example ¶
_ = log.SetDefaultLogger(log.Standard().WithStartingLevel(log.CRITICAL)) log.Criticalf("%v", "CRT") log.Errorf("%v", "ERR") log.Warningf("%v", "WRN") log.Infof("%v", "INF") log.Debugf("%v", "DBG") log.Tracef("%v", "TRC")
Output: CRT
func Debug ¶
func Debug(v ...interface{})
Debug logs a debug log entry through the default logger
Example ¶
_ = log.SetDefaultLogger(log.Standard().WithStartingLevel(log.DEBUG)) log.Critical("CRT") log.Error("ERR") log.Warning("WRN") log.Info("INF") log.Debug("DBG") log.Trace("TRC")
Output: CRT ERR WRN INF DBG
func Debugf ¶
func Debugf(format string, v ...interface{})
Debugf logs a formatted debug log entry through the default logger
Example ¶
_ = log.SetDefaultLogger(log.Standard().WithStartingLevel(log.DEBUG)) log.Criticalf("%v", "CRT") log.Errorf("%v", "ERR") log.Warningf("%v", "WRN") log.Infof("%v", "INF") log.Debugf("%v", "DBG") log.Tracef("%v", "TRC")
Output: CRT ERR WRN INF DBG
func Error ¶
func Error(v ...interface{})
Error logs a error log entry through the default logger
Example ¶
_ = log.SetDefaultLogger(log.Standard().WithStartingLevel(log.ERROR)) log.Critical("CRT") log.Error("ERR") log.Warning("WRN") log.Info("INF") log.Debug("DBG") log.Trace("TRC")
Output: CRT ERR
func Errorf ¶
func Errorf(format string, v ...interface{})
Errorf logs a formatted error log entry through the default logger
Example ¶
_ = log.SetDefaultLogger(log.Standard().WithStartingLevel(log.ERROR)) log.Criticalf("%v", "CRT") log.Errorf("%v", "ERR") log.Warningf("%v", "WRN") log.Infof("%v", "INF") log.Debugf("%v", "DBG") log.Tracef("%v", "TRC")
Output: CRT ERR
func Info ¶
func Info(v ...interface{})
Info logs a info log entry through the default logger
Example ¶
_ = log.SetDefaultLogger(log.Standard().WithStartingLevel(log.INFO)) log.Critical("CRT") log.Error("ERR") log.Warning("WRN") log.Info("INF") log.Debug("DBG") log.Trace("TRC")
Output: CRT ERR WRN INF
func Infof ¶
func Infof(format string, v ...interface{})
Infof logs a formatted info log entry through the default logger
Example ¶
_ = log.SetDefaultLogger(log.Standard().WithStartingLevel(log.INFO)) log.Criticalf("%v", "CRT") log.Errorf("%v", "ERR") log.Warningf("%v", "WRN") log.Infof("%v", "INF") log.Debugf("%v", "DBG") log.Tracef("%v", "TRC")
Output: CRT ERR WRN INF
func LevelName ¶
LevelName is a convenience method to translate the log level into a name. It only works for loggers implementing the default severity scale.
func LevelSeverity ¶ added in v1.1.0
func LoggerLevel ¶ added in v1.0.1
LoggerLevel gets the current log level of the logger with the given name. ErrLoggerDoesNotExist is returned as an error if a logger with the given name doesn't is unknown.
func LoggerLevelName ¶ added in v1.0.1
LoggerLevelName gets the current log level name of the logger with the given name. ErrLoggerDoesNotExist is returned as an error if a logger with the given name doesn't is unknown. Utility method for loggers using the standard severity scale.
func LoggerLevelNames ¶ added in v1.0.1
LoggerLevelNames gets the current log level names of all known loggers. Only relevant if logger is using the standard severity scale.
func LoggerLevels ¶ added in v1.0.1
LoggerLevels gets the current log levels of all known loggers
func SetDefaultLogger ¶ added in v1.1.0
SetDefaultLogger allows overriding the default logger with different options
Example ¶
_ = log.SetDefaultLogger(log.Standard().WithLogPrefix(log.Name, log.Source, log.Separator).WithStartingLevel(log.ERROR)) log.Criticalf("%v", "CRT") log.Errorf("%v", "ERR") log.Warningf("%v", "WRN") log.Infof("%v", "INF") log.Debugf("%v", "DBG") log.Tracef("%v", "TRC")
Output: DEFAULT example_test.go:213 - CRT DEFAULT example_test.go:214 - ERR
func SetLoggerLevel ¶ added in v1.0.1
SetLoggerLevel sets the log level of a logger by name. DEFAULT may be used to set the default logger level.
func SetLoggerLevels ¶ added in v1.0.1
SetLoggerLevels sets the log levels of several loggers at once. If any logger is not found it will be omitted from the response
func Trace ¶
func Trace(v ...interface{})
Trace logs a trace log entry through the default logger
Example ¶
_ = log.SetDefaultLogger(log.Standard()) log.SetLevel(log.TRACE) log.Critical("CRT") log.Error("ERR") log.Warning("WRN") log.Info("INF") log.Debug("DBG") log.Trace("TRC")
Output: CRT ERR WRN INF DBG TRC
func Tracef ¶
func Tracef(format string, v ...interface{})
Tracef logs a formatted trace log entry through the default logger
Example ¶
_ = log.SetDefaultLogger(log.Standard().WithStartingLevel(log.TRACE)) log.Criticalf("%v", "CRT") log.Errorf("%v", "ERR") log.Warningf("%v", "WRN") log.Infof("%v", "INF") log.Debugf("%v", "DBG") log.Tracef("%v", "TRC")
Output: CRT ERR WRN INF DBG TRC
func Warning ¶
func Warning(v ...interface{})
Warning logs a warning log entry through the default logger
Example ¶
log.SetDefaultLogger(log.Standard()) log.Critical("CRT") log.Error("ERR") log.Warning("WRN") log.Info("INF") log.Debug("DBG") log.Trace("TRC")
Output: CRT ERR WRN
func Warningf ¶
func Warningf(format string, v ...interface{})
Warningf logs a formatted warning log entry through the default logger
Example ¶
log.SetDefaultLogger(log.Standard()) log.Criticalf("%v", "CRT") log.Errorf("%v", "ERR") log.Warningf("%v", "WRN") log.Infof("%v", "INF") log.Debugf("%v", "DBG") log.Tracef("%v", "TRC")
Output: CRT ERR WRN
Types ¶
type Appender ¶ added in v1.1.0
type Appender interface {
Print(logEntry *LogEntry)
}
Appender An Appender takes log entries and outputs it to whatever medium it implements
type AppendersLogger ¶ added in v1.1.0
type AppendersLogger interface { Options }
func SyncedAppenders ¶ added in v1.1.0
func SyncedAppenders() AppendersLogger
type Logger ¶
type Logger interface { // SetLevel sets the current log level of the logger SetLevel(int) // Level returns the current log level of the logger Level() int // Critical logs the message(s) at the critical level Critical(v ...interface{}) // Criticalf logs the formatted message at the critical level Criticalf(format string, v ...interface{}) // Error logs the message(s) at the error level Error(v ...interface{}) // Errorf logs the formatted message at the error level Errorf(format string, v ...interface{}) // Warning logs the message(s) at the warning level Warning(v ...interface{}) // Warningf logs the formatted message at the warning level Warningf(format string, v ...interface{}) // Info logs the message(s) at the info level Info(v ...interface{}) // Infof logs the formatted message at the info level Infof(format string, v ...interface{}) // Debug logs the message(s) at the debug level Debug(v ...interface{}) // Debugf logs the formatted message at the debug level Debugf(format string, v ...interface{}) // Trace logs the message(s) at the trace level Trace(v ...interface{}) // Tracef logs the formatted message at the trace level Tracef(format string, v ...interface{}) }
Logger defines the interface a Logger implementation must provide
func Get ¶
Get will create or get an existing logger with the given name. If the logger doesn't exist it will be created with the default options (warning level, logs to stdout and non-failing criticals). The name must be a non-empty string (may be spaces).
func GetWithOptions ¶
GetWithOptions will create a log with the provided options if it doesn't exist yet or returns an existing log if the provided options are the same as the options the existing logger was created with. Trying to get an existing logger with different options. The name logger may not be an empty string (can be filled spaces).
type Options ¶
type Options interface { // DateFlags sets the format flags for the logger DateFlags(flags int) Options // WithFailingCriticals sets the logger to fail (exit process) when logging a critical WithFailingCriticals() Options // WithoutFailingCriticals sets the logger to log criticals as plain log entries (process doesn't break) WithoutFailingCriticals() Options // WithStartingLevel sets the initial log level the logger has WithStartingLevel(startingLevel int) Options // WithLevelLogPrefix sets the log prefix format for a specific level WithLevelLogPrefix(logLevel int, flags ...uint) Options // WithLogPrefix sets the log prefix format for all levels WithLogPrefix(flags ...uint) Options }
Options represents a logger options object and methods available to configure it
type StandardWriter ¶ added in v1.1.0
func Standard ¶ added in v1.1.0
func Standard() StandardWriter
Standard creates an Options object for standard logging
Example ¶
logger, _ := log.GetWithOptions("TRC", log.Standard().WithLogPrefix(log.Name, log.Separator).WithStartingLevel(log.TRACE)) logger.Critical("CRT") logger.Error("ERR") logger.Warning("WRN") logger.Info("INF") logger.Debug("DBG") logger.Trace("TRC") logger, _ = log.GetWithOptions("DBG", log.Standard().WithLogPrefix(log.Name, log.Separator)) logger.SetLevel(log.DEBUG) logger.Critical("CRT") logger.Error("ERR") logger.Warning("WRN") logger.Info("INF") logger.Debug("DBG") logger.Trace("TRC") logger, _ = log.GetWithOptions("INF", log.Standard().WithLogPrefix(log.Name, log.Separator)) logger.SetLevel(log.INFO) logger.Critical("CRT") logger.Error("ERR") logger.Warning("WRN") logger.Info("INF") logger.Debug("DBG") logger.Trace("TRC") logger, _ = log.GetWithOptions("WRN", log.Standard().WithLogPrefix(log.Name, log.Separator)) logger.SetLevel(log.WARNING) logger.Critical("CRT") logger.Error("ERR") logger.Warning("WRN") logger.Info("INF") logger.Debug("DBG") logger.Trace("TRC")
Output: TRC - CRT TRC - ERR TRC - WRN TRC - INF TRC - DBG TRC - TRC DBG - CRT DBG - ERR DBG - WRN DBG - INF DBG - DBG INF - CRT INF - ERR INF - WRN INF - INF WRN - CRT WRN - ERR WRN - WRN