Compare commits

..

3 Commits

Author SHA1 Message Date
Jeffrey Paul ea0c84547f add example script 2024-06-14 05:39:08 -07:00
Jeffrey Paul a852d938e7 ignore binary 2024-06-14 05:39:03 -07:00
Jeffrey Paul a660203e8f fix call stack depth bug to show log location correctly 2024-06-14 05:38:29 -07:00
3 changed files with 46 additions and 4 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
.aider* .aider*
cmd/example/example

26
cmd/example/main.go Normal file
View File

@ -0,0 +1,26 @@
package main
import (
"log/slog"
_ "sneak.berlin/go/simplelog"
)
func main() {
// log structured data with slog as usual:
slog.Info(
"User login attempt",
slog.String("user", "JohnDoe"),
slog.Int("attempt", 3),
)
slog.Warn(
"Configuration mismatch",
slog.String("expected", "config.json"),
slog.String("found", "config.dev.json"),
)
slog.Error(
"Failed to save data",
slog.String("reason", "permission denied"),
)
}

View File

@ -16,7 +16,10 @@ func NewConsoleHandler() *ConsoleHandler {
return &ConsoleHandler{} return &ConsoleHandler{}
} }
func (c *ConsoleHandler) Handle(ctx context.Context, record slog.Record) error { func (c *ConsoleHandler) Handle(
ctx context.Context,
record slog.Record,
) error {
timestamp := time.Now().UTC().Format("2006-01-02T15:04:05.000Z07:00") timestamp := time.Now().UTC().Format("2006-01-02T15:04:05.000Z07:00")
var colorFunc func(format string, a ...interface{}) string var colorFunc func(format string, a ...interface{}) string
@ -32,16 +35,28 @@ func (c *ConsoleHandler) Handle(ctx context.Context, record slog.Record) error {
} }
// Get the caller information // Get the caller information
_, file, line, ok := runtime.Caller(5) _, file, line, ok := runtime.Caller(4)
if !ok { if !ok {
file = "???" file = "???"
line = 0 line = 0
} }
fmt.Println(colorFunc("%s [%s] %s:%d: %s", timestamp, record.Level, file, line, record.Message)) fmt.Println(
colorFunc(
"%s [%s] %s:%d: %s",
timestamp,
record.Level,
file,
line,
record.Message,
),
)
return nil return nil
} }
func (c *ConsoleHandler) Enabled(ctx context.Context, level slog.Level) bool { func (c *ConsoleHandler) Enabled(
ctx context.Context,
level slog.Level,
) bool {
return true return true
} }