smegmesh/pkg/log/log.go

71 lines
1.3 KiB
Go
Raw Permalink Normal View History

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"
"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()
}
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)
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() {
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
}