This repository has been archived by the owner on May 13, 2022. It is now read-only.
/
channel_logger.go
147 lines (129 loc) · 4.24 KB
/
channel_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
141
142
143
144
145
146
147
// Copyright Monax Industries Limited
// SPDX-License-Identifier: Apache-2.0
package loggers
import (
"sync"
"github.com/eapache/channels"
"github.com/go-kit/kit/log"
"github.com/hyperledger/burrow/logging/errors"
)
const (
DefaultLoggingRingBufferCap channels.BufferCap = 100
)
type ChannelLogger struct {
ch channels.Channel
sync.RWMutex
}
var _ log.Logger = (*ChannelLogger)(nil)
// Creates a Logger that uses a uses a non-blocking ring buffered channel.
// This logger provides a common abstraction for both a buffered, flushable
// logging cache. And a non-blocking conduit to transmit logs via
// DrainForever (or NonBlockingLogger).
func NewChannelLogger(loggingRingBufferCap channels.BufferCap) *ChannelLogger {
return &ChannelLogger{
ch: channels.NewRingChannel(loggingRingBufferCap),
}
}
func (cl *ChannelLogger) Log(keyvals ...interface{}) error {
// In case channel is being reset
cl.RLock()
defer cl.RUnlock()
cl.ch.In() <- keyvals
// We don't have a way to pass on any logging errors, but that's okay: Log is
// a maximal interface and the error return type is only there for special
// cases.
return nil
}
// Get the current occupancy level of the ring buffer
func (cl *ChannelLogger) BufferLength() int {
return cl.ch.Len()
}
// Get the cap off the internal ring buffer
func (cl *ChannelLogger) BufferCap() channels.BufferCap {
return cl.ch.Cap()
}
// Read a log line by waiting until one is available and returning it
func (cl *ChannelLogger) WaitReadLogLine() []interface{} {
logLine, ok := <-cl.ch.Out()
return readLogLine(logLine, ok)
}
// Tries to read a log line from the channel buffer or returns nil if none is
// immediately available
func (cl *ChannelLogger) ReadLogLine() []interface{} {
select {
case logLine, ok := <-cl.ch.Out():
return readLogLine(logLine, ok)
default:
return nil
}
}
func readLogLine(logLine interface{}, ok bool) []interface{} {
if !ok {
// Channel closed
return nil
}
// We are passing slices of interfaces down this channel (go-kit log's Log
// interface type), a panic is the right thing to do if this type assertion
// fails.
return logLine.([]interface{})
}
// Enters an infinite loop that will drain any log lines from the passed logger.
// You may pass in a channel
//
// Exits if the channel is closed.
func (cl *ChannelLogger) DrainForever(logger log.Logger, errCh channels.Channel) {
// logLine could be nil if channel was closed while waiting for next line
for logLine := cl.WaitReadLogLine(); logLine != nil; logLine = cl.WaitReadLogLine() {
err := logger.Log(logLine...)
if err != nil && errCh != nil {
errCh.In() <- err
}
}
}
// Drains everything that is available at the time of calling
func (cl *ChannelLogger) Flush(logger log.Logger) error {
// Grab the buffer at the here rather than within loop condition so that we
// do not drain the buffer forever
cl.Lock()
defer cl.Unlock()
bufferLength := cl.BufferLength()
var errs []error
for i := 0; i < bufferLength; i++ {
logLine := cl.WaitReadLogLine()
if logLine != nil {
err := logger.Log(logLine...)
if err != nil {
errs = append(errs, err)
}
}
}
return errors.CombineErrors(errs)
}
// Drains the next contiguous segment of loglines up to the buffer cap waiting
// for at least one line
func (cl *ChannelLogger) FlushLogLines() [][]interface{} {
logLines := make([][]interface{}, 0, cl.ch.Len())
cl.Flush(log.LoggerFunc(func(keyvals ...interface{}) error {
logLines = append(logLines, keyvals)
return nil
}))
return logLines
}
// Close the existing channel halting goroutines that are draining the channel
// and create a new channel to buffer into. Should not cause any log lines
// arriving concurrently to be lost, but any that have not been drained from
// old channel may be.
func (cl *ChannelLogger) Reset() {
cl.RWMutex.Lock()
defer cl.RWMutex.Unlock()
cl.ch.Close()
cl.ch = channels.NewRingChannel(cl.ch.Cap())
}
// Returns a Logger that wraps the outputLogger passed and does not block on
// calls to Log and a channel of any errors from the underlying logger
func NonBlockingLogger(outputLogger log.Logger) (*ChannelLogger, channels.Channel) {
cl := NewChannelLogger(DefaultLoggingRingBufferCap)
errCh := channels.NewRingChannel(cl.BufferCap())
go cl.DrainForever(outputLogger, errCh)
return cl, errCh
}