2018-05-04 00:07:41 +00:00
|
|
|
package logger
|
|
|
|
|
|
|
|
import (
|
2020-01-17 04:21:58 +00:00
|
|
|
"errors"
|
2018-05-04 00:07:41 +00:00
|
|
|
"io"
|
2018-10-06 20:47:31 +00:00
|
|
|
"os"
|
2018-05-04 00:07:41 +00:00
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Logger is the main logger that is abstracted in this package.
|
|
|
|
// It is exported here for use with gorm.
|
|
|
|
var Logger *logrus.Logger
|
|
|
|
|
2020-01-17 04:21:58 +00:00
|
|
|
// ErrInvalidLevel is returned when an invalid log level is given in the config
|
|
|
|
var ErrInvalidLevel = errors.New("invalid log level")
|
|
|
|
|
|
|
|
// Config represents configuration details for logging.
|
|
|
|
type Config struct {
|
|
|
|
Filename string `json:"filename"`
|
|
|
|
Level string `json:"level"`
|
|
|
|
}
|
|
|
|
|
2018-05-04 00:07:41 +00:00
|
|
|
func init() {
|
|
|
|
Logger = logrus.New()
|
|
|
|
Logger.Formatter = &logrus.TextFormatter{DisableColors: true}
|
|
|
|
}
|
|
|
|
|
2018-10-06 20:47:31 +00:00
|
|
|
// Setup configures the logger based on options in the config.json.
|
2020-01-17 04:21:58 +00:00
|
|
|
func Setup(config *Config) error {
|
|
|
|
var err error
|
|
|
|
// Set up logging level
|
|
|
|
level := logrus.InfoLevel
|
|
|
|
if config.Level != "" {
|
|
|
|
level, err = logrus.ParseLevel(config.Level)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Logger.SetLevel(level)
|
2018-10-06 20:47:31 +00:00
|
|
|
// Set up logging to a file if specified in the config
|
2020-01-17 04:21:58 +00:00
|
|
|
logFile := config.Filename
|
2018-10-06 20:47:31 +00:00
|
|
|
if logFile != "" {
|
|
|
|
f, err := os.OpenFile(logFile, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
mw := io.MultiWriter(os.Stderr, f)
|
|
|
|
Logger.Out = mw
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-16 03:38:51 +00:00
|
|
|
// Debug logs a debug message
|
2018-05-04 00:07:41 +00:00
|
|
|
func Debug(args ...interface{}) {
|
|
|
|
Logger.Debug(args...)
|
|
|
|
}
|
|
|
|
|
2018-12-16 03:38:51 +00:00
|
|
|
// Debugf logs a formatted debug messsage
|
2018-05-04 00:07:41 +00:00
|
|
|
func Debugf(format string, args ...interface{}) {
|
|
|
|
Logger.Debugf(format, args...)
|
|
|
|
}
|
|
|
|
|
2018-12-16 03:38:51 +00:00
|
|
|
// Info logs an informational message
|
2018-05-04 00:07:41 +00:00
|
|
|
func Info(args ...interface{}) {
|
|
|
|
Logger.Info(args...)
|
|
|
|
}
|
|
|
|
|
2018-12-16 03:38:51 +00:00
|
|
|
// Infof logs a formatted informational message
|
2018-05-04 00:07:41 +00:00
|
|
|
func Infof(format string, args ...interface{}) {
|
|
|
|
Logger.Infof(format, args...)
|
|
|
|
}
|
|
|
|
|
2018-12-16 03:38:51 +00:00
|
|
|
// Error logs an error message
|
2018-05-04 00:07:41 +00:00
|
|
|
func Error(args ...interface{}) {
|
|
|
|
Logger.Error(args...)
|
|
|
|
}
|
|
|
|
|
2018-12-16 03:38:51 +00:00
|
|
|
// Errorf logs a formatted error message
|
2018-05-04 00:07:41 +00:00
|
|
|
func Errorf(format string, args ...interface{}) {
|
|
|
|
Logger.Errorf(format, args...)
|
|
|
|
}
|
|
|
|
|
2018-12-16 03:38:51 +00:00
|
|
|
// Warn logs a warning message
|
2018-05-04 00:07:41 +00:00
|
|
|
func Warn(args ...interface{}) {
|
|
|
|
Logger.Warn(args...)
|
|
|
|
}
|
|
|
|
|
2018-12-16 03:38:51 +00:00
|
|
|
// Warnf logs a formatted warning message
|
2018-05-04 00:07:41 +00:00
|
|
|
func Warnf(format string, args ...interface{}) {
|
|
|
|
Logger.Warnf(format, args...)
|
|
|
|
}
|
|
|
|
|
2018-12-16 03:38:51 +00:00
|
|
|
// Fatal logs a fatal error message
|
2018-05-04 00:07:41 +00:00
|
|
|
func Fatal(args ...interface{}) {
|
|
|
|
Logger.Fatal(args...)
|
|
|
|
}
|
|
|
|
|
2018-12-16 03:38:51 +00:00
|
|
|
// Fatalf logs a formatted fatal error message
|
2018-05-04 00:07:41 +00:00
|
|
|
func Fatalf(format string, args ...interface{}) {
|
|
|
|
Logger.Fatalf(format, args...)
|
|
|
|
}
|
|
|
|
|
2018-12-16 03:38:51 +00:00
|
|
|
// WithFields returns a new log enty with the provided fields
|
2018-05-04 00:07:41 +00:00
|
|
|
func WithFields(fields logrus.Fields) *logrus.Entry {
|
|
|
|
return Logger.WithFields(fields)
|
|
|
|
}
|
|
|
|
|
2018-12-16 03:38:51 +00:00
|
|
|
// Writer returns the current logging writer
|
2018-05-04 00:07:41 +00:00
|
|
|
func Writer() *io.PipeWriter {
|
|
|
|
return Logger.Writer()
|
|
|
|
}
|