我正在开发一个Java项目,一个日志分析器,以通用的方式导入任何类型的日志信息。 实际上,我在Log类实例中划分了任何日志表,并在Event类实例中划分了任何表单行。 问题是:日志读取所有工作表,并使用每个读取行实例化一个事件,将其放在LinkedHashSet中。 当构造事件时,它取决于它所处的日志类型 - 所有信息(例如事件发生的时间)。 除了从事件字符串中获取日期/时间外,一切都完美无瑕。
事实上,没有任何类型的错误,Log添加了Event循环,停止。 删除每个与时间相关的行,我正确地设法获得所有内容:使用它们,它会停止,没有任何错误,但让我只获得一堆行,而不是我应该得到的数千行。
在这里你可以找到日志和事件类,甚至是从事件中获取时间的方法,以及从事件的最后一行获取时间和第一个没有时间的方法。
日志:
public class Log {
/** LogType assigned to log file */
private LogType type;
/** filename associated to log file*/
private String name;
/** path associated to log file */
private String path;
private LinkedHashSet<Evento> events;
/**
* Log constructor:
* @param path points to file which create log instance from
* @param type is the LogType type associated with the rising Log
* @param bInitialize indicates if Log has to initialize events list
*/
public Log(String path, LogType type, boolean bInitialize) {
String[] pathComponents = path.split("/");
this.path = path;
this.type = type;
this.name = pathComponents[pathComponents.length - 1];
populateEvents();
}
public LinkedHashSet<Evento> getEvents() {
return events;
}
/** type field getter */
public LogType getType() {
return type;
}
/** name field getter */
public String getName() {
return name;
}
/** path field getter */
public String getPath() {
return path;
}
/** @return path field */
public String toString() {
return path;
}
public TreeSet<Utente> getUsers() {
TreeSet<Utente> users = new TreeSet<Utente>();
for (Evento event : events)
users.add(event.getUser());
return users;
}
private void populateEvents() {
events = new LinkedHashSet<Evento>();
try {
File file = new File(path);
FileInputStream fInputStream = new FileInputStream(file);
byte[] data = new byte[(int) file.length()];
fInputStream.read(data);
fInputStream.close();
String[] eventsRead = new String(data, "UTF-8").split("\n");
for (String event : eventsRead)
events.add(new Evento(event, type));
} catch (Exception e) {
// Nothing really needed.
}
}
/** name field setter */
public void setName(String name) {
this.name = name;
}
/**
* Requests that the file or directory denoted by this abstract
* pathname be deleted when the virtual machine terminates.
*/
public void setToDeleted() {
new File(path).deleteOnExit();
}
}
事件:
public class Evento implements Comparable<Evento> {
private LocalDateTime time;
private LogType type;
private String event;
private Utente user;
public Evento(String event, LogType type) {
this.event = event;
this.type = type;
time = type.getAssociatedLoader().getTimeFromLine(event);
user = type.getAssociatedLoader().getUserFromLine(event);
}
public boolean equals(Evento comparedEvent) {
return event.equals(comparedEvent.getEvent());
}
@Override
public int compareTo(Evento comparedEvent) {
return event.compareTo(comparedEvent.getEvent());
}
public LocalDateTime getTime() {
return time;
}
public String getEvent() {
return event;
}
public Utente getUser() {
return user;
}
@Override
public String toString() {
return event;
}
}
getTimeFromLine()方法:
@Override
public LocalDateTime getTimeFromLine(String line) {
String clockString = line.split("\t")[2];
return LocalDateTime.of(Integer.parseInt(clockString.substring(0,4)),
Integer.parseInt(clockString.substring(6,7)),
Integer.parseInt(clockString.substring(9,10)),
Integer.parseInt(clockString.substring(11,13)),
Integer.parseInt(clockString.substring(15,16)),
Integer.parseInt(clockString.substring(17,19)));
}
行示例(首先正确工作,而不是后者):
142\twestchester.gov\t2006-03-20 03:55:57\t1\thttp://www.westchestergov.com
142\tspace.comhttp\t2006-03-24 20:51:24\t\t
答案 0 :(得分:1)
你不应该吞下异常 - 如果你让它传播你会有一个相当自我解释的异常消息,可以帮助你找到问题!
java.time.DateTimeException:DayOfMonth的值无效(有效值1 - 28/31):0
因此,您的clockString.substring()
之一未使用正确的索引
您的getTimeFromLine
方法不必要复杂,我建议使用DateTimeFormatter
而不是手动解析字符串
建议更换:
private static final DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
public LocalDateTime getTimeFromLine(String line) {
String clockString = line.split("\t")[2];
return LocalDateTime.parse(clockString, fmt);
}