simplelog/console_handler.go
sneak 47416f00ed aider: Update console logger to always display times in UTC.
# Aider chat conversation:

USER: console logger should always display times in utc

ASSISTANT: To ensure that the console logger always displays times in UTC, we need to modify the `Handle` method in `console_handler.go` to use `time.Now().UTC()` instead of `time.Now()`.

Here is the *SEARCH/REPLACE* block:

console_handler.go
```go
<<<<<<< SEARCH
	timestamp := time.Now().Format("2006-01-02T15:04:05.000Z07:00")
=======
	timestamp := time.Now().UTC().Format("2006-01-02T15:04:05.000Z07:00")
>>>>>>> REPLACE
```
2024-05-14 05:16:01 -07:00

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
}