forked from techjacker/repo-security-scanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.go
50 lines (43 loc) · 1.08 KB
/
log.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
import (
"fmt"
"github.com/sirupsen/logrus"
elastic "gopkg.in/olivere/elastic.v5"
elogrus "gopkg.in/sohlich/elogrus.v2"
"github.com/techjacker/diffence"
)
// Log is the log interface for the app
type Log interface {
Log(v ...interface{})
}
// Log is the log for the app
type Logger struct {
log *logrus.Logger
}
func (l Logger) Log(v ...interface{}) {
i := 1
for filename, rule := range v[0].(diffence.MatchedRules) {
i++
l.log.WithFields(logrus.Fields{
"organisation": v[1],
"repo": v[2],
"url": v[3],
"filename": filename,
"reason": rule[0].Caption,
}).Error(fmt.Sprintf("Violation found in %s:%s", v[1], v[2]))
}
}
// NewESLogger is a factory for Elasticsearch loggers
func NewESLogger(esUrl, esIndex string) (Logger, error) {
log := logrus.New()
client, err := elastic.NewClient(elastic.SetURL(esUrl))
if err != nil {
return Logger{}, err
}
hook, err := elogrus.NewElasticHook(client, "localhost", logrus.DebugLevel, esIndex)
if err != nil {
return Logger{}, err
}
log.Hooks.Add(hook)
return Logger{log: log}, nil
}