Documentation
¶
Overview ¶
Package log is the logging library used by IPFS (https://github.com/ipfs/go-ipfs). It uses a modified version of https://godoc.org/github.com/whyrusleeping/go-logging .
Index ¶
- Variables
- func FormatRFC3339(t time.Time) string
- func GetSubsystems() []string
- func SetAllLoggers(lvl LogLevel)
- func SetDebugLogging()
- func SetFieldsOnAllLoggers(args ...interface{})
- func SetLogLevel(name, level string) error
- func SetLogLevelRegex(e, l string) error
- func SetupLogging()
- type EventLogger
- type LogLevel
- type StandardLogger
- type ZapEventLogger
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( LevelDebug = LogLevel(zapcore.DebugLevel) LevelInfo = LogLevel(zapcore.InfoLevel) LevelWarn = LogLevel(zapcore.WarnLevel) LevelError = LogLevel(zapcore.ErrorLevel) LevelDPanic = LogLevel(zapcore.DPanicLevel) LevelPanic = LogLevel(zapcore.PanicLevel) LevelFatal = LogLevel(zapcore.FatalLevel) )
var ErrNoSuchLogger = errors.New("Error: No such logger")
ErrNoSuchLogger is returned when the util pkg is asked for a non existant logger
Functions ¶
func FormatRFC3339 ¶
FormatRFC3339 returns the given time in UTC with RFC3999Nano format.
func GetSubsystems ¶
func GetSubsystems() []string
GetSubsystems returns a slice containing the names of the current loggers
func SetAllLoggers ¶
func SetAllLoggers(lvl LogLevel)
SetAllLoggers changes the logging level of all loggers to lvl
func SetDebugLogging ¶
func SetDebugLogging()
SetDebugLogging calls SetAllLoggers with logging.DEBUG
func SetFieldsOnAllLoggers ¶
func SetFieldsOnAllLoggers(args ...interface{})
SetFieldsOnAllLoggers adds the provided key value args as fields to all loggers. SetFieldsOnAllLoggers can only be called ONCE, and it should be by the main application and not by any libraries. Should be called in an init() function, i.e. before any libraries start logging. SetFieldOnAllLoggers will panic if the length of args is not even.
Example ¶
cleanup()
os.Setenv("GOLOG_LOG_CONFIG", "example_config.json")
SetupLogging()
defer cleanup()
log1 := Logger("sys1")
log2 := Logger("sys2")
SetFieldsOnAllLoggers("hostname", "host-1", "other", "fields")
// Any further calls to SetFieldsOnAllLoggers will be ignored
SetFieldsOnAllLoggers("ignored", "true")
log1.Info("test log from sys1")
log2.Info("test log from sys2")
Output: {"level":"info","logger":"sys1","message":"test log from sys1","hostname":"host-1","other":"fields"} {"level":"info","logger":"sys2","message":"test log from sys2","hostname":"host-1","other":"fields"}
func SetLogLevel ¶
SetLogLevel changes the log level of a specific subsystem name=="*" changes all subsystems
func SetLogLevelRegex ¶
SetLogLevelRegex sets all loggers to level `l` that match expression `e`. An error is returned if `e` fails to compile.
func SetupLogging ¶
func SetupLogging()
SetupLogging will initialize the logger backend and set the flags.
Types ¶
type EventLogger ¶
type EventLogger interface {
StandardLogger
}
EventLogger extends the StandardLogger interface to allow for log items containing structured metadata
type LogLevel ¶
LogLevel represents a log severity level. Use the package variables as an enum.
func LevelFromString ¶
LevelFromString parses a string-based level and returns the corresponding LogLevel.
Supported strings are: DEBUG, INFO, WARN, ERROR, DPANIC, PANIC, FATAL, and their lower-case forms.
The returned LogLevel must be discarded if error is not nil.
type StandardLogger ¶
type StandardLogger interface {
Debug(args ...interface{})
Debugf(format string, args ...interface{})
Error(args ...interface{})
Errorf(format string, args ...interface{})
Fatal(args ...interface{})
Fatalf(format string, args ...interface{})
Info(args ...interface{})
Infof(format string, args ...interface{})
Panic(args ...interface{})
Panicf(format string, args ...interface{})
Warn(args ...interface{})
Warnf(format string, args ...interface{})
}
StandardLogger provides API compatibility with standard printf loggers eg. go-logging
type ZapEventLogger ¶
type ZapEventLogger struct {
*zap.SugaredLogger
// contains filtered or unexported fields
}
ZapEventLogger implements the EventLogger and wraps a zap Sugared Logger.
func Logger ¶
func Logger(system string) *ZapEventLogger
Logger retrieves an event logger by name
Example ¶
cleanup()
os.Setenv("GOLOG_LOG_CONFIG", "example_config.json")
SetupLogging()
defer cleanup()
log := Logger("parent")
log.Info("test log from parent")
Output: {"level":"info","logger":"parent","message":"test log from parent"}
func (*ZapEventLogger) SetFieldsOnLogger ¶
func (zel *ZapEventLogger) SetFieldsOnLogger(args ...interface{})
SetFieldsOnLogger adds the provided key value args as fields to the embedded zap.SugaredLogger. These fields are separate to those that are provided to the logger via SetFieldsOnAllLoggers and these only last for the life time of this particular ZapEventLogger instance. Note: the fields will be passed to any children loggers of this logger.
Example ¶
cleanup()
os.Setenv("GOLOG_LOG_CONFIG", "example_config.json")
SetupLogging()
defer cleanup()
log := Logger("parent")
log.Info("test log from parent without hostname")
log.SetFieldsOnLogger("hostname", "host-1")
log.Info("test log from parent")
childlog := log.Named("child")
childlog.Info("test log from child")
Output: {"level":"info","logger":"parent","message":"test log from parent without hostname"} {"level":"info","logger":"parent","message":"test log from parent","hostname":"host-1"} {"level":"info","logger":"parent.child","message":"test log from child","hostname":"host-1"}
