-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.go
64 lines (55 loc) · 1.43 KB
/
plugin.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
package pluginServerLogs
import (
"fmt"
"time"
"github.com/log-rush/distribution-server/domain"
"github.com/log-rush/distribution-server/pkg/app"
"github.com/log-rush/distribution-server/pkg/devkit"
logging "github.com/log-rush/go-client"
)
type Config struct {
StreamName string
Id string
Key string
BatchSize int
}
type ServerLogsPlugin struct {
config Config
Plugin app.Plugin
stream logging.Stream
}
func NewServerLogsPlugin(config Config) ServerLogsPlugin {
plugin := ServerLogsPlugin{
config: config,
}
p := devkit.NewPlugin(
"server-logs",
devkit.PluginHandlers{
LoggerHandler: func(context *app.Context) domain.Logger {
batchSize := config.BatchSize
if batchSize < 20 {
batchSize = 20
}
plugin.stream = logging.NewLogStream(logging.ClientOptions{
DataSourceUrl: fmt.Sprintf("http://%s:%d/", context.Config.Host, context.Config.Port),
BatchSize: batchSize,
}, config.StreamName, config.Id, config.Key)
return devkit.NewLogger(plugin.HandleLog)
},
},
devkit.PluginHooks{
OnAfterServe: func(context *app.Context) {
go func() {
<-time.After(time.Second * 1)
plugin.stream.Register()
}()
},
},
)
plugin.Plugin = p
return plugin
}
func (p *ServerLogsPlugin) HandleLog(level devkit.LogLevel, template string, args ...interface{}) {
log := fmt.Sprintf("[server] [%s] %s", level, fmt.Sprintf(template, args...))
p.stream.Log(log)
}