summaryrefslogtreecommitdiff
path: root/internal/utils/log.go
diff options
context:
space:
mode:
authorKyren223 <ulmliad223@gmail.com>2024-10-05 00:03:23 +0300
committerKyren223 <ulmliad223@gmail.com>2024-10-05 00:03:23 +0300
commitddb2878cc2f7c8bcc89108726ecb2dba7c861539 (patch)
treee7db305e960664770db32e40b793432b9335a0a0 /internal/utils/log.go
parenta79c02bbc3d2ca72517940f72778c6c6ef75a1be (diff)
feat: committing in case I regret this
Diffstat (limited to 'internal/utils/log.go')
-rw-r--r--internal/utils/log.go44
1 files changed, 44 insertions, 0 deletions
diff --git a/internal/utils/log.go b/internal/utils/log.go
new file mode 100644
index 0000000..3548bd3
--- /dev/null
+++ b/internal/utils/log.go
@@ -0,0 +1,44 @@
+package utils
+
+import (
+ "context"
+ "fmt"
+ "io"
+ "log"
+ "log/slog"
+ "os"
+ "time"
+)
+
+func SetupLogger(name string) {
+ logger := slog.New(CustomHandler{os.Stdout, name, nil})
+ slog.SetDefault(logger)
+ slog.SetLogLoggerLevel()
+}
+
+type CustomHandler struct {
+ writer io.Writer
+ group string
+ attrs []slog.Attr
+}
+
+func (h CustomHandler) Handle(ctx context.Context, r slog.Record) error {
+ // timestamp := time.Now().Format(time.RFC3339Nano)
+ timestamp := time.Now().Format(time.TimeOnly)
+ severity := r.Level.String()
+ _, err := fmt.Fprintf(h.writer, "[%s] [%s/%s]: %s\n", timestamp, h.group, severity, r.Message)
+ return err
+}
+
+func (h CustomHandler) Enabled(ctx context.Context, level slog.Level) bool {
+ slog.Handler
+ return true
+}
+
+func (h CustomHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
+ return &CustomHandler{h.writer, h.group, append(h.attrs, attrs...)}
+}
+
+func (h CustomHandler) WithGroup(name string) slog.Handler {
+ return &CustomHandler{h.writer, name, h.attrs}
+}