Skip to content

Commit

Permalink
Add kinesis hook
Browse files Browse the repository at this point in the history
  • Loading branch information
evalphobia committed Sep 9, 2016
1 parent 5f4045c commit 16fed0b
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
File renamed without changes.
79 changes: 79 additions & 0 deletions log/kinesis/kinesis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package kinesis

import (
"net/http"

"github.com/Sirupsen/logrus"
"github.com/evalphobia/logrus_kinesis"

"github.com/evalphobia/go-log-wrapper/log"
)

// default logging hook level
var hookLevel = []logrus.Level{
logrus.PanicLevel,
logrus.ErrorLevel,
}

func SetLevels(levels []logrus.Level) {
hookLevel = levels
}

func AddLevel(level logrus.Level) {
hookLevel = append(hookLevel, level)
}

func Set(name string, conf Config) error {
hook, err := logrus_kinesis.New(name, logrus_kinesis.Config{
AccessKey: conf.AccessKey,
SecretKey: conf.SecretKey,
Region: conf.Region,
})
if err != nil {
return err
}

hook.SetLevels(hookLevel)
hook.AddIgnore("context")
hook.AddFilter("trace", filterTrace)
hook.AddFilter("http_request", filterRequest)
hook.Async()
logrus.AddHook(hook)
return nil
}

type Config struct {
AccessKey string
SecretKey string
Region string
}

func filterTrace(v interface{}) interface{} {
trace, ok := v.([]log.StackTrace)
if !ok || len(trace) == 0 {
return v
}

return trace[0]
}

func filterRequest(v interface{}) interface{} {
req, ok := v.(*http.Request)
if !ok {
return v
}

return request{
Method: req.Method,
Host: req.Host,
RequestURI: req.RequestURI,
RemoteAddr: req.RemoteAddr,
}
}

type request struct {
Method string
Host string
RequestURI string
RemoteAddr string
}

0 comments on commit 16fed0b

Please sign in to comment.