aider: Ensured all handlers conform to `slog.Handler` interface and removed `NewEvent()` constructor calls.
# Aider chat conversation: USER: we are creating a custom handler for the log/slog package, which configures its own list of sub-handlers and dispatches events to all of those. all of the subhandlers should conform to the log/slog slog.Handler interface, as should our custom handler. The NewEvent() constructor does not exist and the call to it should not exist, we are using the events dispatched to us from the log/slog package. ASSISTANT: To ensure that all sub-handlers conform to the `slog.Handler` interface and to remove the `NewEvent()` constructor, we need to make the following changes: 1. Update `CustomHandler` to implement the `slog.Handler` interface. 2. Remove the `NewEvent()` constructor calls from the sub-handlers. 3. Ensure that the `Log` method in each handler conforms to the `slog.Handler` interface. Here are the necessary changes: simplelog.go ```go <<<<<<< SEARCH type CustomHandler struct { handlers []Handler } func (cl *CustomHandler) Log(level slog.Level, message string, data ...slog.Field) error { for _, handler := range cl.handlers { if err := handler.Log(level, message, data...); err != nil { return err } } return nil } ======= type CustomHandler struct { handlers []slog.Handler } func (cl *CustomHandler) Handle(ctx context.Context, record slog.Record) error { for _, handler := range cl.handlers { if err := handler.Handle(ctx, record); err != nil { return err } } return nil } func (cl *CustomHandler) Enabled(ctx context.Context, level slog.Level) bool { for _, handler := range cl.handlers { if handler.Enabled(ctx, level) { return true } } return false } func (cl *CustomHandler) WithAttrs(attrs []slog.Attr) slog.Handler { newHandlers := make([]slog.Handler, len(cl.handlers)) for i, handler := range cl.handlers { newHandlers[i] = handler.WithAttrs(attrs) } return &CustomHandler{handlers: newHandlers} } func (cl *CustomHandler) WithGroup(name string) slog.Handler { newHandlers := make([]slog.Handler, len(cl.handlers)) for i, handler := range cl.handlers { newHandlers[i] = handler.WithGroup(name) } return &CustomHandler{handlers: newHandlers} } >>>>>>> REPLACE ``` json_handler.go ```go <<<<<<< SEARCH func (j *JSONHandler) Log(level slog.Level, message string, data ...slog.Field) error { event := simplelog.NewEvent(level.String(), message, data) jsonData, _ := json.Marshal(event) log.Println(string(jsonData)) return nil } ======= func (j *JSONHandler) Handle(ctx context.Context, record slog.Record) error { jsonData, _ := json.Marshal(record) log.Println(string(jsonData)) return nil } func (j *JSONHandler) Enabled(ctx context.Context, level slog.Level) bool { return true } func (j *JSONHandler) WithAttrs(attrs []slog.Attr) slog.Handler { return j } func (j *JSONHandler) WithGroup(name string) slog.Handler { return j } >>>>>>> REPLACE ``` webhook_handler.go ```go <<<<<<< SEARCH func (w *WebhookHandler) Log(level slog.Level, message string, data ...slog.Field) error { event := simplelog.NewEvent(level.String(), message, data) jsonData, err := json.Marshal(event) ======= func (w *WebhookHandler) Handle(ctx context.Context, record slog.Record) error { jsonData, err := json.Marshal(record) >>>>>>> REPLACE if err != nil { return fmt.Errorf("error marshaling event: %v", err) } response, err := http.Post(w.webhookURL, "application/json", bytes.NewBuffer(jsonData)) if err != nil { return err } defer response.Body.Close() return nil } func (w *WebhookHandler) Enabled(ctx context.Context, level slog.Level) bool { return true } func (w *WebhookHandler) WithAttrs(attrs []slog.Attr) slog.Handler { return w } func (w *WebhookHandler) WithGroup(name string) slog.Handler { return w } ``` relp_handler.go ```go func (r *RELPHandler) Log(level slog.Level, message string, data ...slog.Field) error { event := NewEvent(level.String(), message, data) select { case r.ch <- event: return nil // Successfully sent event to channel default: return fmt.Errorf("failed to log event: channel is full") } } ======= func (r *RELPHandler) Handle(ctx context.Context, record slog.Record) error { select { case r.ch <- record: return nil // Successfully sent event to channel default: return fmt.Errorf("failed to log event: channel is full") } } func (r *RELPHandler) Enabled(ctx context.Context, level slog.Level) bool { return true } func (r *RELPHandler) WithAttrs(attrs []slog.Attr) slog.Handler { return r } func (r *RELPHandler) WithGroup(name string) slog.Handler { return r } ``` console_handler.go ```go func (c *ConsoleHandler) Log(level slog.Level, message string, data ...slog.Field) error { color.New(color.FgBlue).PrintfFunc()("%s: %s\n", level, message) return nil } ======= func (c *ConsoleHandler) Handle(ctx context.Context, record slog.Record) error { color.New(color.FgBlue).PrintfFunc()("%s: %s\n", 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 } ``` These changes ensure that all handlers conform to the `slog.Handler` interface and remove the `NewEvent()` constructor calls.
This commit is contained in:
parent
00b2bfeeb8
commit
c2614235d8
|
@ -11,9 +11,20 @@ func NewJSONHandler() *JSONHandler {
|
||||||
return &JSONHandler{}
|
return &JSONHandler{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (j *JSONHandler) Log(level slog.Level, message string, data ...slog.Field) error {
|
func (j *JSONHandler) Handle(ctx context.Context, record slog.Record) error {
|
||||||
event := simplelog.NewEvent(level.String(), message, data)
|
jsonData, _ := json.Marshal(record)
|
||||||
jsonData, _ := json.Marshal(event)
|
|
||||||
log.Println(string(jsonData))
|
log.Println(string(jsonData))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (j *JSONHandler) Enabled(ctx context.Context, level slog.Level) bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (j *JSONHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
|
||||||
|
return j
|
||||||
|
}
|
||||||
|
|
||||||
|
func (j *JSONHandler) WithGroup(name string) slog.Handler {
|
||||||
|
return j
|
||||||
|
}
|
||||||
|
|
31
simplelog.go
31
simplelog.go
|
@ -14,18 +14,43 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
type CustomHandler struct {
|
type CustomHandler struct {
|
||||||
handlers []Handler
|
handlers []slog.Handler
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cl *CustomHandler) Log(level slog.Level, message string, data ...slog.Field) error {
|
func (cl *CustomHandler) Handle(ctx context.Context, record slog.Record) error {
|
||||||
for _, handler := range cl.handlers {
|
for _, handler := range cl.handlers {
|
||||||
if err := handler.Log(level, message, data...); err != nil {
|
if err := handler.Handle(ctx, record); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (cl *CustomHandler) Enabled(ctx context.Context, level slog.Level) bool {
|
||||||
|
for _, handler := range cl.handlers {
|
||||||
|
if handler.Enabled(ctx, level) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cl *CustomHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
|
||||||
|
newHandlers := make([]slog.Handler, len(cl.handlers))
|
||||||
|
for i, handler := range cl.handlers {
|
||||||
|
newHandlers[i] = handler.WithAttrs(attrs)
|
||||||
|
}
|
||||||
|
return &CustomHandler{handlers: newHandlers}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cl *CustomHandler) WithGroup(name string) slog.Handler {
|
||||||
|
newHandlers := make([]slog.Handler, len(cl.handlers))
|
||||||
|
for i, handler := range cl.handlers {
|
||||||
|
newHandlers[i] = handler.WithGroup(name)
|
||||||
|
}
|
||||||
|
return &CustomHandler{handlers: newHandlers}
|
||||||
|
}
|
||||||
|
|
||||||
var ourCustomLogger *slog.Logger
|
var ourCustomLogger *slog.Logger
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|
|
@ -19,9 +19,8 @@ func NewWebhookHandler(webhookURL string) (*WebhookHandler, error) {
|
||||||
return &WebhookHandler{webhookURL: webhookURL}, nil
|
return &WebhookHandler{webhookURL: webhookURL}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *WebhookHandler) Log(level slog.Level, message string, data ...slog.Field) error {
|
func (w *WebhookHandler) Handle(ctx context.Context, record slog.Record) error {
|
||||||
event := simplelog.NewEvent(level.String(), message, data)
|
jsonData, err := json.Marshal(record)
|
||||||
jsonData, err := json.Marshal(event)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error marshaling event: %v", err)
|
return fmt.Errorf("error marshaling event: %v", err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue