mirror of
https://github.com/minio/minio.git
synced 2025-11-08 21:24:55 -05:00
logrus based logger
This commit is contained in:
22
vendor/github.com/weekface/mgorus/LICENSE
generated
vendored
Normal file
22
vendor/github.com/weekface/mgorus/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2015
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
34
vendor/github.com/weekface/mgorus/README.md
generated
vendored
Normal file
34
vendor/github.com/weekface/mgorus/README.md
generated
vendored
Normal file
@@ -0,0 +1,34 @@
|
||||
# Mongodb Hooks for [Logrus](https://github.com/Sirupsen/logrus) <img src="http://i.imgur.com/hTeVwmJ.png" width="40" height="40" alt=":walrus:" class="emoji" title=":walrus:"/>
|
||||
|
||||
## Install
|
||||
|
||||
```shell
|
||||
$ go get github.com/weekface/mgorus
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```go
|
||||
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)
|
||||
}
|
||||
|
||||
log.WithFields(logrus.Fields{
|
||||
"name": "zhangsan",
|
||||
"age": 28,
|
||||
}).Error("Hello world!")
|
||||
}
|
||||
```
|
||||
|
||||
## License
|
||||
*MIT*
|
||||
47
vendor/github.com/weekface/mgorus/mgorus.go
generated
vendored
Normal file
47
vendor/github.com/weekface/mgorus/mgorus.go
generated
vendored
Normal file
@@ -0,0 +1,47 @@
|
||||
package mgorus
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/Sirupsen/logrus"
|
||||
"gopkg.in/mgo.v2"
|
||||
"gopkg.in/mgo.v2/bson"
|
||||
)
|
||||
|
||||
type hooker struct {
|
||||
c *mgo.Collection
|
||||
}
|
||||
|
||||
type M bson.M
|
||||
|
||||
func NewHooker(mgoUrl, db, collection string) (*hooker, error) {
|
||||
session, err := mgo.Dial(mgoUrl)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &hooker{c: session.DB(db).C(collection)}, nil
|
||||
}
|
||||
|
||||
func (h *hooker) Fire(entry *logrus.Entry) error {
|
||||
entry.Data["Level"] = entry.Level.String()
|
||||
entry.Data["Time"] = entry.Time
|
||||
entry.Data["Message"] = entry.Message
|
||||
mgoErr := h.c.Insert(M(entry.Data))
|
||||
if mgoErr != nil {
|
||||
return fmt.Errorf("Failed to send log entry to mongodb: %s", mgoErr)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *hooker) Levels() []logrus.Level {
|
||||
return []logrus.Level{
|
||||
logrus.PanicLevel,
|
||||
logrus.FatalLevel,
|
||||
logrus.ErrorLevel,
|
||||
logrus.WarnLevel,
|
||||
logrus.InfoLevel,
|
||||
logrus.DebugLevel,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user