Add support for terminals in cat server

Fixes #33
This commit is contained in:
games647 2022-08-22 12:57:27 +02:00
parent 10af97eb50
commit 8846241df8
No known key found for this signature in database
GPG Key ID: BFC68C8708713A88
3 changed files with 19 additions and 9 deletions

View File

@ -1,5 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.games647</groupId>
@ -92,7 +92,7 @@
<id>sponge-repo</id>
<url>https://repo.spongepowered.org/maven</url>
</repository>
<repository>
<id>bungeecord-repo</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>

View File

@ -13,6 +13,7 @@ import java.util.logging.Level;
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.Layout;
import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
@ -20,6 +21,8 @@ import org.bukkit.plugin.java.JavaPlugin;
public class ColorConsoleBukkit extends JavaPlugin implements PlatformPlugin {
private static final String TERMINAL_NAME = "TerminalConsole";
private static final String CATSERVER_TERMINAL = "Console";
private final Log4JInstaller installer = new Log4JInstaller();
@ -42,7 +45,7 @@ public class ColorConsoleBukkit extends JavaPlugin implements PlatformPlugin {
@Override
public void installLogFormat(ConsoleConfig configuration) {
try {
oldLayout = installer.installLog4JFormat(this, TERMINAL_NAME, configuration);
oldLayout = installer.installLog4JFormat(this, getTerminalName(), configuration);
} catch (ReflectiveOperationException reflectiveEx) {
getLogger().log(Level.WARNING, "Failed to install log format", reflectiveEx);
}
@ -56,7 +59,7 @@ public class ColorConsoleBukkit extends JavaPlugin implements PlatformPlugin {
@Override
public void revertLogFormat() {
try {
installer.revertLog4JFormat(TERMINAL_NAME, oldLayout);
installer.revertLog4JFormat(getTerminalName(), oldLayout);
} catch (ReflectiveOperationException ex) {
getLogger().log(Level.WARNING, "Cannot revert log format", ex);
}
@ -98,4 +101,12 @@ public class ColorConsoleBukkit extends JavaPlugin implements PlatformPlugin {
consoleConfig.setTruncateColor(bukkitConfig.getBoolean("truncateColor"));
return consoleConfig;
}
private String getTerminalName() {
if (Bukkit.getVersion().contains("Cat")) {
return CATSERVER_TERMINAL;
}
return TERMINAL_NAME;
}
}

View File

@ -8,8 +8,8 @@ import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.EnumMap;
import java.util.Map;
import java.util.TreeMap;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.Appender;
@ -108,8 +108,7 @@ public class Log4JInstaller {
public Appender getTerminalAppender(String terminalName) {
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Configuration conf = ctx.getConfiguration();
return conf.getAppenders().get(terminalName);
return conf.getAppender(terminalName);
}
@VisibleForTesting
@ -119,12 +118,12 @@ public class Log4JInstaller {
@VisibleForTesting
protected String formatDate(String logFormat, String dateStyle) {
return logFormat.replaceFirst("(%d)\\{.*?\\}", "%style{$0}{" + dateStyle + '}');
return logFormat.replaceFirst("(%d)\\{.*?}", "%style{$0}{" + dateStyle + '}');
}
@VisibleForTesting
protected String mapLoggingLevels(String logFormat, Map<LoggingLevel, String> levelColors) {
Map<LoggingLevel, String> sortedColors = new TreeMap<>();
Map<LoggingLevel, String> sortedColors = new EnumMap<>(LoggingLevel.class);
sortedColors.putAll(levelColors);
String levelFormat = Joiner.on(", ").withKeyValueSeparator('=').join(sortedColors);