forked from sneak/simplelog
# Aider chat conversation:
USER: ../console_handler.go:34:19: record.Source undefined (type slog.Record has no field or method Source)
ASSISTANT: To fix the issue with `record.Source` being undefined, we need to update the `console_handler.go` file to remove the reference to `record.Source`. Instead, we can log the message without the source file and line number.
Here are the *SEARCH/REPLACE* blocks:
console_handler.go
```python
<<<<<<< SEARCH
// Extract the source file and line number
source := record.Source
fmt.Println(colorFunc("%s [%s] %s:%d: %s", timestamp, record.Level, source.File, source.Line, record.Message))
=======
fmt.Println(colorFunc("%s [%s] %s", timestamp, record.Level, record.Message))
>>>>>>> REPLACE
```
48 lines
1.0 KiB
Go
48 lines
1.0 KiB
Go
package simplelog
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log/slog"
|
|
"time"
|
|
|
|
"github.com/fatih/color"
|
|
)
|
|
|
|
type ConsoleHandler struct{}
|
|
|
|
func NewConsoleHandler() *ConsoleHandler {
|
|
return &ConsoleHandler{}
|
|
}
|
|
|
|
func (c *ConsoleHandler) Handle(ctx context.Context, record slog.Record) error {
|
|
timestamp := time.Now().UTC().Format("2006-01-02T15:04:05.000Z07:00")
|
|
var colorFunc func(format string, a ...interface{}) string
|
|
|
|
switch record.Level {
|
|
case slog.LevelInfo:
|
|
colorFunc = color.New(color.FgBlue).SprintfFunc()
|
|
case slog.LevelWarn:
|
|
colorFunc = color.New(color.FgYellow).SprintfFunc()
|
|
case slog.LevelError:
|
|
colorFunc = color.New(color.FgRed).SprintfFunc()
|
|
default:
|
|
colorFunc = color.New(color.FgWhite).SprintfFunc()
|
|
}
|
|
|
|
fmt.Println(colorFunc("%s [%s] %s", timestamp, record.Level, record.Message))
|
|
return nil
|
|
}
|
|
|
|
func (c *ConsoleHandler) Enabled(ctx context.Context, level slog.Level) bool {
|
|
return true
|
|
}
|
|
|
|
func (c *ConsoleHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
|
|
return c
|
|
}
|
|
|
|
func (c *ConsoleHandler) WithGroup(name string) slog.Handler {
|
|
return c
|
|
}
|