forked from premendrasingh/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
97 lines (80 loc) · 2.13 KB
/
file.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
package fileout
import (
"encoding/json"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/common/op"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/libbeat/outputs"
)
func init() {
outputs.RegisterOutputPlugin("file", New)
}
type fileOutput struct {
rotator logp.FileRotator
}
// New instantiates a new file output instance.
func New(cfg *common.Config, _ int) (outputs.Outputer, error) {
config := defaultConfig
if err := cfg.Unpack(&config); err != nil {
return nil, err
}
// disable bulk support in publisher pipeline
cfg.SetInt("flush_interval", -1, -1)
cfg.SetInt("bulk_max_size", -1, -1)
output := &fileOutput{}
if err := output.init(config); err != nil {
return nil, err
}
return output, nil
}
func (out *fileOutput) init(config config) error {
out.rotator.Path = config.Path
out.rotator.Name = config.Filename
if out.rotator.Name == "" {
out.rotator.Name = config.Index
}
logp.Info("File output path set to: %v", out.rotator.Path)
logp.Info("File output base filename set to: %v", out.rotator.Name)
rotateeverybytes := uint64(config.RotateEveryKb) * 1024
logp.Info("Rotate every bytes set to: %v", rotateeverybytes)
out.rotator.RotateEveryBytes = &rotateeverybytes
keepfiles := config.NumberOfFiles
logp.Info("Number of files set to: %v", keepfiles)
out.rotator.KeepFiles = &keepfiles
err := out.rotator.CreateDirectory()
if err != nil {
return err
}
err = out.rotator.CheckIfConfigSane()
if err != nil {
return err
}
return nil
}
// Implement Outputer
func (out *fileOutput) Close() error {
return nil
}
func (out *fileOutput) PublishEvent(
sig op.Signaler,
opts outputs.Options,
event common.MapStr,
) error {
jsonEvent, err := json.Marshal(event)
if err != nil {
// mark as success so event is not sent again.
op.SigCompleted(sig)
logp.Err("Fail to json encode event(%v): %#v", err, event)
return err
}
err = out.rotator.WriteLine(jsonEvent)
if err != nil {
if opts.Guaranteed {
logp.Critical("Unable to write events to file: %s", err)
} else {
logp.Err("Error when writing line to file: %s", err)
}
}
op.Sig(sig, err)
return err
}