forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
107 lines (87 loc) · 2.26 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
98
99
100
101
102
103
104
105
106
107
package fileout
import (
"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 {
beatName string
rotator logp.FileRotator
codec outputs.Codec
}
// New instantiates a new file output instance.
func New(beatName string, 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{beatName: beatName}
if err := output.init(config); err != nil {
return nil, err
}
return output, nil
}
func (out *fileOutput) init(config config) error {
var err error
out.rotator.Path = config.Path
out.rotator.Name = config.Filename
if out.rotator.Name == "" {
out.rotator.Name = out.beatName
}
codec, err := outputs.CreateEncoder(config.Codec)
if err != nil {
return err
}
out.codec = codec
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,
data outputs.Data,
) error {
var serializedEvent []byte
var err error
serializedEvent, err = out.codec.Encode(data.Event)
if err != nil {
op.SigCompleted(sig)
return err
}
err = out.rotator.WriteLine(serializedEvent)
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
}