mirror of
https://github.com/Febbweiss/logstash-forwarder-java.git
synced 2026-03-04 22:25:39 +00:00
Fix #17.
This commit is contained in:
@@ -46,11 +46,12 @@ public class FileWatcher {
|
|||||||
private boolean tail = false;
|
private boolean tail = false;
|
||||||
private Event stdinFields;
|
private Event stdinFields;
|
||||||
private boolean stdinConfigured = false;
|
private boolean stdinConfigured = false;
|
||||||
|
private String sincedbFile = null;
|
||||||
|
|
||||||
public FileWatcher() {
|
public FileWatcher() {
|
||||||
try {
|
try {
|
||||||
logger.debug("Loading saved states");
|
logger.debug("Loading saved states");
|
||||||
savedStates = Registrar.readStateFromJson();
|
savedStates = Registrar.readStateFromJson(sincedbFile);
|
||||||
} catch(Exception e) {
|
} catch(Exception e) {
|
||||||
logger.warn("Could not load saved states : " + e.getMessage());
|
logger.warn("Could not load saved states : " + e.getMessage());
|
||||||
}
|
}
|
||||||
@@ -103,7 +104,7 @@ public class FileWatcher {
|
|||||||
logger.trace("Reading files");
|
logger.trace("Reading files");
|
||||||
logger.trace("==============");
|
logger.trace("==============");
|
||||||
int numberOfLinesRead = reader.readFiles(oldWatchMap.values());
|
int numberOfLinesRead = reader.readFiles(oldWatchMap.values());
|
||||||
Registrar.writeStateToJson(oldWatchMap.values());
|
Registrar.writeStateToJson(sincedbFile,oldWatchMap.values());
|
||||||
return numberOfLinesRead;
|
return numberOfLinesRead;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -363,4 +364,8 @@ public class FileWatcher {
|
|||||||
this.tail = tail;
|
this.tail = tail;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setSincedb(String sincedbFile) {
|
||||||
|
this.sincedbFile = sincedbFile;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -47,6 +47,7 @@ import org.apache.log4j.RollingFileAppender;
|
|||||||
import org.apache.log4j.spi.RootLogger;
|
import org.apache.log4j.spi.RootLogger;
|
||||||
|
|
||||||
public class Forwarder {
|
public class Forwarder {
|
||||||
|
private static final String SINCEDB = ".logstash-forwarder-java";
|
||||||
private static Logger logger = Logger.getLogger(Forwarder.class);
|
private static Logger logger = Logger.getLogger(Forwarder.class);
|
||||||
private static int spoolSize = 1024;
|
private static int spoolSize = 1024;
|
||||||
private static int idleTimeout = 5000;
|
private static int idleTimeout = 5000;
|
||||||
@@ -65,6 +66,7 @@ public class Forwarder {
|
|||||||
private static String logfile = null;
|
private static String logfile = null;
|
||||||
private static String logfileSize = "10MB";
|
private static String logfileSize = "10MB";
|
||||||
private static int logfileNumber = 5;
|
private static int logfileNumber = 5;
|
||||||
|
private static String sincedbFile = SINCEDB;
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
try {
|
try {
|
||||||
@@ -73,6 +75,7 @@ public class Forwarder {
|
|||||||
watcher = new FileWatcher();
|
watcher = new FileWatcher();
|
||||||
watcher.setMaxSignatureLength(signatureLength);
|
watcher.setMaxSignatureLength(signatureLength);
|
||||||
watcher.setTail(tailSelected);
|
watcher.setTail(tailSelected);
|
||||||
|
watcher.setSincedb(sincedbFile);
|
||||||
configManager = new ConfigurationManager(config);
|
configManager = new ConfigurationManager(config);
|
||||||
configManager.readConfiguration();
|
configManager.readConfiguration();
|
||||||
for(FilesSection files : configManager.getConfig().getFiles()) {
|
for(FilesSection files : configManager.getConfig().getFiles()) {
|
||||||
@@ -181,6 +184,10 @@ public class Forwarder {
|
|||||||
.hasArg()
|
.hasArg()
|
||||||
.withDescription("Number of logfiles (default 5)")
|
.withDescription("Number of logfiles (default 5)")
|
||||||
.create("logfilenumber");
|
.create("logfilenumber");
|
||||||
|
Option sincedbOption = OptionBuilder.withArgName("sincedb file")
|
||||||
|
.hasArg()
|
||||||
|
.withDescription("Sincedb file name")
|
||||||
|
.create("sincedb");
|
||||||
|
|
||||||
options.addOption(helpOption)
|
options.addOption(helpOption)
|
||||||
.addOption(idleTimeoutOption)
|
.addOption(idleTimeoutOption)
|
||||||
@@ -194,7 +201,8 @@ public class Forwarder {
|
|||||||
.addOption(configOption)
|
.addOption(configOption)
|
||||||
.addOption(logfileOption)
|
.addOption(logfileOption)
|
||||||
.addOption(logfileNumberOption)
|
.addOption(logfileNumberOption)
|
||||||
.addOption(logfileSizeOption);
|
.addOption(logfileSizeOption)
|
||||||
|
.addOption(sincedbOption);
|
||||||
|
|
||||||
CommandLineParser parser = new GnuParser();
|
CommandLineParser parser = new GnuParser();
|
||||||
try {
|
try {
|
||||||
@@ -235,6 +243,9 @@ public class Forwarder {
|
|||||||
if(line.hasOption("logfilenumber")) {
|
if(line.hasOption("logfilenumber")) {
|
||||||
logfileNumber = Integer.parseInt(line.getOptionValue("logfilenumber"));
|
logfileNumber = Integer.parseInt(line.getOptionValue("logfilenumber"));
|
||||||
}
|
}
|
||||||
|
if(line.hasOption("sincedb")) {
|
||||||
|
sincedbFile = line.getOptionValue("sincedb");
|
||||||
|
}
|
||||||
} catch(ParseException e) {
|
} catch(ParseException e) {
|
||||||
printHelp(options);
|
printHelp(options);
|
||||||
System.exit(1);;
|
System.exit(1);;
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ import com.fasterxml.jackson.databind.JsonMappingException;
|
|||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
public class Registrar {
|
public class Registrar {
|
||||||
private static final String SINCEDB = ".logstash-forwarder-java";
|
|
||||||
|
|
||||||
private static ObjectMapper mapper = new ObjectMapper();
|
private static ObjectMapper mapper = new ObjectMapper();
|
||||||
|
|
||||||
@@ -40,10 +40,6 @@ public class Registrar {
|
|||||||
return readStateFromJson(new File(file));
|
return readStateFromJson(new File(file));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static FileState[] readStateFromJson() throws JsonParseException, JsonMappingException, IOException {
|
|
||||||
return readStateFromJson(SINCEDB);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void writeStateToJson(File file, Collection<FileState> stateList) throws JsonGenerationException, JsonMappingException, IOException {
|
public static void writeStateToJson(File file, Collection<FileState> stateList) throws JsonGenerationException, JsonMappingException, IOException {
|
||||||
mapper.writeValue(file, stateList);
|
mapper.writeValue(file, stateList);
|
||||||
}
|
}
|
||||||
@@ -52,7 +48,4 @@ public class Registrar {
|
|||||||
writeStateToJson(new File(file), stateList);
|
writeStateToJson(new File(file), stateList);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void writeStateToJson(Collection<FileState> stateList) throws JsonGenerationException, JsonMappingException, IOException {
|
|
||||||
writeStateToJson(SINCEDB, stateList);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user