mongohook

package module
v0.0.0-...-79ee868 Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2018 License: MIT Imports: 7 Imported by: 1

README

An asynchronous MongoDB Hook for Logrus

Build Codecov ReportCard GoDoc License

Quick Start

Download and install
$ go get -u -v github.com/LyricTian/logrus-mongo-hook
Usage
package main

import (
    "github.com/LyricTian/logrus-mongo-hook"
    "github.com/sirupsen/logrus"
)

func main() {
    hook := mongohook.DefaultWithURL("127.0.0.1:27017","test","t_log")
    defer hook.Flush()

    log := logrus.New()
    log.AddHook(hook)
}

MIT License

Copyright (c) 2018 Lyric

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ExecCloser

type ExecCloser interface {
	Exec(entry *logrus.Entry) error
	Close() error
}

ExecCloser write the logrus entry to the database and close the database

func NewExec

func NewExec(sess *mgo.Session, dbName, cName string) ExecCloser

NewExec create an exec instance

func NewExecWithURL

func NewExecWithURL(url, dbName, cName string) ExecCloser

NewExecWithURL create an exec instance

type FilterHandle

type FilterHandle func(*logrus.Entry) *logrus.Entry

FilterHandle a filter handler

type Hook

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

Hook to send logs to a mongo database

Example
sess, err := mgo.Dial(mgoURL)
if err != nil {
	fmt.Println(err)
	return
}
defer sess.Close()

cName := "e_log"
hook := mongohook.Default(sess, dbName, cName)
defer sess.DB(dbName).C(cName).DropCollection()

log := logrus.New()
log.AddHook(hook)
log.WithField("foo", "bar").Info("foo test")
hook.Flush()

var item struct {
	Message string `bson:"message"`
}
err = sess.DB(dbName).C(cName).Find(nil).Select(bson.M{"_id": 0, "message": 1}).One(&item)
if err != nil {
	fmt.Println(err)
	return
}

fmt.Println(item.Message)
Output:

foo test

func Default

func Default(sess *mgo.Session, dbName, cName string, opts ...Option) *Hook

Default create a default mongo hook

func DefaultWithURL

func DefaultWithURL(url, dbName, cName string, opts ...Option) *Hook

DefaultWithURL create a default mongo hook

func New

func New(opt ...Option) *Hook

New creates a hook to be added to an instance of logger

func (*Hook) Fire

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

Fire is called when a log event is fired

func (*Hook) Flush

func (h *Hook) Flush()

Flush waits for the log queue to be empty

func (*Hook) Levels

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

Levels returns the available logging levels

type Option

type Option func(*options)

Option a hook parameter options

func SetExec

func SetExec(exec ExecCloser) Option

SetExec set the Execer interface

func SetExtra

func SetExtra(extra map[string]interface{}) Option

SetExtra set extended parameters

func SetFilter

func SetFilter(filter FilterHandle) Option

SetFilter set the entry filter

func SetLevels

func SetLevels(levels ...logrus.Level) Option

SetLevels set the available log level

func SetMaxQueues

func SetMaxQueues(maxQueues int) Option

SetMaxQueues set the number of buffers

func SetMaxWorkers

func SetMaxWorkers(maxWorkers int) Option

SetMaxWorkers set the number of worker threads

func SetOut

func SetOut(out io.Writer) Option

SetOut set error output

Jump to

Keyboard shortcuts

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