Skip to content

Commit cf5ff7e

Browse files
Код с презентации | Алексей Мичурин
1 parent 141592b commit cf5ff7e

11 files changed

Lines changed: 1032 additions & 0 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package main
2+
3+
import (
4+
"log/slog"
5+
)
6+
7+
func main() {
8+
slog.Info("OK (function)", "userID", 111)
9+
10+
logger := slog.Default()
11+
logger = logger.With("data_center", "SomeDC")
12+
logger.Info("OK (method)", "userID", 222)
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
import (
4+
"log/slog"
5+
"os"
6+
)
7+
8+
func main() {
9+
handler := slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
10+
Level: slog.LevelDebug,
11+
AddSource: true,
12+
})
13+
logger := slog.New(handler)
14+
logger.Info("OK", "user", 111)
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"log/slog"
6+
"os"
7+
)
8+
9+
// -----------------------------------------------
10+
11+
type HandlerMiddlware struct {
12+
next slog.Handler
13+
}
14+
15+
func NewHandlerMiddleware(next slog.Handler) *HandlerMiddlware {
16+
return &HandlerMiddlware{next: next}
17+
}
18+
19+
func (h *HandlerMiddlware) Enabled(ctx context.Context, rec slog.Level) bool {
20+
return h.next.Enabled(ctx, rec)
21+
}
22+
23+
func (h *HandlerMiddlware) Handle(ctx context.Context, rec slog.Record) error {
24+
return h.next.Handle(ctx, rec)
25+
}
26+
27+
func (h *HandlerMiddlware) WithAttrs(attrs []slog.Attr) slog.Handler {
28+
return &HandlerMiddlware{next: h.next.WithAttrs(attrs)} // не забыть обернуть, но осторожно
29+
}
30+
31+
func (h *HandlerMiddlware) WithGroup(name string) slog.Handler {
32+
return &HandlerMiddlware{next: h.next.WithGroup(name)} // не забыть обернуть, но осторожно
33+
}
34+
35+
// -----------------------------------------------
36+
37+
func main() {
38+
var handler slog.Handler
39+
40+
handler = slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug})
41+
42+
handler = NewHandlerMiddleware(handler)
43+
44+
logger := slog.New(handler)
45+
46+
logger.Info("OK", "user", 111)
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"log/slog"
6+
"os"
7+
)
8+
9+
// -----------------------------------------------
10+
11+
type HandlerMiddlware struct {
12+
next slog.Handler
13+
}
14+
15+
func NewHandlerMiddleware(next slog.Handler) *HandlerMiddlware {
16+
return &HandlerMiddlware{next: next}
17+
}
18+
19+
func (h *HandlerMiddlware) Enabled(ctx context.Context, rec slog.Level) bool {
20+
return h.next.Enabled(ctx, rec)
21+
}
22+
23+
func (h *HandlerMiddlware) Handle(ctx context.Context, rec slog.Record) error {
24+
return h.next.Handle(ctx, rec)
25+
}
26+
27+
func (h *HandlerMiddlware) WithAttrs(attrs []slog.Attr) slog.Handler {
28+
return &HandlerMiddlware{next: h.next.WithAttrs(attrs)} // не забыть обернуть, но осторожно
29+
}
30+
31+
func (h *HandlerMiddlware) WithGroup(name string) slog.Handler {
32+
return &HandlerMiddlware{next: h.next.WithGroup(name)} // не забыть обернуть, но осторожно
33+
}
34+
35+
// -----------------------------------------------
36+
37+
func InitLogging() {
38+
handler := slog.Handler(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug}))
39+
handler = NewHandlerMiddleware(handler)
40+
slog.SetDefault(slog.New(handler))
41+
}
42+
43+
// -----------------------------------------------
44+
45+
func TransmitSMS(ctx context.Context, gate, phone, message string) error {
46+
slog.InfoContext(ctx, "Transmit SMS gateway OK", "phone", phone, "sms_gate", gate, "message", message)
47+
return nil
48+
}
49+
50+
// -----------------------------------------------
51+
52+
func ResolveGate(ctx context.Context, phone string) (string, error) {
53+
gate := "RHO"
54+
slog.InfoContext(ctx, "Resolve SMS gateway OK", "phone", phone, "sms_gate", gate)
55+
return gate, nil
56+
}
57+
58+
// -----------------------------------------------
59+
60+
func SendSMS(ctx context.Context, phone string) error {
61+
slog.InfoContext(ctx, "Sending SMS", "phone", phone)
62+
message := "Спасибо"
63+
gate, _ := ResolveGate(ctx, phone)
64+
_ = TransmitSMS(ctx, gate, phone, message)
65+
slog.InfoContext(ctx, "Send SMS OK", "phone", phone, "message", message)
66+
return nil
67+
}
68+
69+
// -----------------------------------------------
70+
71+
func GetPhoenByID(ctx context.Context, userID int) (string, error) {
72+
phone := "+78880001100"
73+
slog.InfoContext(ctx, "Get phone OK", "userID", userID, "phone", phone)
74+
return phone, nil
75+
}
76+
77+
// -----------------------------------------------
78+
79+
func Handler(ctx context.Context, userID int) {
80+
slog.InfoContext(ctx, "Handler started", "userID", userID)
81+
phone, _ := GetPhoenByID(ctx, userID)
82+
_ = SendSMS(ctx, phone)
83+
slog.InfoContext(ctx, "Handler done", "userID", userID)
84+
}
85+
86+
// -----------------------------------------------
87+
88+
func main() {
89+
InitLogging()
90+
91+
// где-то в listener-e
92+
ctx := context.Background()
93+
Handler(ctx, 111)
94+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"log/slog"
6+
"os"
7+
)
8+
9+
// -----------------------------------------------
10+
11+
type HandlerMiddlware struct {
12+
next slog.Handler
13+
}
14+
15+
func NewHandlerMiddleware(next slog.Handler) *HandlerMiddlware {
16+
return &HandlerMiddlware{next: next}
17+
}
18+
19+
func (h *HandlerMiddlware) Enabled(ctx context.Context, rec slog.Level) bool {
20+
return h.next.Enabled(ctx, rec)
21+
}
22+
23+
func (h *HandlerMiddlware) Handle(ctx context.Context, rec slog.Record) error {
24+
if c, ok := ctx.Value(key).(logCtx); ok {
25+
rec.Add("userID", c.UserID)
26+
}
27+
return h.next.Handle(ctx, rec)
28+
}
29+
30+
func (h *HandlerMiddlware) WithAttrs(attrs []slog.Attr) slog.Handler {
31+
return &HandlerMiddlware{next: h.next.WithAttrs(attrs)} // не забыть обернуть, но осторожно
32+
}
33+
34+
func (h *HandlerMiddlware) WithGroup(name string) slog.Handler {
35+
return &HandlerMiddlware{next: h.next.WithGroup(name)} // не забыть обернуть, но осторожно
36+
}
37+
38+
type logCtx struct {
39+
UserID int
40+
}
41+
42+
type keyType int
43+
44+
const key = keyType(0)
45+
46+
func WithLogUserID(ctx context.Context, userID int) context.Context {
47+
return context.WithValue(ctx, key, logCtx{UserID: userID})
48+
}
49+
50+
// -----------------------------------------------
51+
52+
func InitLogging() {
53+
handler := slog.Handler(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug}))
54+
handler = NewHandlerMiddleware(handler)
55+
slog.SetDefault(slog.New(handler))
56+
}
57+
58+
// -----------------------------------------------
59+
60+
func TransmitSMS(ctx context.Context, gate, phone, message string) error {
61+
slog.InfoContext(ctx, "Transmit SMS gateway OK", "phone", phone, "sms_gate", gate, "message", message)
62+
return nil
63+
}
64+
65+
// -----------------------------------------------
66+
67+
func ResolveGate(ctx context.Context, phone string) (string, error) {
68+
gate := "RHO"
69+
slog.InfoContext(ctx, "Resolve SMS gateway OK", "phone", phone, "sms_gate", gate)
70+
return gate, nil
71+
}
72+
73+
// -----------------------------------------------
74+
75+
func SendSMS(ctx context.Context, phone string) error {
76+
slog.InfoContext(ctx, "Sending SMS", "phone", phone)
77+
message := "Спасибо"
78+
gate, _ := ResolveGate(ctx, phone)
79+
_ = TransmitSMS(ctx, gate, phone, message)
80+
slog.InfoContext(ctx, "Send SMS OK", "phone", phone, "message", message)
81+
return nil
82+
}
83+
84+
// -----------------------------------------------
85+
86+
func GetPhoenByID(ctx context.Context, userID int) (string, error) {
87+
phone := "+78880001100"
88+
slog.InfoContext(ctx, "Get phone OK", "phone", phone)
89+
return phone, nil
90+
}
91+
92+
// -----------------------------------------------
93+
94+
func Handler(ctx context.Context, userID int) {
95+
ctx = WithLogUserID(ctx, userID)
96+
slog.InfoContext(ctx, "Handler started")
97+
phone, _ := GetPhoenByID(ctx, userID)
98+
_ = SendSMS(ctx, phone)
99+
slog.InfoContext(ctx, "Handler done")
100+
}
101+
102+
// -----------------------------------------------
103+
104+
func main() {
105+
InitLogging()
106+
107+
ctx := context.Background()
108+
Handler(ctx, 111)
109+
}

0 commit comments

Comments
 (0)