mgorus

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2018 License: MIT Imports: 4 Imported by: 10

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

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Hooker

type Hooker struct {
	// contains filtered or unexported fields
}

Hooker is a logrus hooker for mongodb

func NewHooker

func NewHooker(mgoURL, db, collection string) (*Hooker, error)

NewHooker dials a mongodb server without authentication or encryption and readies the hook to place logs inside of the given collection

func NewHookerFromSession

func NewHookerFromSession(session *mgo.Session, db, collection string) *Hooker

NewHookerFromSession makes a copy of an existing mongodb session and readies the hook to place logs inside of the given collection

func (*Hooker) Fire

func (h *Hooker) Fire(entry *logrus.Entry) error

Fire places a logrus entry into the log collection

func (*Hooker) Levels

func (h *Hooker) Levels() []logrus.Level

Levels returns the logrus levels the hook supports

Jump to

Keyboard shortcuts

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