forked from jkroepke/openvpn-auth-oauth2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
49 lines (39 loc) · 871 Bytes
/
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
package testutils
import (
"bytes"
"log/slog"
"sync"
)
type Logger struct {
*slog.Logger
*Buffer
}
func NewTestLogger() *Logger {
buffer := new(Buffer)
return &Logger{
slog.New(slog.NewTextHandler(buffer, nil)),
buffer,
}
}
func (l Logger) GetLogs() string {
return l.Buffer.String()
}
type Buffer struct {
buffer bytes.Buffer
mutex sync.Mutex
}
// Write appends the contents of p to the buffer, growing the buffer as needed.
// It returns the number of bytes written.
func (s *Buffer) Write(p []byte) (int, error) {
s.mutex.Lock()
defer s.mutex.Unlock()
return s.buffer.Write(p) //nolint:wrapcheck
}
// String returns the contents of the unread portion of the buffer
// as a string.
// If the Buffer is a nil pointer, it returns "<nil>".
func (s *Buffer) String() string {
s.mutex.Lock()
defer s.mutex.Unlock()
return s.buffer.String()
}