2024-05-14 11:48:28 +00:00
|
|
|
package simplelog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"log/slog"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/mattn/go-isatty"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
relpServerURL = os.Getenv("LOGGER_RELP_URL")
|
|
|
|
webhookURL = os.Getenv("LOGGER_WEBHOOK_URL")
|
|
|
|
)
|
|
|
|
|
aider: Renamed CustomLogger to CustomHandler.
# Aider chat conversation:
USER: rename CustomLogger to CustomHandler
ASSISTANT: To make this change we need to modify `simplelog.go`:
1. Replace all instances of `CustomLogger` with `CustomHandler`.
Here are the *SEARCH/REPLACE* blocks:
simplelog.go
```go
<<<<<<< SEARCH
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
}
var ourCustomLogger *slog.Logger
func init() {
ourCustomLogger = NewCustomLogger()
slog.SetDefault(ourCustomLogger)
}
func NewCustomLogger() *CustomLogger {
cl := &CustomLogger{}
=======
type CustomHandler struct {
handlers []slog.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
}
var ourCustomLogger *slog.Logger
func init() {
ourCustomLogger = NewCustomHandler()
slog.SetDefault(ourCustomLogger)
}
func NewCustomHandler() *CustomHandler {
cl := &CustomHandler{}
>>>>>>> REPLACE
```
2024-05-14 11:48:29 +00:00
|
|
|
type CustomHandler struct {
|
aider: Fixed undefined variables and types, updated handlers to use `slog.Handler` interface, and defined missing `event` variables.
# Aider chat conversation:
USER: ../console_handler.go:15:77: undefined: slog.Field
../console_handler.go:16:51: undefined: event
../json_handler.go:14:33: undefined: slog
../json_handler.go:15:30: undefined: event
../relp_handler.go:117:33: undefined: slog
../relp_handler.go:119:15: undefined: event
../simplelog.go:20:77: undefined: slog.Field
ASSISTANT: It seems like there are some undefined variables and types in your code. Let's fix them.
In `console_handler.go`, `slog.Field` and `event` are undefined. We need to replace `slog.Field` with `simplelog.Field` and define `event`.
In `json_handler.go`, `slog` and `event` are undefined. We need to replace `slog` with `simplelog` and define `event`.
In `relp_handler.go`, `slog` and `event` are undefined. We need to replace `slog` with `simplelog` and define `event`.
In `simplelog.go`, `slog.Field` is undefined. We need to replace `slog.Field` with `simplelog.Field`.
Here are the *SEARCH/REPLACE* blocks:
console_handler.go
```go
<<<<<<< SEARCH
func (c *ConsoleHandler) Log(level slog.Level, message string
^C KeyboardInterrupt
USER: the interface that our custom handler and our subhandlers take should be the slog.Handler interface from the go standard library log/slog package.
ASSISTANT: I see, you want to use the `slog.Handler` interface from the `log/slog` package.
Let's modify the `handler.go` file to import `log/slog` and use `slog.Handler` instead of `Handler`.
handler.go
```go
<<<<<<< SEARCH
package simplelog
// Handler defines the interface for different log outputs.
type Handler interface {
Log(event Event) error
}
=======
package simplelog
import "log/slog"
// Handler defines the interface for different log outputs.
type Handler interface {
slog.Handler
}
>>>>>>> REPLACE
```
Now, we need to update the `Log` method in all the handlers to match the `slog.Handler` interface.
console_handler.go
```go
<<<<<<< SEARCH
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
}
=======
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
}
>>>>>>> REPLACE
```
json_handler.go
```go
<<<<<<< SEARCH
func (j *JSONHandler) Log(level slog.Level, message string, data ...slog.Field) error {
jsonData, _ := json.Marshal(event)
log.Println(string(jsonData))
return nil
}
=======
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
}
>>>>>>> REPLACE
```
relp_handler.go
```go
<<<<<<< SEARCH
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
default:
return fmt.Errorf("failed to log event: channel is full")
}
}
=======
func (r *RELPHandler) Log(level slog.Level, message string, data ...slog.Field) error {
event := simplelog.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")
}
}
>>>>>>> REPLACE
```
webhook_handler.go
```go
<<<<<<< SEARCH
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)
}
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) Log(level slog.Level, message string, data ...slog.Field) error {
event := simplelog.NewEvent(level.String(), message, data)
jsonData, err := json.Marshal(event)
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
}
>>>>>>> REPLACE
```
simplelog.go
```go
<<<<<<< SEARCH
type CustomHandler struct {
handlers []slog.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 []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
}
>>>>>>> REPLACE
```
Please let me know if you need further changes.
2024-05-14 11:52:13 +00:00
|
|
|
handlers []Handler
|
2024-05-14 11:48:28 +00:00
|
|
|
}
|
|
|
|
|
aider: Renamed CustomLogger to CustomHandler.
# Aider chat conversation:
USER: rename CustomLogger to CustomHandler
ASSISTANT: To make this change we need to modify `simplelog.go`:
1. Replace all instances of `CustomLogger` with `CustomHandler`.
Here are the *SEARCH/REPLACE* blocks:
simplelog.go
```go
<<<<<<< SEARCH
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
}
var ourCustomLogger *slog.Logger
func init() {
ourCustomLogger = NewCustomLogger()
slog.SetDefault(ourCustomLogger)
}
func NewCustomLogger() *CustomLogger {
cl := &CustomLogger{}
=======
type CustomHandler struct {
handlers []slog.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
}
var ourCustomLogger *slog.Logger
func init() {
ourCustomLogger = NewCustomHandler()
slog.SetDefault(ourCustomLogger)
}
func NewCustomHandler() *CustomHandler {
cl := &CustomHandler{}
>>>>>>> REPLACE
```
2024-05-14 11:48:29 +00:00
|
|
|
func (cl *CustomHandler) Log(level slog.Level, message string, data ...slog.Field) error {
|
2024-05-14 11:48:28 +00:00
|
|
|
for _, handler := range cl.handlers {
|
|
|
|
if err := handler.Log(level, message, data...); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var ourCustomLogger *slog.Logger
|
|
|
|
|
|
|
|
func init() {
|
aider: Renamed CustomLogger to CustomHandler.
# Aider chat conversation:
USER: rename CustomLogger to CustomHandler
ASSISTANT: To make this change we need to modify `simplelog.go`:
1. Replace all instances of `CustomLogger` with `CustomHandler`.
Here are the *SEARCH/REPLACE* blocks:
simplelog.go
```go
<<<<<<< SEARCH
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
}
var ourCustomLogger *slog.Logger
func init() {
ourCustomLogger = NewCustomLogger()
slog.SetDefault(ourCustomLogger)
}
func NewCustomLogger() *CustomLogger {
cl := &CustomLogger{}
=======
type CustomHandler struct {
handlers []slog.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
}
var ourCustomLogger *slog.Logger
func init() {
ourCustomLogger = NewCustomHandler()
slog.SetDefault(ourCustomLogger)
}
func NewCustomHandler() *CustomHandler {
cl := &CustomHandler{}
>>>>>>> REPLACE
```
2024-05-14 11:48:29 +00:00
|
|
|
ourCustomLogger = NewCustomHandler()
|
2024-05-14 11:48:28 +00:00
|
|
|
slog.SetDefault(ourCustomLogger)
|
|
|
|
}
|
|
|
|
|
aider: Renamed CustomLogger to CustomHandler.
# Aider chat conversation:
USER: rename CustomLogger to CustomHandler
ASSISTANT: To make this change we need to modify `simplelog.go`:
1. Replace all instances of `CustomLogger` with `CustomHandler`.
Here are the *SEARCH/REPLACE* blocks:
simplelog.go
```go
<<<<<<< SEARCH
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
}
var ourCustomLogger *slog.Logger
func init() {
ourCustomLogger = NewCustomLogger()
slog.SetDefault(ourCustomLogger)
}
func NewCustomLogger() *CustomLogger {
cl := &CustomLogger{}
=======
type CustomHandler struct {
handlers []slog.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
}
var ourCustomLogger *slog.Logger
func init() {
ourCustomLogger = NewCustomHandler()
slog.SetDefault(ourCustomLogger)
}
func NewCustomHandler() *CustomHandler {
cl := &CustomHandler{}
>>>>>>> REPLACE
```
2024-05-14 11:48:29 +00:00
|
|
|
func NewCustomHandler() *CustomHandler {
|
|
|
|
cl := &CustomHandler{}
|
2024-05-14 11:48:28 +00:00
|
|
|
if isatty.IsTerminal(os.Stdout.Fd()) {
|
|
|
|
cl.handlers = append(cl.handlers, NewConsoleHandler())
|
|
|
|
} else {
|
|
|
|
cl.handlers = append(cl.handlers, NewJSONHandler())
|
|
|
|
}
|
|
|
|
if relpServerURL != "" {
|
|
|
|
handler, err := NewRELPHandler(relpServerURL)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to initialize RELP handler: %v", err)
|
|
|
|
}
|
|
|
|
cl.handlers = append(cl.handlers, handler)
|
|
|
|
}
|
|
|
|
if webhookURL != "" {
|
|
|
|
handler, err := NewWebhookHandler(webhookURL)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to initialize Webhook handler: %v", err)
|
|
|
|
}
|
|
|
|
cl.handlers = append(cl.handlers, handler)
|
|
|
|
}
|
|
|
|
return cl
|
|
|
|
}
|