-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
80 lines (64 loc) · 1.65 KB
/
main.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
package main
import (
log "github.com/cihub/seelog"
"github.com/gocql/gocql"
)
type TraceWriter struct {
session *gocql.Session
log log.LoggerInterface
}
func (t *TraceWriter) Write(p []byte) (n int, err error) {
t.log.Info(string(p[:len(p)-1]))
return len(p), nil
}
func NewTraceWriter(s *gocql.Session, l log.LoggerInterface) *TraceWriter {
return &TraceWriter{session: s, log: l}
}
func init() {
logger, err := log.LoggerFromConfigAsFile("seelog.xml")
if err != nil {
panic(err)
}
log.ReplaceLogger(logger)
}
func main() {
cluster := gocql.NewCluster("127.0.0.1")
cluster.Keyspace = "system"
session, err := cluster.CreateSession()
defer session.Close()
if err != nil {
log.Critical(err)
}
traceSession, err := cluster.CreateSession()
defer session.Close()
if err != nil {
log.Critical(err)
}
// Create a new logger instance that we adjust the stack depth for to get
// more meaningful frames
logger, err := log.LoggerFromConfigAsString(
`<seelog>
<outputs>
<console formatid="fmt"/>
</outputs>
<formats>
<format id="fmt" format="%Date(Jan 02 2006 03:04:05.000) [%LEVEL] %File:%Line - %Msg%n"/>
</formats>
</seelog>
`)
logger.SetAdditionalStackDepth(2)
if err != nil {
log.Critical(err)
}
writer := NewTraceWriter(traceSession, logger)
tracer := gocql.NewTraceWriter(traceSession, writer)
session.SetTrace(tracer)
var count int
iter := session.Query(`select count(*) from schema_keyspaces`).Iter()
iter.Scan(&count)
err = iter.Close()
if err != nil {
log.Critical(err)
}
log.Infof("This instance has %d keyspaces", count)
}