File tree 2 files changed +57
-9
lines changed
2 files changed +57
-9
lines changed Original file line number Diff line number Diff line change
1
+ package logrus
2
+
3
+ import (
4
+ "bytes"
5
+ "sync"
6
+ )
7
+
8
+ var (
9
+ bufferPool BufferPool
10
+ )
11
+
12
+ type BufferPool interface {
13
+ Put (* bytes.Buffer )
14
+ Get () * bytes.Buffer
15
+ }
16
+
17
+ type defaultPool struct {
18
+ pool * sync.Pool
19
+ }
20
+
21
+ func (p * defaultPool ) Put (buf * bytes.Buffer ) {
22
+ p .pool .Put (buf )
23
+ }
24
+
25
+ func (p * defaultPool ) Get () * bytes.Buffer {
26
+ return p .pool .Get ().(* bytes.Buffer )
27
+ }
28
+
29
+ func getBuffer () * bytes.Buffer {
30
+ return bufferPool .Get ()
31
+ }
32
+
33
+ func putBuffer (buf * bytes.Buffer ) {
34
+ buf .Reset ()
35
+ bufferPool .Put (buf )
36
+ }
37
+
38
+ // SetBufferPool allows to replace the default logrus buffer pool
39
+ // to better meets the specific needs of an application.
40
+ func SetBufferPool (bp BufferPool ) {
41
+ bufferPool = bp
42
+ }
43
+
44
+ func init () {
45
+ SetBufferPool (& defaultPool {
46
+ pool : & sync.Pool {
47
+ New : func () interface {} {
48
+ return new (bytes.Buffer )
49
+ },
50
+ },
51
+ })
52
+ }
Original file line number Diff line number Diff line change @@ -13,7 +13,6 @@ import (
13
13
)
14
14
15
15
var (
16
- bufferPool * sync.Pool
17
16
18
17
// qualified package name, cached at first use
19
18
logrusPackage string
@@ -31,12 +30,6 @@ const (
31
30
)
32
31
33
32
func init () {
34
- bufferPool = & sync.Pool {
35
- New : func () interface {} {
36
- return new (bytes.Buffer )
37
- },
38
- }
39
-
40
33
// start at the bottom of the stack before the package-name cache is primed
41
34
minimumCallerDepth = 1
42
35
}
@@ -243,9 +236,12 @@ func (entry Entry) log(level Level, msg string) {
243
236
244
237
entry .fireHooks ()
245
238
246
- buffer = bufferPool .Get ().(* bytes.Buffer )
239
+ buffer = getBuffer ()
240
+ defer func () {
241
+ entry .Buffer = nil
242
+ putBuffer (buffer )
243
+ }()
247
244
buffer .Reset ()
248
- defer bufferPool .Put (buffer )
249
245
entry .Buffer = buffer
250
246
251
247
entry .write ()
You can’t perform that action at this time.
0 commit comments