-
Notifications
You must be signed in to change notification settings - Fork 26
/
logs.go
138 lines (122 loc) · 3.35 KB
/
logs.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package logs
import (
"os"
"sync"
"github.com/appbaseio/reactivesearch-api/middleware"
"github.com/appbaseio/reactivesearch-api/plugins"
"github.com/natefinch/lumberjack"
"github.com/robfig/cron"
log "github.com/sirupsen/logrus"
)
const (
logTag = "[logs]"
defaultLogsEsIndex = ".logs"
envEsURL = "ES_CLUSTER_URL"
envLogsEsIndex = "LOGS_ES_INDEX"
defaultLogFilePath = "log/arc/es.json"
envLogFilePath = "LOG_FILE_PATH"
config = `
{
"aliases": {
"%s": {
"is_write_index": true
}
},
"settings": {
%s
"index.number_of_shards": 1,
"index.number_of_replicas": %d
},
"mappings": %s
}`
rolloverConfig = `{
"max_age": "%s",
"max_docs": %d,
"max_size": "%s"
}`
)
var (
singleton *Logs
once sync.Once
)
// Logs plugin records an elasticsearch request and its response.
type Logs struct {
es logsService
lumberjack lumberjack.Logger
enableDiffing bool
}
// Instance returns the singleton instance of Logs plugin.
// Note: Only this function must be used (both within and outside the package) to
// obtain the instance Logs in order to avoid stateless instances of the plugin.
func Instance() *Logs {
once.Do(func() { singleton = &Logs{} })
return singleton
}
// Name returns the name of the plugin: "[logs]"
func (l *Logs) Name() string {
return logTag
}
// Get the Lumberjack logger
func (l *Logs) Lumberjack() lumberjack.Logger {
return l.lumberjack
}
// InitFunc is a part of Plugin interface that gets executed only once, and initializes
// the dao, i.e. elasticsearch before the plugin is operational.
func (l *Logs) InitFunc() error {
// fetch the required env vars
indexName := os.Getenv(envLogsEsIndex)
if indexName == "" {
indexName = defaultLogsEsIndex
}
// initialize the elasticsearch client
var err error
l.es, err = initPlugin(indexName, config)
if err != nil {
return err
}
filePath := os.Getenv(envLogFilePath)
if filePath == "" {
log.Warnln(logTag, envLogFilePath+" is not defined log will get stored at ", defaultLogFilePath)
filePath = defaultLogFilePath
}
// configure lumberjack
l.lumberjack = lumberjack.Logger{
Filename: filePath,
MaxSize: 1000,
MaxBackups: 3,
MaxAge: 30, //days
}
// init cron job
cronjob := cron.New()
cronjob.AddFunc("@midnight", func() { l.es.rolloverIndexJob(indexName) })
cronjob.Start()
// Set the value for enableDiffing
//
// We will read the value from an environment variable
// though it is passes as a commandline flag.
//
// NOTE: It is probably not the trivial way to read the value
// but as of now there is no way to pass a variable from
// main to the InitFunc of plugins and will require a lot
// of structural changes
shouldEnableDiffing := os.Getenv("SHOULD_ENABLE_DIFFING")
if shouldEnableDiffing == "true" {
l.enableDiffing = true
} else {
l.enableDiffing = false
}
log.Debug(logTag, ": Diffing is enabled: ", l.enableDiffing)
return nil
}
// Routes returns an empty slice of routes, since Logs is solely a middleware.
func (l *Logs) Routes() []plugins.Route {
return l.routes()
}
// Default empty middleware array function
func (l *Logs) ESMiddleware() []middleware.Middleware {
return make([]middleware.Middleware, 0)
}
// Default empty middleware array function
func (a *Logs) RSMiddleware() []middleware.Middleware {
return make([]middleware.Middleware, 0)
}