diff --git a/console_handler.go b/console_handler.go index 3d8e26a..dd8bcab 100644 --- a/console_handler.go +++ b/console_handler.go @@ -2,7 +2,9 @@ package simplelog import ( "context" + "fmt" "log/slog" + "time" "github.com/fatih/color" ) @@ -14,7 +16,21 @@ func NewConsoleHandler() *ConsoleHandler { } func (c *ConsoleHandler) Handle(ctx context.Context, record slog.Record) error { - color.New(color.FgBlue).PrintfFunc()("%s: %s\n", record.Level, record.Message) + timestamp := time.Now().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 }