|
| 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