Mongodb Hooks for Logrus ![:walrus: :walrus:](https://camo.githubusercontent.com/af2c989074fb1620e1cde7d99bc233c64c759df90024a1c4caeb02cb151ab1f2/687474703a2f2f692e696d6775722e636f6d2f68546556776d4a2e706e67)
$ go get github.com/weekface/mgorus
package main
import (
"github.com/sirupsen/logrus"
"github.com/weekface/mgorus"
)
func main() {
log := logrus.New()
hooker, err := mgorus.NewHooker("localhost:27017", "db", "collection")
if err == nil {
log.Hooks.Add(hooker)
} else {
fmt.Print(err)
}
log.WithFields(logrus.Fields{
"name": "zhangsan",
"age": 28,
}).Error("Hello world!")
}
With authentication:
package main
import (
"github.com/sirupsen/logrus"
"github.com/weekface/mgorus"
)
func main() {
log := logrus.New()
hooker, err := mgorus.NewHookerWithAuth("localhost:27017", "db", "collection", "user", "pass")
if err == nil {
log.Hooks.Add(hooker)
} else {
fmt.Print(err)
}
log.WithFields(logrus.Fields{
"name": "zhangsan",
"age": 28,
}).Error("Hello world!")
}
With a pre-existing collection
package main
import (
"crypto/tls"
"log"
"net"
"time"
mgo "gopkg.in/mgo.v2"
"github.com/sirupsen/logrus"
"github.com/weekface/mgorus"
)
func main() {
s, err := mgo.DialWithInfo(&mgo.DialInfo{
Addrs: []string{"localhost:27017"},
Timeout: 5 * time.Second,
Database: "admin",
Username: "",
Password: "",
DialServer: func(addr *mgo.ServerAddr) (net.Conn, error) {
conn, err := tls.Dial("tcp", addr.String(), &tls.Config{InsecureSkipVerify: true})
return conn, err
},
})
if err != nil {
log.Fatalf("can't create session: %s\n", err)
}
c := s.DB("db").C("collection")
log := logrus.New()
hooker := mgorus.NewHookerFromCollection(c)
log.Hooks.Add(hooker)
log.WithFields(logrus.Fields{
"name": "zhangsan",
"age": 28,
}).Error("Hello world!")
}
MIT