aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/cowtipper/cowlection/search/LogFilesSearcher.java
blob: c005852de53dfcb2e5d3dc75a0c5482431f295f4 (plain)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package de.cowtipper.cowlection.search;

import de.cowtipper.cowlection.config.MooConfig;
import net.minecraft.util.EnumChatFormatting;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.*;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.zip.GZIPInputStream;

class LogFilesSearcher {
    private static final Pattern LOG_FILE_PATTERN = Pattern.compile("^(\\d{4})-(\\d{2})-(\\d{2})-\\d+\\.log\\.gz$");
    /**
     * Log4j.xml PatternLayout: [%d{HH:mm:ss}] [%t/%level]: %msg%n
     * Log line: [TIME] [THREAD/LEVEL]: [CHAT] msg
     * examples:
     * - [13:33:37] [Client thread/INFO]: [CHAT] Hello World
     * - [08:15:42] [Client thread/ERROR]: Item entity 9001 has no item?!
     */
    private static final Pattern LOG4J_PATTERN = Pattern.compile("^\\[(?<timeHours>[\\d]{2}):(?<timeMinutes>[\\d]{2}):(?<timeSeconds>[\\d]{2})] \\[(?<thread>[^/]+)/(?<logLevel>[A-Z]+)]:(?<isChat> \\[CHAT])? (?<message>.*)$");
    private int analyzedFilesWithHits = 0;

    LogSearchResults searchFor(String searchQuery, boolean chatOnly, boolean matchCase, boolean removeFormatting, LocalDate dateStart, LocalDate dateEnd) throws IOException {
        LogSearchResults logSearchResults = new LogSearchResults();
        long fileSizeLimit = MooConfig.getMaxLogFileSize();
        long latestLogSizeLimit = MooConfig.getMaxLatestLogFileSize();
        for (String logsDirPath : MooConfig.logsDirs) {
            File logsDir = new File(logsDirPath);
            if (!logsDir.exists() || !logsDir.isDirectory()) {
                continue;
            }
            try (Stream<Path> paths = Files.find(logsDir.toPath(), 1, (path, attr) -> {
                if (!attr.isRegularFile()) {
                    return false;
                }
                String fileName = path.getFileName().toString();
                return fileName.endsWith(".log.gz") || "latest.log".equals(fileName);
            }).collect(Collectors.toList()).parallelStream()) {
                paths.forEach(path -> {
                    String fileName = path.getFileName().toString();
                    if (fileName.endsWith("z")) { // .log.gz
                        Matcher fileNameMatcher = LOG_FILE_PATTERN.matcher(fileName);
                        if (fileNameMatcher.matches()) {
                            LocalDate fileLocalDate = LocalDate.of(Integer.parseInt(fileNameMatcher.group(1)),
                                    Integer.parseInt(fileNameMatcher.group(2)), Integer.parseInt(fileNameMatcher.group(3)));
                            if (!fileLocalDate.isBefore(dateStart) && !fileLocalDate.isAfter(dateEnd)) {
                                if (path.toFile().length() > fileSizeLimit) {
                                    // .log.gz file too large
                                    logSearchResults.addSkippedFile();
                                } else {
                                    logSearchResults.addAnalyzedFile();
                                    logSearchResults.addSearchResults(analyzeFile(path, true, fileLocalDate, searchQuery, chatOnly, matchCase, removeFormatting));
                                }
                            }
                        }
                    } else if (fileName.equals("latest.log")) {
                        LocalDate lastModified = Instant.ofEpochMilli(path.toFile().lastModified()).atZone(ZoneId.systemDefault()).toLocalDate();
                        if (!lastModified.isBefore(dateStart) && !lastModified.isAfter(dateEnd)) {
                            if (path.toFile().length() > latestLogSizeLimit) {
                                // latest.log too large
                                logSearchResults.addSkippedFile();
                            } else {
                                logSearchResults.addAnalyzedFile();
                                logSearchResults.addSearchResults(analyzeFile(path, false, lastModified, searchQuery, chatOnly, matchCase, removeFormatting));
                            }
                        }
                    }
                });
            } catch (IOException e) {
                throw new IOException(EnumChatFormatting.DARK_RED + "ERROR: An error occurred trying to read/parse '" + EnumChatFormatting.RED + logsDirPath + EnumChatFormatting.DARK_RED + "':\n"
                        + EnumChatFormatting.GOLD + e.getLocalizedMessage(), e);
            }
        }

        if (logSearchResults.getAnalyzedFiles() == 0) {
            // no files were analyzed
            int skippedFileCounter = logSearchResults.getSkippedFiles();
            if (skippedFileCounter > 0) {
                throw new FileNotFoundException(EnumChatFormatting.DARK_RED + "ERROR: No Minecraft log files could be found for the selected date range.\n"
                        + EnumChatFormatting.RED + skippedFileCounter + EnumChatFormatting.DARK_RED + " log files were skipped because they are too large (" + EnumChatFormatting.RED + ".log.gz" + EnumChatFormatting.DARK_RED + " files >" + FileUtils.byteCountToDisplaySize(MooConfig.getMaxLogFileSize()) + "; " + EnumChatFormatting.RED + "latest.log" + EnumChatFormatting.DARK_RED + " >" + FileUtils.byteCountToDisplaySize(MooConfig.getMaxLatestLogFileSize()) + ").\n"
                        + EnumChatFormatting.RED + "Please check if the dates as well as the directories of the log files are set correctly (Log Search ➡ Settings [top right corner]).\n"
                        + EnumChatFormatting.DARK_RED + "You could also increase the maximum allowed log file size to be searched (Log Search ➡ Settings), but note that each file must be unzipped before it can be analyzed, which can make the log file search take significantly longer for large files.");
            } else {
                throw new FileNotFoundException(EnumChatFormatting.DARK_RED + "ERROR: No Minecraft log files could be found for the selected date range.\n"
                        + EnumChatFormatting.RED + "Please check if the dates as well as the directories of the log files are set correctly (Log Search ➡ Settings [top right corner]).");
            }
        } else {
            logSearchResults.setAnalyzedFilesWithHits(analyzedFilesWithHits);
            return logSearchResults;
        }
    }

    private List<LogEntry> analyzeFile(Path path, boolean isGzipped, LocalDate date, String searchTerm, boolean chatOnly, boolean matchCase, boolean removeFormatting) {
        List<LogEntry> searchResults = new ArrayList<>();
        boolean foundSearchTermInFile = false;
        try (BufferedReader in = (isGzipped
                ? new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(path.toFile())))) // ....log.gz
                : new BufferedReader(new InputStreamReader(new FileInputStream(path.toFile()))))) { // latest.log
            String content;
            LogEntry logEntry = null;
            while ((content = in.readLine()) != null) {
                Matcher logLineMatcher = LOG4J_PATTERN.matcher(content);
                if (logLineMatcher.matches()) { // current line is a new log entry
                    if (logEntry != null) {
                        // we had a previous log entry; analyze it!
                        LogEntry result = analyzeLogEntry(logEntry, searchTerm, matchCase, removeFormatting);
                        if (result != null) {
                            searchResults.add(result);
                            foundSearchTermInFile = true;
                        }
                        logEntry = null;
                    }
                    // handle first line of new log entry
                    if (chatOnly && logLineMatcher.group("isChat") == null) {
                        // not a chat log entry, although we're only searching for chat messages, abort!
                        continue;
                    }
                    LocalDateTime dateTime = getDate(date, logLineMatcher);
                    logEntry = new LogEntry(dateTime, path, logLineMatcher.group("message"));
                } else if (logEntry != null) {
                    // multiline log entry
                    logEntry.addLogLine(content);
                }
            }
            if (logEntry != null) {
                // end of file! analyze last log entry in file
                LogEntry result = analyzeLogEntry(logEntry, searchTerm, matchCase, removeFormatting);
                if (result != null) {
                    searchResults.add(result);
                    foundSearchTermInFile = true;
                }
            }
            if (foundSearchTermInFile) {
                analyzedFilesWithHits++;
            }
        } catch (IOException ignored) {
            // most likely corrupted .log.gz file - skip it.
        }
        return searchResults;
    }

    private LocalDateTime getDate(LocalDate date, Matcher logLineMatcher) {
        int hour = Integer.parseInt(logLineMatcher.group(1));
        int minute = Integer.parseInt(logLineMatcher.group(2));
        int sec = Integer.parseInt(logLineMatcher.group(3));

        return LocalDateTime.of(date, LocalTime.of(hour, minute, sec));
    }

    private LogEntry analyzeLogEntry(LogEntry logEntry, String searchTerms, boolean matchCase, boolean removeFormatting) {
        if (logEntry.getMessage().length() > 5000) {
            // avoid ultra long log entries
            return null;
        }
        logEntry.fixWeirdCharacters();

        if (removeFormatting) {
            logEntry.removeFormatting();
        }
        String logMessage = logEntry.getMessage();
        if (!matchCase) {
            if (!StringUtils.containsIgnoreCase(logMessage, searchTerms)) {
                // no result, abort
                return null;
            }
        } else if (!logMessage.contains(searchTerms)) {
            // no result, abort
            return null;
        }

        return logEntry;
    }
}