smegmesh/pkg/log/log.go

58 lines
1.0 KiB
Go
Raw 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"
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()
}
2023-10-24 01:12:38 +02:00
func NewLogrusLogger() *LogrusLogger {
logger := logrus.New()
logger.SetFormatter(&logrus.TextFormatter{FullTimestamp: true})
logger.SetOutput(os.Stdout)
logger.SetLevel(logrus.InfoLevel)
return &LogrusLogger{logger: logger}
}
2023-09-28 17:55:37 +02:00
func init() {
2023-10-24 01:12:38 +02:00
SetLogger(NewLogrusLogger())
}
func SetLogger(l Logger) {
Log = l
2023-09-28 17:55:37 +02:00
}