com.github.houbb.logstash4j.plugins.output.File Maven / Gradle / Ivy
The newest version!
package com.github.houbb.logstash4j.plugins.output;
import com.github.houbb.heaven.util.io.FileUtil;
import com.github.houbb.logstash4j.plugins.api.config.ILogstashConfig;
import com.github.houbb.logstash4j.plugins.api.output.AbstractLogstashOutput;
import com.github.houbb.logstash4j.plugins.api.support.LogstashEventDataContext;
import com.github.houbb.logstash4j.plugins.api.support.format.EventMapFormatFactory;
import com.github.houbb.logstash4j.plugins.api.support.format.IEventMapFormat;
import com.github.houbb.logstash4j.plugins.output.constant.FileConfigEnum;
public class File extends AbstractLogstashOutput {
private IEventMapFormat eventMapFormat;
private String path;
@Override
public void register(ILogstashConfig config) {
super.register(config);
// 初始化
this.init();
}
protected void init() {
this.path = (String) getConfigVal(FileConfigEnum.PATH);
String format = (String) getConfigVal(FileConfigEnum.FORMAT);
eventMapFormat = EventMapFormatFactory.getFormat(format);
}
@Override
public void doProcess(LogstashEventDataContext context) {
String formatLine = eventMapFormat.format(context.getEventMap());
FileUtil.append(path, formatLine);
}
protected Object getConfigVal(final FileConfigEnum configEnum) {
return super.getConfigOrDefault(configEnum.getCode(), configEnum.getDefaultValue());
}
}