-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultilogger_test.go
106 lines (92 loc) · 2.94 KB
/
multilogger_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package multiwriter
import (
"context"
"log"
"log/slog"
"os"
"testing"
colored_logger "github.com/rzaripov1990/multiwriter/ext/colored"
kafka_logger "github.com/rzaripov1990/multiwriter/ext/kafka"
kgo "github.com/segmentio/kafka-go"
)
func TestStd(t *testing.T) {
logger := slog.New(
slog.NewJSONHandler(
os.Stdout,
&slog.HandlerOptions{
AddSource: false,
Level: slog.LevelDebug,
},
),
)
logger.Debug("start", "json", "true")
}
func TestMulti(t *testing.T) {
// writer #1
file, err := os.Create("./custom-file.log") //create a new file
if err != nil {
panic(err)
}
defer file.Close()
// writer #2
kafka := kafka_logger.New(
&kgo.Writer{
Addr: kgo.TCP("127.0.0.1:9092"),
Balancer: &kgo.RoundRobin{},
RequiredAcks: kgo.RequireOne,
Async: true,
Compression: kgo.Compression(0),
AllowAutoTopicCreation: true,
Topic: "logs",
},
)
defer kafka.Close()
// writer #3
stdout := colored_logger.New(true, os.Stdout) // wrap os.Stdout for colorize
colorize := true
ignoreErrors := true
logger := slog.New(
slog.NewJSONHandler(
New(
colorize,
ignoreErrors,
stdout, // #1
file, // #2
kafka, // #3
//os.Stdout, // #4 standart
),
&slog.HandlerOptions{
AddSource: false,
Level: slog.LevelDebug,
},
),
)
var (
ctx context.Context = context.Background()
slogValues []slog.Attr = []slog.Attr{
slog.String("possession", "version"),
slog.String("song", "concept"),
slog.String("construction", "direction"),
slog.String("reading", "quantity"),
slog.String("historian", "efficiency"),
slog.String("establishment", "courage"),
}
)
logger.LogAttrs(ctx, slog.LevelDebug, "Lorem Ipsum is simply dummy text of the printing and typesetting industry.", slogValues[:2]...)
logger.LogAttrs(ctx, slog.LevelInfo, "Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book", slogValues[2:4]...)
logger.LogAttrs(ctx, slog.LevelWarn, "It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged", slogValues[4:]...)
logger.LogAttrs(ctx, slog.LevelError, "It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.")
}
func TestLog(t *testing.T) {
file, err := os.Create("./custom-file.log")
if err != nil {
panic(err)
}
defer file.Close()
mw := New(false, false, os.Stdout, file)
log.SetOutput(mw)
log.Println(`"INFO": информационное сообщение`)
log.Println(`"WARN": предупреждающее сообщение`)
log.Println(`"ERROR": ошибка`)
log.Println(`"DEBUG": сообщение для отладки`)
}