-
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 2 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 |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ | |
|
|
||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
| import java.util.logging.Level; | ||
|
|
||
| /** | ||
| * | ||
|
|
@@ -50,60 +51,33 @@ 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"; | ||
|
|
||
| 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; | ||
| 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 (containsLevelMarker(line, Level.INFO) || containsLevelMarker(line, Level.CONFIG) | ||
| || containsLevelMarker(line, Level.FINE) || containsLevelMarker(line, Level.FINER) | ||
| || containsLevelMarker(line, Level.FINEST)) | ||
| level = INFO; | ||
| if (line.contains("[SEVERE]") || line.contains("[STDERR]")) | ||
| if (containsLevelMarker(line, Level.SEVERE)) | ||
| level = ERROR; | ||
| if (line.contains("[WARNING]")) | ||
| if (containsLevelMarker(line, Level.WARNING)) | ||
| level = WARN; | ||
| if (line.contains("[DEBUG]")) | ||
| level = DEBUG; | ||
|
|
@@ -120,17 +94,38 @@ 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 👍 / 👎. |
||
| } | ||
|
|
||
| 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; | ||
| }; | ||
| } | ||
|
|
||
| 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 guessStderrLevel(String line, Log4jLevel fallback) { | ||
| if (line.contains(WRAPPED_PRINT_STREAM) && fallback != null) { | ||
| return fallback; | ||
| } | ||
| return ERROR; | ||
| } | ||
|
|
||
| private static boolean containsLevelMarker(String line, Level level) { | ||
| return line.contains("[" + level.getName() + "]") | ||
|
Wulian233 marked this conversation as resolved.
Outdated
|
||
| || line.contains("[" + level.getLocalizedName() + "]"); | ||
| } | ||
|
|
||
| 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.