Skip to content

Commit f44c6d5

Browse files
committed
feat: implement structured logging with new slog package and Manager struct
- Add new `slog` package - Implement `Manager` struct to handle logging - Add `New` function to create a new `Manager` instance - Implement `Infof`, `Errorf`, `Fatalf`, `Debugf` methods for formatted logging - Implement `Info`, `Error`, `Fatal`, `Debug` methods for unformatted logging Signed-off-by: appleboy <[email protected]>
1 parent 7a07f0e commit f44c6d5

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

slog/slog.go

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package slog
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log/slog"
7+
"os"
8+
)
9+
10+
// New to create new interface for logger
11+
func New() *Manager {
12+
return &Manager{
13+
logger: slog.New(slog.NewTextHandler(os.Stdout, nil)),
14+
}
15+
}
16+
17+
// Manager for slog
18+
type Manager struct {
19+
logger *slog.Logger
20+
}
21+
22+
func (l Manager) Infof(format string, args ...interface{}) {
23+
l.logger.InfoContext(context.Background(), fmt.Sprintf(format, args...))
24+
}
25+
26+
func (l Manager) Errorf(format string, args ...interface{}) {
27+
l.logger.ErrorContext(context.Background(), fmt.Sprintf(format, args...))
28+
}
29+
30+
func (l Manager) Fatalf(format string, args ...interface{}) {
31+
l.logger.ErrorContext(context.Background(), fmt.Sprintf(format, args...))
32+
os.Exit(1)
33+
}
34+
35+
func (l Manager) Debugf(format string, args ...interface{}) {
36+
l.logger.DebugContext(context.Background(), fmt.Sprintf(format, args...))
37+
}
38+
39+
func (l Manager) Info(args ...interface{}) {
40+
l.logger.InfoContext(context.Background(), fmt.Sprint(args...))
41+
}
42+
43+
func (l Manager) Error(args ...interface{}) {
44+
l.logger.ErrorContext(context.Background(), fmt.Sprint(args...))
45+
}
46+
47+
func (l Manager) Fatal(args ...interface{}) {
48+
l.logger.ErrorContext(context.Background(), fmt.Sprint(args...))
49+
os.Exit(1)
50+
}
51+
52+
func (l Manager) Debug(args ...interface{}) {
53+
l.logger.DebugContext(context.Background(), fmt.Sprint(args...))
54+
}

0 commit comments

Comments
 (0)