1 Star 0 Fork 0

zhangjungang/beats

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
file.go 2.26 KB
一键复制 编辑 原始数据 按行查看 历史
Nicolas Ruflin 提交于 2017-03-22 10:01 . Remove topology map (#3769)
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 {
beat common.BeatInfo
rotator logp.FileRotator
codec outputs.Codec
}
// New instantiates a new file output instance.
func New(beat common.BeatInfo, cfg *common.Config) (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{beat: beat}
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.beat.Beat
}
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
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zhangjungang/beats.git
git@gitee.com:zhangjungang/beats.git
zhangjungang
beats
beats
v6.0.0-alpha2

搜索帮助