-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
140 lines (109 loc) · 2.9 KB
/
logger.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package logger
import (
"io"
"github.com/nano-interactive/go-logger/serializer"
)
var (
_ io.Closer = &GenericLogger[any, *serializer.Json[any]]{}
_ Log[any] = &GenericLogger[any, *serializer.Json[any]]{}
)
type (
Error interface {
Print(string, ...any)
}
Log[T any] interface {
Log(T) error
LogMultiple([]T) error
}
GenericLogger[T any, TSerializer serializer.Interface[T]] struct {
error Error
serializer TSerializer
handle io.Writer
}
GenericLoggerPooled[T any, TSerializer serializer.PooledSerializer[T]] struct {
error Error
handle io.Writer
pool serializer.PoolInterface[T, TSerializer]
}
)
var (
_ Log[any] = &GenericLoggerPooled[any, *serializer.PoolJsonSerializer[any]]{}
_ Log[any] = &GenericLogger[any, *serializer.Json[any]]{}
)
func NewWithPooledSerializer[T any, TSerializer serializer.PooledSerializer[T]](w io.Writer, serializer serializer.PoolInterface[T, TSerializer], modifiers ...Modifier[T]) *GenericLoggerPooled[T, TSerializer] {
cfg := Config[T]{
logger: nopErrorLog,
}
for _, modifier := range modifiers {
modifier(&cfg)
}
l := &GenericLoggerPooled[T, TSerializer]{
pool: serializer,
handle: w,
}
return l
}
func New[T any, TSerializer serializer.Interface[T]](w io.Writer, serializer TSerializer, modifiers ...Modifier[T]) *GenericLogger[T, TSerializer] {
cfg := Config[T]{
logger: nopErrorLog,
}
for _, modifier := range modifiers {
modifier(&cfg)
}
l := &GenericLogger[T, TSerializer]{
serializer: serializer,
handle: w,
}
return l
}
//go:inline
func serialize[T any, TSerialize serializer.Interface[T]](handle io.Writer, errorLog Error, serializer TSerialize, data []T) error {
rawData, err := serializer.Serialize(data)
if err != nil {
if errorLog != nil {
errorLog.Print(failedToSerializeTheData, err)
}
return err
}
n, err := handle.Write(rawData)
if err != nil {
if errorLog != nil {
errorLog.Print(failedToWriteToTheFile, "", err)
}
return err
}
if n != len(rawData) {
if errorLog != nil {
errorLog.Print(notEnoughBytesWritten, n, len(rawData))
}
}
return nil
}
func (l *GenericLogger[T, TSerializer]) Log(data T) error {
many := [...]T{data}
return l.LogMultiple(many[:])
}
func (l *GenericLogger[T, TSerializer]) LogMultiple(data []T) error {
return serialize(l.handle, l.error, l.serializer, data)
}
func (l *GenericLogger[T, TSerializer]) Close() error {
if closer, ok := l.handle.(io.Closer); ok {
return closer.Close()
}
return nil
}
func (l *GenericLoggerPooled[T, TSerializer]) Log(data T) error {
many := [...]T{data}
return l.LogMultiple(many[:])
}
func (l *GenericLoggerPooled[T, TSerializer]) LogMultiple(data []T) error {
s := l.pool.Acquire()
defer l.pool.Release(s)
return serialize(l.handle, l.error, s, data)
}
func (l *GenericLoggerPooled[T, TSerializer]) Close() error {
if closer, ok := l.handle.(io.Closer); ok {
return closer.Close()
}
return nil
}