-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
sysloglogger.go
87 lines (73 loc) · 2.52 KB
/
sysloglogger.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
/*
Copyright 2019 The Vitess 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 sysloglogger implements an optional plugin that logs all queries to syslog.
package sysloglogger
import (
"bytes"
"flag"
"log/syslog"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/servenv"
"vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv"
)
// syslogWriter is an interface that wraps syslog.Writer, so it can be mocked in unit tests.
type syslogWriter interface {
Info(string) error
Close() error
}
// writer holds a persistent connection to the syslog daemon (or a mock when under test).
var writer syslogWriter
// ch holds the tabletserver.StatsLogger channel to which this plugin subscribes (or a mock when under test).
var ch chan interface{}
// logQueries is the vttablet startup flag that must be set for this plugin to be active.
var logQueries = flag.Bool("log_queries", false, "Enable query logging to syslog.")
func init() {
servenv.OnRun(func() {
if *logQueries {
var err error
writer, err = syslog.New(syslog.LOG_INFO, "vtquerylogger")
if err != nil {
log.Errorf("Query logger is unable to connect to syslog: %v", err)
return
}
go run()
}
})
}
// Run logs queries to syslog, if the "log_queries" flag is set to true when starting vttablet.
func run() {
log.Info("Logging queries to syslog")
defer writer.Close()
// ch will only be non-nil in a unit test context, when a mock has been populated
if ch == nil {
ch = tabletenv.StatsLogger.Subscribe("gwslog")
defer tabletenv.StatsLogger.Unsubscribe(ch)
}
formatParams := map[string][]string{"full": {}}
for out := range ch {
stats, ok := out.(*tabletenv.LogStats)
if !ok {
log.Errorf("Unexpected value in query logs: %#v (expecting value of type %T)", out, &tabletenv.LogStats{})
continue
}
var b bytes.Buffer
if err := stats.Logf(&b, formatParams); err != nil {
log.Errorf("Error formatting logStats: %v", err)
continue
}
if err := writer.Info(b.String()); err != nil {
log.Errorf("Error writing to syslog: %v", err)
continue
}
}
}