Documentation ¶
Overview ¶
Package log contains NullLogger, StdLogger and the Logger interface.
Logging ¶
Interface Logger is used all over the place and there are no other dependencies. Default Logger is a null logger. You must take care to implement a logger which is also thread safe.
Convention: Because recording a human-meaningful message is common and good practice, the first argument to every logging method is the value to the *implicit* key 'msg'. You may supply any additional context as a set of key/value pairs to the logging function.
To initialize your own logger you must somewhere set the logging object to the util/log package.
import "github.com/corestoreio/csfw/utils/log" func init() { log.Set(NewMyCustomLogger()) }
Level guards exists to avoid the cost of building arguments. Get in the habit of using guards.
import "github.com/corestoreio/csfw/utils/log" if log.IsDebug() { log.Debug("some message", "key1", expensive()) }
Standardizes on key-value pair argument sequence:
import "github.com/corestoreio/csfw/utils/log" log.Debug("message from inside Fn()", "key1", value1, "key2", value2) // instead of this log.WithFields(logrus.Fields{"m": "pkg", "key1": value1, "key2": value2}).Debug("inside fn()")
Please consider the key-value pairs when implementing your own logger.
Recommended Loggers are https://github.com/mgutz/logxi and https://github.com/Sirupsen/logrus and https://github.com/inconshreveable/log15
Standard Logger ¶
CoreStore provides a leveled logger based on Go's standard library without any dependencies. This StdLogger obeys to the interface Logger of this package.
import "github.com/corestoreio/csfw/utils/log" func init() { log.Set(log.NewStdLogger()) }
log.NewStdLogger() accepts a wide range of optional arguments. Please see the functions Std*Option().
Index ¶
- Constants
- Variables
- func Debug(msg string, args ...interface{})
- func Error(msg string, args ...interface{}) error
- func Fatal(msg string, args ...interface{})
- func Info(msg string, args ...interface{})
- func IsDebug() bool
- func IsInfo() bool
- func IsTrace() bool
- func IsWarn() bool
- func Log(level int, msg string, args []interface{})
- func Set(l Logger)
- func SetLevel(l int)
- func SetNull()
- func Trace(msg string, args ...interface{})
- func Warn(msg string, args ...interface{})
- type Logger
- type NullLogger
- func (l *NullLogger) Debug(msg string, args ...interface{})
- func (l *NullLogger) Error(msg string, args ...interface{}) error
- func (l *NullLogger) Fatal(msg string, args ...interface{})
- func (l *NullLogger) Info(msg string, args ...interface{})
- func (l *NullLogger) IsDebug() bool
- func (l *NullLogger) IsInfo() bool
- func (l *NullLogger) IsTrace() bool
- func (l *NullLogger) IsWarn() bool
- func (l *NullLogger) Log(level int, msg string, args []interface{})
- func (l *NullLogger) New(ctx ...interface{}) Logger
- func (l *NullLogger) SetLevel(level int)
- func (l *NullLogger) Trace(msg string, args ...interface{})
- func (l *NullLogger) Warn(msg string, args ...interface{})
- type StdLogger
- func (l *StdLogger) Debug(msg string, args ...interface{})
- func (l *StdLogger) Error(msg string, args ...interface{}) error
- func (l *StdLogger) Fatal(msg string, args ...interface{})
- func (l *StdLogger) Info(msg string, args ...interface{})
- func (l *StdLogger) IsDebug() bool
- func (l *StdLogger) IsInfo() bool
- func (l *StdLogger) IsTrace() bool
- func (l *StdLogger) IsWarn() bool
- func (l *StdLogger) Log(level int, msg string, args []interface{})
- func (l *StdLogger) New(iOpts ...interface{}) Logger
- func (l *StdLogger) SetLevel(level int)
- func (l *StdLogger) Trace(msg string, args ...interface{})
- func (l *StdLogger) Warn(msg string, args ...interface{})
- type StdOption
- func StdDebugOption(out io.Writer, prefix string, flag int) StdOption
- func StdErrorOption(out io.Writer, prefix string, flag int) StdOption
- func StdFatalOption(out io.Writer, prefix string, flag int) StdOption
- func StdGlobalFlagOption(f int) StdOption
- func StdGlobalWriterOption(w io.Writer) StdOption
- func StdInfoOption(out io.Writer, prefix string, flag int) StdOption
- func StdLevelOption(level int) StdOption
- func StdTraceOption(out io.Writer, prefix string, flag int) StdOption
- func StdWarnOption(out io.Writer, prefix string, flag int) StdOption
Constants ¶
const ( StdLevelFatal int = iota + 1 StdLevelError StdLevelWarn StdLevelInfo StdLevelDebug StdLevelTrace )
Variables ¶
var AssignmentChar = ": "
The assignment character between key-value pairs
var (
ErrLoggerSet = errors.New("Logger already initialized")
)
var Separator = " "
Separator is the separator to use between key value pairs
Functions ¶
Types ¶
type Logger ¶
type Logger interface { // New returns a new Logger that has this logger's context plus the given context New(ctx ...interface{}) Logger Trace(msg string, args ...interface{}) Debug(msg string, args ...interface{}) Info(msg string, args ...interface{}) Warn(msg string, args ...interface{}) // Error logs an error entry. Returns the first argument as an error OR // if the 2nd index of args (that is args[1] ;-) ) contains the error // then that error will be returned. Error(msg string, args ...interface{}) error Fatal(msg string, args ...interface{}) Log(level int, msg string, args []interface{}) SetLevel(int) IsTrace() bool IsDebug() bool IsInfo() bool IsWarn() bool }
Logger defines the minimum requirements for logging. See doc.go for more details. Interface may be extended ...
type NullLogger ¶
type NullLogger struct{}
NullLogger is the default logger for this package.
func (*NullLogger) Debug ¶
func (l *NullLogger) Debug(msg string, args ...interface{})
Debug logs a debug entry.
func (*NullLogger) Error ¶
func (l *NullLogger) Error(msg string, args ...interface{}) error
Error logs an error entry. Returns the first argument as an error OR if the 2nd index of args (that is args[1] ;-) ) contains the error then that error will be returned.
func (*NullLogger) Fatal ¶
func (l *NullLogger) Fatal(msg string, args ...interface{})
Fatal logs a fatal entry then panics.
func (*NullLogger) Info ¶
func (l *NullLogger) Info(msg string, args ...interface{})
Info logs an info entry.
func (*NullLogger) IsDebug ¶
func (l *NullLogger) IsDebug() bool
IsDebug determines if this logger logs a debug statement.
func (*NullLogger) IsInfo ¶
func (l *NullLogger) IsInfo() bool
IsInfo determines if this logger logs an info statement.
func (*NullLogger) IsTrace ¶
func (l *NullLogger) IsTrace() bool
IsTrace determines if this logger logs a trace statement.
func (*NullLogger) IsWarn ¶
func (l *NullLogger) IsWarn() bool
IsWarn determines if this logger logs a warning statement.
func (*NullLogger) Log ¶
func (l *NullLogger) Log(level int, msg string, args []interface{})
Log logs a leveled entry.
func (*NullLogger) New ¶
func (l *NullLogger) New(ctx ...interface{}) Logger
New returns a new Logger that has this logger's context plus the given context
func (*NullLogger) SetLevel ¶
func (l *NullLogger) SetLevel(level int)
SetLevel sets the level of this logger.
func (*NullLogger) Trace ¶
func (l *NullLogger) Trace(msg string, args ...interface{})
Trace logs a trace entry.
func (*NullLogger) Warn ¶
func (l *NullLogger) Warn(msg string, args ...interface{})
Warn logs a warn entry.
type StdLogger ¶
type StdLogger struct {
// contains filtered or unexported fields
}
StdLogger implements logging with Go's standard library
func NewStdLogger ¶
NewStdLogger creates a new logger with 6 different sub loggers. You can use option functions to modify each logger independently. Default output goes to Stderr.
func (*StdLogger) Error ¶
Error logs an error entry. Returns the first argument as an error OR if the 2nd index of args (that is args[1] ;-) ) contains the error then that error will be returned.
func (*StdLogger) New ¶
New returns a new Logger that has this logger's context plus the given context This function panics if an argument is not of type StdOption.
type StdOption ¶
type StdOption func(*StdLogger)
StdOption function to modify a logger
func StdDebugOption ¶
StdDebugOption applies options for debug logging
func StdErrorOption ¶
StdErrorOption applies options for error logging
func StdFatalOption ¶
StdFatalOption applies options for fatal logging
func StdGlobalFlagOption ¶
StdWriterOption sets the global flag for all loggers. This global flag can be overwritten by individual level options. Please see http://golang.org/pkg/log/#pkg-constants
func StdGlobalWriterOption ¶
StdGlobalWriterOption sets the global writer for all loggers. This global writer can be overwritten by individual level options.
func StdInfoOption ¶
StdInfoOption applies options for info logging
func StdLevelOption ¶
StdLevelOption sets the log level. See constants Level*
func StdTraceOption ¶
StdTraceOption applies options for trace logging