2021-02-02 12:05:47 +01:00
|
|
|
package model
|
|
|
|
|
2022-01-03 13:53:23 +01:00
|
|
|
//
|
|
|
|
// Logger
|
|
|
|
//
|
|
|
|
|
|
|
|
// DebugLogger is a logger emitting only debug messages.
|
|
|
|
type DebugLogger interface {
|
2021-02-02 12:05:47 +01:00
|
|
|
// Debug emits a debug message.
|
|
|
|
Debug(msg string)
|
|
|
|
|
|
|
|
// Debugf formats and emits a debug message.
|
|
|
|
Debugf(format string, v ...interface{})
|
2022-01-03 13:53:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// InfoLogger is a logger emitting debug and infor messages.
|
|
|
|
type InfoLogger interface {
|
|
|
|
// An InfoLogger is also a DebugLogger.
|
|
|
|
DebugLogger
|
2021-02-02 12:05:47 +01:00
|
|
|
|
|
|
|
// Info emits an informational message.
|
|
|
|
Info(msg string)
|
|
|
|
|
2021-06-22 14:43:58 +02:00
|
|
|
// Infof formats and emits an informational message.
|
2021-02-02 12:05:47 +01:00
|
|
|
Infof(format string, v ...interface{})
|
2022-01-03 13:53:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Logger defines the common interface that a logger should have. It is
|
|
|
|
// out of the box compatible with `log.Log` in `apex/log`.
|
|
|
|
type Logger interface {
|
|
|
|
// A Logger is also an InfoLogger.
|
|
|
|
InfoLogger
|
2021-02-02 12:05:47 +01:00
|
|
|
|
|
|
|
// Warn emits a warning message.
|
|
|
|
Warn(msg string)
|
|
|
|
|
|
|
|
// Warnf formats and emits a warning message.
|
|
|
|
Warnf(format string, v ...interface{})
|
|
|
|
}
|
|
|
|
|
2022-01-03 13:53:23 +01:00
|
|
|
// DiscardLogger is the default logger that discards its input
|
2021-02-02 12:05:47 +01:00
|
|
|
var DiscardLogger Logger = logDiscarder{}
|
|
|
|
|
2022-01-03 13:53:23 +01:00
|
|
|
// logDiscarder is a logger that discards its input
|
2021-02-02 12:05:47 +01:00
|
|
|
type logDiscarder struct{}
|
|
|
|
|
2022-01-03 13:53:23 +01:00
|
|
|
// Debug implements DebugLogger.Debug
|
2021-02-02 12:05:47 +01:00
|
|
|
func (logDiscarder) Debug(msg string) {}
|
|
|
|
|
2022-01-03 13:53:23 +01:00
|
|
|
// Debugf implements DebugLogger.Debugf
|
2021-02-02 12:05:47 +01:00
|
|
|
func (logDiscarder) Debugf(format string, v ...interface{}) {}
|
|
|
|
|
2022-01-03 13:53:23 +01:00
|
|
|
// Info implements InfoLogger.Info
|
2021-02-02 12:05:47 +01:00
|
|
|
func (logDiscarder) Info(msg string) {}
|
|
|
|
|
2022-01-03 13:53:23 +01:00
|
|
|
// Infov implements InfoLogger.Infov
|
2021-02-02 12:05:47 +01:00
|
|
|
func (logDiscarder) Infof(format string, v ...interface{}) {}
|
|
|
|
|
2022-01-03 13:53:23 +01:00
|
|
|
// Warn implements Logger.Warn
|
2021-02-02 12:05:47 +01:00
|
|
|
func (logDiscarder) Warn(msg string) {}
|
|
|
|
|
2022-01-03 13:53:23 +01:00
|
|
|
// Warnf implements Logger.Warnf
|
2021-02-02 12:05:47 +01:00
|
|
|
func (logDiscarder) Warnf(format string, v ...interface{}) {}
|
2022-02-16 20:47:44 +01:00
|
|
|
|
|
|
|
// ErrorToStringOrOK emits "ok" on "<nil>"" values for success.
|
|
|
|
func ErrorToStringOrOK(err error) string {
|
|
|
|
if err != nil {
|
|
|
|
return err.Error()
|
|
|
|
}
|
|
|
|
return "ok"
|
|
|
|
}
|
2022-06-02 13:50:34 +02:00
|
|
|
|
|
|
|
// ValidLoggerOrDefault is a factory that either returns the logger
|
|
|
|
// provided as argument, if not nil, or DiscardLogger.
|
|
|
|
func ValidLoggerOrDefault(logger Logger) Logger {
|
|
|
|
if logger != nil {
|
|
|
|
return logger
|
|
|
|
}
|
|
|
|
return DiscardLogger
|
|
|
|
}
|