forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
66 lines (61 loc) · 1.39 KB
/
log.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
package kapacitor
import (
"bytes"
"encoding/json"
"fmt"
"log"
"strings"
"github.com/influxdata/kapacitor/pipeline"
"github.com/influxdata/wlog"
)
type LogNode struct {
node
level wlog.Level
prefix string
}
// Create a new LogNode which logs all data it receives
func newLogNode(et *ExecutingTask, n *pipeline.LogNode, l *log.Logger) (*LogNode, error) {
level, ok := wlog.StringToLevel[strings.ToUpper(n.Level)]
if !ok {
return nil, fmt.Errorf("invalid log level %s", n.Level)
}
nn := &LogNode{
node: node{Node: n, et: et, logger: l},
level: level,
prefix: n.Prefix,
}
nn.node.runF = nn.runLog
return nn, nil
}
func (s *LogNode) runLog([]byte) error {
key := fmt.Sprintf("%c! %s", wlog.ReverseLevels[s.level], s.prefix)
var buf bytes.Buffer
env := json.NewEncoder(&buf)
switch s.Wants() {
case pipeline.StreamEdge:
for p, ok := s.ins[0].NextPoint(); ok; p, ok = s.ins[0].NextPoint() {
buf.Reset()
env.Encode(p)
s.logger.Println(key, buf.String())
for _, child := range s.outs {
err := child.CollectPoint(p)
if err != nil {
return err
}
}
}
case pipeline.BatchEdge:
for b, ok := s.ins[0].NextBatch(); ok; b, ok = s.ins[0].NextBatch() {
buf.Reset()
env.Encode(b)
s.logger.Println(key, buf.String())
for _, child := range s.outs {
err := child.CollectBatch(b)
if err != nil {
return err
}
}
}
}
return nil
}