-
Notifications
You must be signed in to change notification settings - Fork 5
/
buffer.go
152 lines (121 loc) · 3.95 KB
/
buffer.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
141
142
143
144
145
146
147
148
149
150
151
152
/*
Copyright 2017 The Nuclio Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package nucliozap
import (
"bytes"
"encoding/json"
"fmt"
"io"
"time"
"github.com/nuclio/errors"
)
var ErrBufferPoolAllocationTimeout = errors.New("Timed out waiting for buffer logger")
// BufferLogger is a logger who outputs the records to a buffer
type BufferLogger struct {
encoding string
Logger *NuclioZap
Buffer *bytes.Buffer
}
// NewBufferLogger creates a logger that is able to capture the output into a buffer. if a request arrives
// and the user wishes to capture the log, this will be used as the logger instead of the default
// logger
func NewBufferLogger(name string, encoding string, level Level) (*BufferLogger, error) {
writer := &bytes.Buffer{}
return newBufferLogger(name, encoding, level, writer, writer)
}
func NewBufferLoggerWithRedactor(name string, encoding string, level Level, redactor *Redactor) (*BufferLogger, error) {
return newBufferLogger(name, encoding, level, redactor, redactor.GetOutput().(*bytes.Buffer))
}
func newBufferLogger(name string,
encoding string,
level Level,
writer io.Writer,
buffer *bytes.Buffer) (*BufferLogger, error) {
newLogger, err := NewNuclioZap(name,
encoding,
nil,
writer,
writer,
level)
if err != nil {
return nil, errors.Wrap(err, "Failed to create buffer logger")
}
return &BufferLogger{
Logger: newLogger,
Buffer: buffer,
encoding: encoding,
}, nil
}
func (bl *BufferLogger) GetJSONString() (string, error) {
if bl.encoding != "json" {
return "", fmt.Errorf("Can only return JSON when encoding is JSON, not %s", bl.encoding)
}
jsonBody := bl.Buffer.Bytes()
if len(jsonBody) != 0 {
// remove last comma
jsonBody = jsonBody[:len(jsonBody)-1]
}
return "[" + string(jsonBody) + "]", nil
}
func (bl *BufferLogger) GetLogEntries() ([]map[string]interface{}, error) {
jsonBody, err := bl.GetJSONString()
if err != nil {
return nil, errors.Wrap(err, "Failed to get JSON string")
}
var unmarshalledJSONBody []map[string]interface{}
if err := json.Unmarshal([]byte(jsonBody), &unmarshalledJSONBody); err != nil {
return nil, errors.Wrap(err, "Failed to unmarshal JSON body")
}
return unmarshalledJSONBody, nil
}
// BufferLoggerPool is a pool for buffer loggers
type BufferLoggerPool struct {
bufferLoggerChan chan *BufferLogger
defaultAllocateTimeout time.Duration
}
func NewBufferLoggerPool(numBufferLoggers int,
name string,
encoding string,
level Level) (*BufferLoggerPool, error) {
// create a channel for the buffer loggers
bufferLoggersChan := make(chan *BufferLogger, numBufferLoggers)
// create buffer loggers
for bufferLoggerIdx := 0; bufferLoggerIdx < numBufferLoggers; bufferLoggerIdx++ {
newBufferLogger, err := NewBufferLogger(name, encoding, level)
if err != nil {
return nil, errors.Wrap(err, "Failed to create buffer logger")
}
// shove to channel
bufferLoggersChan <- newBufferLogger
}
return &BufferLoggerPool{
bufferLoggerChan: bufferLoggersChan,
defaultAllocateTimeout: 10 * time.Second,
}, nil
}
func (blp *BufferLoggerPool) Allocate(timeout *time.Duration) (*BufferLogger, error) {
if timeout == nil {
timeout = &blp.defaultAllocateTimeout
}
select {
case bufferLogger := <-blp.bufferLoggerChan:
// clear the buffer
bufferLogger.Buffer.Reset()
return bufferLogger, nil
case <-time.After(*timeout):
return nil, ErrBufferPoolAllocationTimeout
}
}
func (blp *BufferLoggerPool) Release(bufferLogger *BufferLogger) {
blp.bufferLoggerChan <- bufferLogger
}