forked from aws/aws-lambda-runtime-interface-emulator
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlogs.go
More file actions
48 lines (41 loc) · 857 Bytes
/
logs.go
File metadata and controls
48 lines (41 loc) · 857 Bytes
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
package main
import (
"strings"
"sync"
)
type LogResponse struct {
Logs string `json:"logs"`
}
type LogCollector struct {
mutex *sync.Mutex
RuntimeLogs []string
}
func (lc *LogCollector) Write(p []byte) (n int, err error) {
lc.Put(string(p))
return len(p), nil
}
func NewLogCollector() *LogCollector {
return &LogCollector{
RuntimeLogs: []string{},
mutex: &sync.Mutex{},
}
}
func (lc *LogCollector) Put(line string) {
lc.mutex.Lock()
defer lc.mutex.Unlock()
lc.RuntimeLogs = append(lc.RuntimeLogs, line)
}
func (lc *LogCollector) reset() {
lc.mutex.Lock()
defer lc.mutex.Unlock()
lc.RuntimeLogs = []string{}
}
func (lc *LogCollector) getLogs() LogResponse {
lc.mutex.Lock()
defer lc.mutex.Unlock()
response := LogResponse{
Logs: strings.Join(lc.RuntimeLogs, ""),
}
lc.RuntimeLogs = []string{}
return response
}