mgorus

package module
v0.0.0-...-239539f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 29, 2018 License: MIT Imports: 4 Imported by: 25

README

Mongodb Hooks for Logrus

Install

$ go get github.com/weekface/mgorus

Usage

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!")
}

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewHooker

func NewHooker(mgoUrl, db, collection string) (*hooker, error)

func NewHookerFromCollection

func NewHookerFromCollection(collection *mgo.Collection) *hooker

func NewHookerWithAuth

func NewHookerWithAuth(mgoUrl, db, collection, user, pass string) (*hooker, error)

func NewHookerWithAuthDb

func NewHookerWithAuthDb(mgoUrl, authdb, db, collection, user, pass string) (*hooker, error)

Types

type M

type M bson.M

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL