-
Notifications
You must be signed in to change notification settings - Fork 0
/
tailoplog.go
52 lines (49 loc) · 933 Bytes
/
tailoplog.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
package main
import (
"encoding/json"
"log"
"os"
"time"
"github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson"
)
func main() {
mongoHost := "127.0.0.1:27017"
if len(os.Args) > 1 {
mongoHost = os.Args[1]
}
session, err := mgo.Dial(mongoHost)
if err != nil {
log.Fatal(err)
}
var last bson.M
err = session.DB("local").C("oplog.rs").Find(nil).Sort("-$natural").One(&last)
if err != nil {
panic(err)
}
iter := session.DB("local").C("oplog.rs").Find(bson.M{"ts": map[string]interface{}{"$gt": last["ts"]}}).LogReplay().Tail(time.Second * 5)
for {
for {
var result bson.M
ok := iter.Next(&result)
if !ok {
break
}
if result["ns"].(string) == "" {
continue
}
b, err := json.Marshal(result)
if err != nil {
panic(err)
}
println(string(b))
}
if iter.Err() != nil {
panic(iter.Close())
}
if iter.Timeout() {
continue
}
panic("unexpect error")
}
}