Loading

基于mongo-driver的logrus钩子

项目地址

https://github.com/wo4zhuzi/mongorus

示例

直连mongodb

package main

import (
    "fmt"
    "github.com/sirupsen/logrus"
    "github.com/wo4zhuzi/mongorus"
)

var Logger *logrus.Logger

func main() {
    Logger = logrus.New()

    hooker, err := mongorus.NewMongoHook("127.0.0.1", "test_db", "test_collection")

    if err == nil {
        Logger.Hooks.Add(hooker)
    } else {
        fmt.Print(err)
    }

    Logger.WithFields(logrus.Fields{
        "blockHeight": 1000,
        "txid":        "0x......",
    }).Warn("warn message : .........")
}

权限连接

package main

import (
    "fmt"
    "github.com/sirupsen/logrus"
    "github.com/wo4zhuzi/mongorus"
    "go.mongodb.org/mongo-driver/mongo/options"
)

var Logger *logrus.Logger

func main() {
    Logger = logrus.New()
    hooker, err := mongorus.NewAuthMongoHook("127.0.0.1:12017", "test_db", "test_collection", options.Credential{
        Username: "test_username",
        Password: "test_password",
    })

    if err == nil {
        Logger.Hooks.Add(hooker)
    } else {
        fmt.Print(err)
    }

    Logger.WithFields(logrus.Fields{
        "blockHeight": 1000,
        "txid":        "0x......",
    }).Warn("warn message : .........")
}

输出结果

image

image

posted @ 2021-07-06 23:53  小博。  阅读(212)  评论(0编辑  收藏  举报