-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
Copy pathhandler.go
142 lines (121 loc) · 2.75 KB
/
handler.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
package logger
import (
"container/list"
"fmt"
"io"
"log"
"os"
"strings"
"sync"
"time"
"github.com/influxdata/telegraf"
)
type entry struct {
timestamp time.Time
level telegraf.LogLevel
prefix string
attributes map[string]interface{}
args []interface{}
}
type handler struct {
level telegraf.LogLevel
timezone *time.Location
impl sink
earlysink *log.Logger
earlylogs *list.List
sync.Mutex
}
func defaultHandler() *handler {
return &handler{
level: telegraf.Info,
timezone: time.UTC,
earlysink: log.New(os.Stderr, "", 0),
earlylogs: list.New(),
}
}
func redirectHandler(w io.Writer) *handler {
return &handler{
level: 99,
timezone: time.UTC,
impl: &redirectLogger{writer: w},
earlysink: log.New(w, "", 0),
earlylogs: list.New(),
}
}
func (h *handler) switchSink(impl sink, level telegraf.LogLevel, tz *time.Location, skipEarlyLogs bool) {
// Setup the new sink etc
h.impl = impl
h.level = level
h.timezone = tz
// Use the new logger to output the early log-messages
h.Lock()
if !skipEarlyLogs && h.earlylogs.Len() > 0 {
current := h.earlylogs.Front()
for current != nil {
e := current.Value.(*entry)
h.impl.Print(e.level, e.timestamp.In(h.timezone), e.prefix, e.attributes, e.args...)
next := current.Next()
h.earlylogs.Remove(current)
current = next
}
}
h.Unlock()
}
func (h *handler) add(level telegraf.LogLevel, ts time.Time, prefix string, attr map[string]interface{}, args ...interface{}) *entry {
e := &entry{
timestamp: ts,
level: level,
prefix: prefix,
attributes: attr,
args: args,
}
h.Lock()
h.earlylogs.PushBack(e)
h.Unlock()
return e
}
func (h *handler) close() error {
if h.impl == nil {
return nil
}
h.Lock()
current := h.earlylogs.Front()
for current != nil {
h.earlylogs.Remove(current)
current = h.earlylogs.Front()
}
h.Unlock()
if l, ok := h.impl.(io.Closer); ok {
return l.Close()
}
return nil
}
// Logger to redirect the logs to an arbitrary writer
type redirectLogger struct {
writer io.Writer
}
func (l *redirectLogger) Print(level telegraf.LogLevel, ts time.Time, prefix string, attr map[string]interface{}, args ...interface{}) {
var attrMsg string
if len(attr) > 0 {
var parts []string
for k, v := range attr {
parts = append(parts, fmt.Sprintf("%s=%v", k, v))
}
attrMsg = "(" + strings.Join(parts, ",") + ")"
}
msg := []interface{}{ts.In(time.UTC).Format(time.RFC3339), level.Indicator()}
if prefix+attrMsg != "" {
msg = append(msg, prefix+attrMsg)
}
msg = append(msg, args...)
fmt.Fprintln(l.writer, msg...)
}
func (l *redirectLogger) Close() error {
if l.writer == os.Stderr {
return nil
}
if closer, ok := l.writer.(io.Closer); ok {
return closer.Close()
}
return nil
}