2023-10-24 01:12:38 +02:00
|
|
|
// Provides a generic interface for logging
|
2023-09-28 17:55:37 +02:00
|
|
|
package logging
|
|
|
|
|
|
|
|
import (
|
2023-11-13 11:44:14 +01:00
|
|
|
"io"
|
2023-09-28 17:55:37 +02:00
|
|
|
"os"
|
2023-10-24 01:12:38 +02:00
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
2024-01-05 13:59:13 +01:00
|
|
|
"github.com/tim-beatham/smegmesh/pkg/conf"
|
2023-09-28 17:55:37 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2023-10-24 01:12:38 +02:00
|
|
|
Log Logger
|
2023-09-28 17:55:37 +02:00
|
|
|
)
|
|
|
|
|
2023-10-24 01:12:38 +02:00
|
|
|
type Logger interface {
|
|
|
|
WriteInfof(msg string, args ...interface{})
|
|
|
|
WriteErrorf(msg string, args ...interface{})
|
|
|
|
WriteWarnf(msg string, args ...interface{})
|
2023-11-13 11:44:14 +01:00
|
|
|
Writer() io.Writer
|
2023-10-24 01:12:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type LogrusLogger struct {
|
|
|
|
logger *logrus.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *LogrusLogger) WriteInfof(msg string, args ...interface{}) {
|
|
|
|
l.logger.Infof(msg, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *LogrusLogger) WriteErrorf(msg string, args ...interface{}) {
|
|
|
|
l.logger.Errorf(msg, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *LogrusLogger) WriteWarnf(msg string, args ...interface{}) {
|
|
|
|
l.logger.Warnf(msg, args...)
|
|
|
|
}
|
|
|
|
|
2023-11-13 11:44:14 +01:00
|
|
|
func (l *LogrusLogger) Writer() io.Writer {
|
|
|
|
return l.logger.Writer()
|
|
|
|
}
|
|
|
|
|
2024-01-05 13:59:13 +01:00
|
|
|
func NewLogrusLogger(confLevel conf.LogLevel) *LogrusLogger {
|
|
|
|
|
|
|
|
var level logrus.Level
|
|
|
|
|
|
|
|
switch confLevel {
|
|
|
|
case conf.ERROR:
|
|
|
|
level = logrus.ErrorLevel
|
|
|
|
case conf.WARNING:
|
|
|
|
level = logrus.WarnLevel
|
|
|
|
case conf.INFO:
|
|
|
|
level = logrus.InfoLevel
|
|
|
|
}
|
|
|
|
|
2023-10-24 01:12:38 +02:00
|
|
|
logger := logrus.New()
|
|
|
|
logger.SetFormatter(&logrus.TextFormatter{FullTimestamp: true})
|
|
|
|
logger.SetOutput(os.Stdout)
|
2024-01-05 13:59:13 +01:00
|
|
|
logger.SetLevel(level)
|
2023-10-24 01:12:38 +02:00
|
|
|
|
|
|
|
return &LogrusLogger{logger: logger}
|
|
|
|
}
|
|
|
|
|
2023-09-28 17:55:37 +02:00
|
|
|
func init() {
|
2024-01-05 13:59:13 +01:00
|
|
|
SetLogger(NewLogrusLogger(conf.INFO))
|
2023-10-24 01:12:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func SetLogger(l Logger) {
|
|
|
|
Log = l
|
2023-09-28 17:55:37 +02:00
|
|
|
}
|