Skip to content

Commit

Permalink
leveled logger, log presentation
Browse files Browse the repository at this point in the history
  • Loading branch information
danielchalef committed Nov 14, 2023
1 parent 068d738 commit ec1d51d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
52 changes: 52 additions & 0 deletions internal/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func GetLogger() *logrus.Logger {
logger.SetFormatter(&logrus.TextFormatter{
DisableColors: false,
FullTimestamp: true,
PadLevelText: true,
})
})

Expand All @@ -31,3 +32,54 @@ func GetLogger() *logrus.Logger {
func SetLogLevel(level logrus.Level) {
logger.SetLevel(level)
}

// LeveledLogger is an interface that wraps the logrus Logger interface
type LeveledLogger interface {
Error(msg string, keysAndValues ...interface{})
Info(msg string, keysAndValues ...interface{})
Debug(msg string, keysAndValues ...interface{})
Warn(msg string, keysAndValues ...interface{})
}

var _ LeveledLogger = &LeveledLogrus{}

// NewLeveledLogrus returns a new LeveledLogrus instance. This is a wrapper
// around logrus.Logger that implements the LeveledLogger interface.
// We use this for the retyrablehttp client.
func NewLeveledLogrus(logger *logrus.Logger) *LeveledLogrus {
return &LeveledLogrus{
Logger: logger,
}
}

type LeveledLogrus struct {
*logrus.Logger
}

func (l *LeveledLogrus) fields(keysAndValues ...interface{}) map[string]interface{} {
fields := make(map[string]interface{})

for i := 0; i < len(keysAndValues)-1; i += 2 {
if key, ok := keysAndValues[i].(string); ok {
fields[key] = keysAndValues[i+1]
}
}

return fields
}

func (l *LeveledLogrus) Error(msg string, keysAndValues ...interface{}) {
l.WithFields(l.fields(keysAndValues...)).Error(msg)
}

func (l *LeveledLogrus) Info(msg string, keysAndValues ...interface{}) {
l.WithFields(l.fields(keysAndValues...)).Info(msg)
}

func (l *LeveledLogrus) Warn(msg string, keysAndValues ...interface{}) {
l.WithFields(l.fields(keysAndValues...)).Warn(msg)
}

func (l *LeveledLogrus) Debug(msg string, keysAndValues ...interface{}) {
l.WithFields(l.fields(keysAndValues...)).Debug(msg)
}
4 changes: 3 additions & 1 deletion pkg/llms/llm_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,12 @@ func Float64ToFloat32Matrix(in [][]float64) [][]float32 {
}

func NewRetryableHTTPClient(retryMax int, timeout time.Duration) *http.Client {
leveledLogger := internal.NewLeveledLogrus(log)

client := retryablehttp.NewClient()
client.RetryMax = retryMax
client.HTTPClient.Timeout = timeout
client.Logger = log
client.Logger = leveledLogger
client.Backoff = retryablehttp.DefaultBackoff
client.CheckRetry = retryPolicy

Expand Down

0 comments on commit ec1d51d

Please sign in to comment.