From f964a0d2e48f97cd71ba5f4bc26fcd147294272a Mon Sep 17 00:00:00 2001 From: sneak Date: Tue, 14 May 2024 04:46:28 -0700 Subject: [PATCH] aider: Update log handlers and CustomLogger to match the slog.Handler interface. # Aider chat conversation: USER: ../custom_logger.go:21:7: undefined: slog.SetDefaultLogger ASSISTANT: The error message indicates that the `slog.SetDefaultLogger` function is not defined. This could be due to the `slog` package not having a function named `SetDefaultLogger`. If you're trying to set the default logger for the `log` package, you should use `log.SetOutput` function. Here is the *SEARCH/REPLACE block* to replace `slog.SetDefaultLogger` with `log.SetOutput` in `custom_logger.go`: custom_logger.go ```go <<<<<<< SEARCH ^C KeyboardInterrupt USER: update our log handlers to match the slog.Handler interface. then update our customlogger to also match the slog.Handler interface. ASSISTANT: To update the log handlers and the CustomLogger to match the `slog.Handler` interface, we need to modify the `console_handler.go`, `json_handler.go`, `relp_handler.go`, `webhook_handler.go`, and `custom_logger.go` files. Here are the *SEARCH/REPLACE* blocks: console_handler.go ```go <<<<<<< SEARCH func (c *ConsoleHandler) Log(event Event) error { ======= func (c *ConsoleHandler) Log(level slog.Level, message string, data ...slog.Field) error { >>>>>>> REPLACE ``` json_handler.go ```go <<<<<<< SEARCH func (j *JSONHandler) Log(event Event) error { ======= func (j *JSONHandler) Log(level slog.Level, message string, data ...slog.Field) error { >>>>>>> REPLACE ``` relp_handler.go ```go <<<<<<< SEARCH func (r *RELPHandler) Log(event Event) error { ======= func (r *RELPHandler) Log(level slog.Level, message string, data ...slog.Field) error { >>>>>>> REPLACE ``` webhook_handler.go ```go <<<<<<< SEARCH func (w *WebhookHandler) Log(event Event) error { ======= func (w *WebhookHandler) Log(level slog.Level, message string, data ...slog.Field) error { >>>>>>> REPLACE ``` custom_logger.go ```go <<<<<<< SEARCH type CustomLogger struct { handlers []Handler } ======= type CustomLogger struct { handlers []slog.Handler } func (cl *CustomLogger) 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 } >>>>>>> REPLACE ``` --- console_handler.go | 2 +- custom_logger.go | 11 ++++++++++- json_handler.go | 2 +- relp_handler.go | 2 +- webhook_handler.go | 2 +- 5 files changed, 14 insertions(+), 5 deletions(-) diff --git a/console_handler.go b/console_handler.go index 359bd3d..913b54d 100644 --- a/console_handler.go +++ b/console_handler.go @@ -10,7 +10,7 @@ func NewConsoleHandler() *ConsoleHandler { return &ConsoleHandler{} } -func (c *ConsoleHandler) Log(event Event) error { +func (c *ConsoleHandler) Log(level slog.Level, message string, data ...slog.Field) error { color.New(color.FgBlue).PrintfFunc()("%s: %s\n", event.Level, event.Message) return nil } diff --git a/custom_logger.go b/custom_logger.go index f380616..9843e96 100644 --- a/custom_logger.go +++ b/custom_logger.go @@ -14,7 +14,16 @@ var ( ) type CustomLogger struct { - handlers []Handler + handlers []slog.Handler +} + +func (cl *CustomLogger) 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 } var ourCustomLogger *slog.Logger diff --git a/json_handler.go b/json_handler.go index 2d98d1a..71fd08c 100644 --- a/json_handler.go +++ b/json_handler.go @@ -11,7 +11,7 @@ func NewJSONHandler() *JSONHandler { return &JSONHandler{} } -func (j *JSONHandler) Log(event Event) error { +func (j *JSONHandler) Log(level slog.Level, message string, data ...slog.Field) error { jsonData, _ := json.Marshal(event) log.Println(string(jsonData)) return nil diff --git a/relp_handler.go b/relp_handler.go index 183686f..ab0714b 100644 --- a/relp_handler.go +++ b/relp_handler.go @@ -114,7 +114,7 @@ func (r *RELPHandler) Startup() error { return nil } -func (r *RELPHandler) Log(event Event) error { +func (r *RELPHandler) Log(level slog.Level, message string, data ...slog.Field) error { select { case r.ch <- event: return nil // Successfully sent event to channel diff --git a/webhook_handler.go b/webhook_handler.go index 107be4f..8443046 100644 --- a/webhook_handler.go +++ b/webhook_handler.go @@ -19,7 +19,7 @@ func NewWebhookHandler(webhookURL string) (*WebhookHandler, error) { return &WebhookHandler{webhookURL: webhookURL}, nil } -func (w *WebhookHandler) Log(event Event) error { +func (w *WebhookHandler) Log(level slog.Level, message string, data ...slog.Field) error { jsonData, err := json.Marshal(event) if err != nil { return fmt.Errorf("error marshaling event: %v", err)