2024-05-14 11:48:28 +00:00
|
|
|
package simplelog
|
|
|
|
|
|
|
|
import (
|
2024-05-14 11:57:12 +00:00
|
|
|
"context"
|
2024-05-14 11:48:28 +00:00
|
|
|
"log"
|
|
|
|
"log/slog"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/mattn/go-isatty"
|
2024-05-14 11:57:12 +00:00
|
|
|
|
|
|
|
"github.com/mattn/go-isatty"
|
2024-05-14 11:48:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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: 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.
2024-05-14 11:54:34 +00:00
|
|
|
handlers []slog.Handler
|
2024-05-14 11:48:28 +00:00
|
|
|
}
|
|
|
|
|
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.
2024-05-14 11:54:34 +00:00
|
|
|
func (cl *CustomHandler) Handle(ctx context.Context, record slog.Record) error {
|
2024-05-14 11:48:28 +00:00
|
|
|
for _, handler := range cl.handlers {
|
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.
2024-05-14 11:54:34 +00:00
|
|
|
if err := handler.Handle(ctx, record); err != nil {
|
2024-05-14 11:48:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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.
2024-05-14 11:54:34 +00:00
|
|
|
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}
|
|
|
|
}
|
|
|
|
|
2024-05-14 11:48:28 +00:00
|
|
|
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
|
|
|
|
}
|