Skip to content

Commit

Permalink
fix: fix double logging when gui is disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
smartcmd committed May 31, 2024
1 parent 45d1437 commit 09e5b67
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
22 changes: 21 additions & 1 deletion Allay-Server/src/main/java/org/allaymc/server/Allay.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.allaymc.server;

import lombok.extern.slf4j.Slf4j;
import net.minecrell.terminalconsole.TerminalConsoleAppender;
import org.allaymc.api.AllayAPI;
import org.allaymc.api.block.palette.BlockStateHashPalette;
import org.allaymc.api.block.registry.BlockTypeRegistry;
Expand Down Expand Up @@ -63,6 +64,9 @@
import org.allaymc.server.world.biome.AllayBiomeTypeRegistry;
import org.allaymc.server.world.generator.AllayWorldGeneratorFactory;
import org.allaymc.server.world.storage.AllayWorldStorageFactory;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.appender.ConsoleAppender;
import org.apache.logging.log4j.core.async.AsyncLoggerContextSelector;
import org.jetbrains.annotations.VisibleForTesting;

Expand All @@ -83,8 +87,14 @@ public static void main(String[] args) {
Server.SETTINGS.genericSettings().enableGui(false);
}
if (Server.SETTINGS.genericSettings().enableGui()) {
DASHBOARD = Dashboard.getInstance();
try {
DASHBOARD = Dashboard.getInstance();
} catch (Throwable t) {
log.error("Cannot init Dashboard!", t);
Server.SETTINGS.genericSettings().enableGui(false);
}
}
removeLog4jAppenderNotInUse(Server.SETTINGS.genericSettings().enableGui());
log.info("Starting Allay...");
try {
initAllayAPI();
Expand All @@ -101,6 +111,16 @@ public static void main(String[] args) {
}
}

private static void removeLog4jAppenderNotInUse(boolean useGui) {
org.apache.logging.log4j.core.Logger logger = (org.apache.logging.log4j.core.Logger) LogManager.getRootLogger();
for (Appender appender : logger.getAppenders().values()) {
// Prevents multiple appender/double logging and removes harmless errors
if ((useGui && appender instanceof TerminalConsoleAppender) || (!useGui && appender instanceof ConsoleAppender)) {
logger.removeAppender(appender);
}
}
}

private static boolean isHeadless() {
try {
Class<?> graphicsEnv = Class.forName("java.awt.GraphicsEnvironment");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,6 @@ public void mouseClicked(MouseEvent e) {
}

private void wrapSystemOutputStreams() {
org.apache.logging.log4j.core.Logger logger = (org.apache.logging.log4j.core.Logger) LogManager.getRootLogger();
for (Appender appender : logger.getAppenders().values()) {
// Prevents multiple appenders/double logging and removes harmless errors
if (appender instanceof TerminalConsoleAppender) {
logger.removeAppender(appender);
}
}
var proxyOutputStream = createProxyOutputStream();
// Override the system output streams
System.setOut(new PrintStream(proxyOutputStream, true));
Expand Down

0 comments on commit 09e5b67

Please sign in to comment.