Add logger interface and stop relying on Logrus directly
This commit is contained in:
		
							
								
								
									
										17
									
								
								pkg/log/logger.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								pkg/log/logger.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,17 @@
 | 
			
		||||
// Package log provides a logger interface for logger libraries
 | 
			
		||||
// so that dex does not depend on any of them directly.
 | 
			
		||||
// It also includes a default implementation using Logrus (used by dex previously).
 | 
			
		||||
package log
 | 
			
		||||
 | 
			
		||||
// Logger serves as an adapter interface for logger libraries
 | 
			
		||||
// so that dex does not depend on any of them directly.
 | 
			
		||||
type Logger interface {
 | 
			
		||||
	WithField(key string, value interface{}) Logger
 | 
			
		||||
 | 
			
		||||
	Info(msg string)
 | 
			
		||||
	Warn(msg string)
 | 
			
		||||
 | 
			
		||||
	Debugf(format string, args ...interface{})
 | 
			
		||||
	Infof(format string, args ...interface{})
 | 
			
		||||
	Errorf(format string, args ...interface{})
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										45
									
								
								pkg/log/logrus.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								pkg/log/logrus.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,45 @@
 | 
			
		||||
package log
 | 
			
		||||
 | 
			
		||||
import "github.com/sirupsen/logrus"
 | 
			
		||||
 | 
			
		||||
// LogrusLogger is an adapter for Logrus implementing the Logger interface.
 | 
			
		||||
type LogrusLogger struct {
 | 
			
		||||
	logger logrus.FieldLogger
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// NewLogrusLogger returns a new Logger wrapping Logrus.
 | 
			
		||||
func NewLogrusLogger(logger logrus.FieldLogger) *LogrusLogger {
 | 
			
		||||
	return &LogrusLogger{
 | 
			
		||||
		logger: logger,
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// WithField adds a field to the log entry.
 | 
			
		||||
func (l *LogrusLogger) WithField(key string, value interface{}) Logger {
 | 
			
		||||
	return NewLogrusLogger(l.logger.WithField(key, value))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Info logs an Info level event.
 | 
			
		||||
func (l *LogrusLogger) Info(msg string) {
 | 
			
		||||
	l.logger.Info(msg)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Warn logs a Warn level event.
 | 
			
		||||
func (l *LogrusLogger) Warn(msg string) {
 | 
			
		||||
	l.logger.Warn(msg)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Debugf formats and logs a Debug level event.
 | 
			
		||||
func (l *LogrusLogger) Debugf(format string, args ...interface{}) {
 | 
			
		||||
	l.logger.Debugf(format, args...)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Infof formats and logs an Info level event.
 | 
			
		||||
func (l *LogrusLogger) Infof(format string, args ...interface{}) {
 | 
			
		||||
	l.logger.Infof(format, args...)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Errorf formats and logs n Error level event.
 | 
			
		||||
func (l *LogrusLogger) Errorf(format string, args ...interface{}) {
 | 
			
		||||
	l.logger.Errorf(format, args...)
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										10
									
								
								pkg/log/logrus_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								pkg/log/logrus_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,10 @@
 | 
			
		||||
package log
 | 
			
		||||
 | 
			
		||||
import "testing"
 | 
			
		||||
 | 
			
		||||
func TestLogrusLoggerImplementsLoggerInterface(t *testing.T) {
 | 
			
		||||
	var i interface{} = new(LogrusLogger)
 | 
			
		||||
	if _, ok := i.(Logger); !ok {
 | 
			
		||||
		t.Errorf("expected %T to implement Logger interface", i)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user