Fix exception logging on java.util.logging. Fix getting manifest

This commit is contained in:
Vankka 2023-05-06 20:43:02 +03:00
parent 6b9606ddd7
commit ca40da0c00
No known key found for this signature in database
GPG Key ID: 6E50CB7A29B96AD0
9 changed files with 62 additions and 12 deletions

View File

@ -91,6 +91,11 @@ public class DiscordSRVBukkitBootstrap extends BukkitBootstrap implements IBoots
return getClasspathAppender(); return getClasspathAppender();
} }
@Override
public ClassLoader classLoader() {
return getClassLoader();
}
@Override @Override
public LifecycleManager lifecycleManager() { public LifecycleManager lifecycleManager() {
return lifecycleManager; return lifecycleManager;

View File

@ -68,6 +68,11 @@ public class DiscordSRVBungeeBootstrap extends BungeeBootstrap implements IBoots
return getClasspathAppender(); return getClasspathAppender();
} }
@Override
public ClassLoader classLoader() {
return getClassLoader();
}
@Override @Override
public LifecycleManager lifecycleManager() { public LifecycleManager lifecycleManager() {
return lifecycleManager; return lifecycleManager;

View File

@ -33,16 +33,16 @@ import com.discordsrv.common.command.game.GameCommandModule;
import com.discordsrv.common.component.ComponentFactory; import com.discordsrv.common.component.ComponentFactory;
import com.discordsrv.common.config.connection.ConnectionConfig; import com.discordsrv.common.config.connection.ConnectionConfig;
import com.discordsrv.common.config.connection.UpdateConfig; import com.discordsrv.common.config.connection.UpdateConfig;
import com.discordsrv.common.config.main.linking.LinkedAccountConfig;
import com.discordsrv.common.config.main.MainConfig; import com.discordsrv.common.config.main.MainConfig;
import com.discordsrv.common.config.main.linking.LinkedAccountConfig;
import com.discordsrv.common.config.manager.ConnectionConfigManager; import com.discordsrv.common.config.manager.ConnectionConfigManager;
import com.discordsrv.common.config.manager.MainConfigManager; import com.discordsrv.common.config.manager.MainConfigManager;
import com.discordsrv.common.debug.data.VersionInfo; import com.discordsrv.common.debug.data.VersionInfo;
import com.discordsrv.common.dependency.DiscordSRVDependencyManager; import com.discordsrv.common.dependency.DiscordSRVDependencyManager;
import com.discordsrv.common.discord.api.DiscordAPIEventModule; import com.discordsrv.common.discord.api.DiscordAPIEventModule;
import com.discordsrv.common.discord.api.DiscordAPIImpl; import com.discordsrv.common.discord.api.DiscordAPIImpl;
import com.discordsrv.common.discord.connection.jda.JDAConnectionManager;
import com.discordsrv.common.discord.connection.details.DiscordConnectionDetailsImpl; import com.discordsrv.common.discord.connection.details.DiscordConnectionDetailsImpl;
import com.discordsrv.common.discord.connection.jda.JDAConnectionManager;
import com.discordsrv.common.event.bus.EventBusImpl; import com.discordsrv.common.event.bus.EventBusImpl;
import com.discordsrv.common.exception.StorageException; import com.discordsrv.common.exception.StorageException;
import com.discordsrv.common.function.CheckedFunction; import com.discordsrv.common.function.CheckedFunction;
@ -91,6 +91,7 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.net.URL; import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.*; import java.util.*;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
@ -208,10 +209,18 @@ public abstract class AbstractDiscordSRV<B extends IBootstrap, C extends MainCon
} }
protected URL getManifest() { protected URL getManifest() {
return getClass().getClassLoader().getResource(JarFile.MANIFEST_NAME); ClassLoader classLoader = bootstrap.classLoader();
if (classLoader instanceof URLClassLoader) {
return ((URLClassLoader) classLoader).findResource(JarFile.MANIFEST_NAME);
}
return classLoader.getResource(JarFile.MANIFEST_NAME);
} }
private void readManifest() { private void readManifest() {
String version = bootstrap.getClass().getPackage().getImplementationVersion();
String gitCommit = null, gitBranch = null, buildTime = null;
try { try {
URL url = getManifest(); URL url = getManifest();
if (url == null) { if (url == null) {
@ -222,21 +231,22 @@ public abstract class AbstractDiscordSRV<B extends IBootstrap, C extends MainCon
Manifest manifest = new Manifest(inputStream); Manifest manifest = new Manifest(inputStream);
Attributes attributes = manifest.getMainAttributes(); Attributes attributes = manifest.getMainAttributes();
String version = readAttribute(attributes, "Implementation-Version"); if (version == null) {
version = readAttribute(attributes, "Implementation-Version");
if (version == null) { if (version == null) {
logger().error("Failed to get version from manifest"); logger().error("Failed to get version from manifest");
} }
}
versionInfo = new VersionInfo( gitCommit = readAttribute(attributes, "Git-Commit");
version, gitBranch = readAttribute(attributes, "Git-Branch");
readAttribute(attributes, "Git-Commit"), buildTime = readAttribute(attributes, "Build-Time");
readAttribute(attributes, "Git-Branch"),
readAttribute(attributes, "Build-Time")
);
} }
} catch (IOException e) { } catch (IOException e) {
logger().error("Failed to read manifest", e); logger().error("Failed to read manifest", e);
} }
versionInfo = new VersionInfo(version, gitCommit, gitBranch, buildTime);
} }
private String readAttribute(Attributes attributes, String key) { private String readAttribute(Attributes attributes, String key) {

View File

@ -27,6 +27,7 @@ public interface IBootstrap {
Logger logger(); Logger logger();
ClasspathAppender classpathAppender(); ClasspathAppender classpathAppender();
ClassLoader classLoader();
LifecycleManager lifecycleManager(); LifecycleManager lifecycleManager();
Path dataDirectory(); Path dataDirectory();

View File

@ -24,6 +24,7 @@ import com.discordsrv.common.logging.Logger;
import com.discordsrv.common.logging.backend.LogFilter; import com.discordsrv.common.logging.backend.LogFilter;
import com.discordsrv.common.logging.backend.LoggingBackend; import com.discordsrv.common.logging.backend.LoggingBackend;
import org.apache.commons.collections4.bidimap.DualHashBidiMap; import org.apache.commons.collections4.bidimap.DualHashBidiMap;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -62,7 +63,15 @@ public class JavaLoggerImpl implements Logger, LoggingBackend {
public void log(@Nullable String loggerName, @NotNull LogLevel level, @Nullable String message, @Nullable Throwable throwable) { public void log(@Nullable String loggerName, @NotNull LogLevel level, @Nullable String message, @Nullable Throwable throwable) {
Level logLevel = LEVELS.getKey(level); Level logLevel = LEVELS.getKey(level);
if (logLevel != null) { if (logLevel != null) {
logger.log(logLevel, message, throwable); List<String> contents = new ArrayList<>(2);
if (message != null) {
contents.add(message);
}
if (throwable != null) {
// Exceptions aren't always logged correctly by the logger itself
contents.add(ExceptionUtils.getStackTrace(throwable));
}
logger.log(logLevel, String.join("\n", contents));
} }
} }

View File

@ -65,6 +65,11 @@ public class MockDiscordSRV extends AbstractDiscordSRV<IBootstrap, MainConfig, C
return null; return null;
} }
@Override
public ClassLoader classLoader() {
return MockDiscordSRV.class.getClassLoader();
}
@Override @Override
public LifecycleManager lifecycleManager() { public LifecycleManager lifecycleManager() {
return null; return null;

View File

@ -54,6 +54,11 @@ public class MockDiscordSRV extends AbstractDiscordSRV<IBootstrap, MainConfig, C
return null; return null;
} }
@Override
public ClassLoader classLoader() {
return null;
}
@Override @Override
public LifecycleManager lifecycleManager() { public LifecycleManager lifecycleManager() {
return null; return null;

View File

@ -99,6 +99,11 @@ public class DiscordSRVSpongeBootstrap extends AbstractBootstrap implements ISpo
return classpathAppender; return classpathAppender;
} }
@Override
public ClassLoader classLoader() {
return getClassLoader();
}
@Override @Override
public LifecycleManager lifecycleManager() { public LifecycleManager lifecycleManager() {
return lifecycleManager; return lifecycleManager;

View File

@ -95,6 +95,11 @@ public class DiscordSRVVelocityBootstrap implements IBootstrap {
return classpathAppender; return classpathAppender;
} }
@Override
public ClassLoader classLoader() {
return getClass().getClassLoader();
}
@Override @Override
public LifecycleManager lifecycleManager() { public LifecycleManager lifecycleManager() {
return lifecycleManager; return lifecycleManager;