-
Notifications
You must be signed in to change notification settings - Fork 911
Fix #4240: 旧版MC日志把每一条都给算成error #6304
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ | |
| */ | ||
| package org.jackhuang.hmcl.util; | ||
|
|
||
| import java.util.logging.Level; | ||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
|
|
||
|
|
@@ -50,66 +51,54 @@ public boolean lessOrEqual(Log4jLevel level) { | |
| public static final Pattern MINECRAFT_LOGGER = Pattern.compile("\\[(?<timestamp>[0-9:]+)] \\[[^/]+/(?<level>[^]]+)]"); | ||
| public static final Pattern MINECRAFT_LOGGER_CATEGORY = Pattern.compile("\\[(?<timestamp>[0-9:]+)] \\[[^/]+/(?<level>[^]]+)] \\[(?<category>[^]]+)]"); | ||
| public static final String JAVA_SYMBOL = "([a-zA-Z_$][a-zA-Z\\d_$]*\\.)+[a-zA-Z_$][a-zA-Z\\d_$]*"; | ||
| private static final String WRAPPED_PRINT_STREAM = "[java.lang.Throwable$WrappedPrintStream:println"; | ||
|
|
||
| private static final String[] INFO_MARKERS = markers( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
These static marker arrays are initialized once and treated as immutable lookup tables, but their types omit the required type-use AGENTS.md reference: AGENTS.md:L11-L14 Useful? React with 👍 / 👎. |
||
| Level.INFO, | ||
| Level.CONFIG, | ||
| Level.FINE, | ||
| Level.FINER, | ||
| Level.FINEST | ||
| ); | ||
| private static final String[] ERROR_MARKERS = markers(Level.SEVERE); | ||
| private static final String[] WARN_MARKERS = markers(Level.WARNING); | ||
|
|
||
| public static Log4jLevel guessLevel(String line) { | ||
| Log4jLevel level = null; | ||
| Matcher m = MINECRAFT_LOGGER.matcher(line); | ||
| if (m.find()) { | ||
| // New style logs from log4j | ||
| String levelStr = m.group("level"); | ||
| if (null != levelStr) | ||
| switch (levelStr) { | ||
| case "INFO": | ||
| level = INFO; | ||
| break; | ||
| case "WARN": | ||
| level = WARN; | ||
| break; | ||
| case "ERROR": | ||
| level = ERROR; | ||
| break; | ||
| case "FATAL": | ||
| level = FATAL; | ||
| break; | ||
| case "TRACE": | ||
| level = TRACE; | ||
| break; | ||
| case "DEBUG": | ||
| level = DEBUG; | ||
| break; | ||
| default: | ||
| break; | ||
| } | ||
| level = parseLevel(m.group("level")); | ||
| Matcher m2 = MINECRAFT_LOGGER_CATEGORY.matcher(line); | ||
| if (m2.find()) { | ||
| String level2Str = m2.group("category"); | ||
| if (null != level2Str) | ||
| switch (level2Str) { | ||
| case "STDOUT": | ||
| level = INFO; | ||
| break; | ||
| case "STDERR": | ||
| level = ERROR; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if (line.contains("STDERR]") || line.contains("[STDERR/]")) { | ||
| level = ERROR; | ||
| if (level2Str != null) { | ||
| level = switch (level2Str) { | ||
| case "STDOUT" -> INFO; | ||
| case "STDERR" -> guessStderrLevel(line, level); | ||
| default -> level; | ||
| }; | ||
| } | ||
| } else if (line.contains("STDERR]") || line.contains("[STDERR/]")) { | ||
| level = guessStderrLevel(line, level); | ||
| } | ||
| } else { | ||
| if (line.contains("[INFO]") || line.contains("[CONFIG]") || line.contains("[FINE]") | ||
| || line.contains("[FINER]") || line.contains("[FINEST]")) | ||
| if (containsAny(line, INFO_MARKERS)) { | ||
| level = INFO; | ||
| if (line.contains("[SEVERE]") || line.contains("[STDERR]")) | ||
| } | ||
| if (containsAny(line, ERROR_MARKERS) || line.contains("[STDERR]")) { | ||
| level = ERROR; | ||
| if (line.contains("[WARNING]")) | ||
| } | ||
| if (containsAny(line, WARN_MARKERS)) { | ||
| level = WARN; | ||
| if (line.contains("[DEBUG]")) | ||
| } | ||
| if (line.contains("[DEBUG]")) { | ||
| level = DEBUG; | ||
| } | ||
| } | ||
| if (line.contains("overwriting existing")) | ||
|
|
||
| if (line.contains("overwriting existing")) { | ||
| level = FATAL; | ||
| } | ||
|
|
||
| /*if (line.contains("Exception in thread") | ||
| || line.matches("\\s+at " + JAVA_SYMBOL) | ||
|
|
@@ -120,17 +109,52 @@ public static Log4jLevel guessLevel(String line) { | |
| return level; | ||
| } | ||
|
|
||
| public static boolean isError(Log4jLevel a) { | ||
| return a != null && a.lessOrEqual(Log4jLevel.ERROR); | ||
| public static Log4jLevel guessLevel(String line, boolean isErrorStream) { | ||
| Log4jLevel level = guessLevel(line); | ||
| return level != null || !isErrorStream ? level : ERROR; | ||
|
Comment on lines
+112
to
+114
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This overload returns AGENTS.md reference: AGENTS.md:L7-L9 Useful? React with 👍 / 👎. |
||
| } | ||
|
|
||
| public static Log4jLevel mergeLevel(Log4jLevel a, Log4jLevel b) { | ||
| if (a == null) | ||
| return b; | ||
| else if (b == null) | ||
| return a; | ||
| else | ||
| return a.level < b.level ? a : b; | ||
| private static Log4jLevel parseLevel(String level) { | ||
|
Wulian233 marked this conversation as resolved.
|
||
| return switch (level) { | ||
| case "FATAL" -> FATAL; | ||
| case "ERROR" -> ERROR; | ||
| case "WARN" -> WARN; | ||
| case "INFO" -> INFO; | ||
| case "DEBUG" -> DEBUG; | ||
| case "TRACE" -> TRACE; | ||
| case "ALL" -> ALL; | ||
| default -> null; | ||
| }; | ||
| } | ||
|
|
||
| private static Log4jLevel guessStderrLevel(String line, Log4jLevel fallback) { | ||
| if (line.contains(WRAPPED_PRINT_STREAM) && fallback != null) { | ||
| return fallback; | ||
| } | ||
| return ERROR; | ||
| } | ||
|
|
||
| private static String[] markers(Level... levels) { | ||
| String[] markers = new String[levels.length * 2]; | ||
| int i = 0; | ||
| for (Level level : levels) { | ||
| markers[i++] = '[' + level.getName() + ']'; | ||
| markers[i++] = '[' + level.getLocalizedName() + ']'; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Useful? React with 👍 / 👎. |
||
| } | ||
| return markers; | ||
| } | ||
|
Wulian233 marked this conversation as resolved.
|
||
|
|
||
| private static boolean containsAny(String line, String[] markers) { | ||
| for (String marker : markers) { | ||
| if (line.contains(marker)) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| public static boolean isError(Log4jLevel a) { | ||
| return a != null && a.lessOrEqual(Log4jLevel.ERROR); | ||
| } | ||
|
|
||
| public static boolean guessLogLineError(String log) { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.