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();
}
@Override
public ClassLoader classLoader() {
return getClassLoader();
}
@Override
public LifecycleManager lifecycleManager() {
return lifecycleManager;

View File

@ -68,6 +68,11 @@ public class DiscordSRVBungeeBootstrap extends BungeeBootstrap implements IBoots
return getClasspathAppender();
}
@Override
public ClassLoader classLoader() {
return getClassLoader();
}
@Override
public LifecycleManager 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.config.connection.ConnectionConfig;
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.linking.LinkedAccountConfig;
import com.discordsrv.common.config.manager.ConnectionConfigManager;
import com.discordsrv.common.config.manager.MainConfigManager;
import com.discordsrv.common.debug.data.VersionInfo;
import com.discordsrv.common.dependency.DiscordSRVDependencyManager;
import com.discordsrv.common.discord.api.DiscordAPIEventModule;
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.jda.JDAConnectionManager;
import com.discordsrv.common.event.bus.EventBusImpl;
import com.discordsrv.common.exception.StorageException;
import com.discordsrv.common.function.CheckedFunction;
@ -91,6 +91,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Path;
import java.util.*;
import java.util.concurrent.CompletableFuture;
@ -208,10 +209,18 @@ public abstract class AbstractDiscordSRV<B extends IBootstrap, C extends MainCon
}
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() {
String version = bootstrap.getClass().getPackage().getImplementationVersion();
String gitCommit = null, gitBranch = null, buildTime = null;
try {
URL url = getManifest();
if (url == null) {
@ -222,21 +231,22 @@ public abstract class AbstractDiscordSRV<B extends IBootstrap, C extends MainCon
Manifest manifest = new Manifest(inputStream);
Attributes attributes = manifest.getMainAttributes();
String version = readAttribute(attributes, "Implementation-Version");
if (version == null) {
version = readAttribute(attributes, "Implementation-Version");
if (version == null) {
logger().error("Failed to get version from manifest");
}
}
versionInfo = new VersionInfo(
version,
readAttribute(attributes, "Git-Commit"),
readAttribute(attributes, "Git-Branch"),
readAttribute(attributes, "Build-Time")
);
gitCommit = readAttribute(attributes, "Git-Commit");
gitBranch = readAttribute(attributes, "Git-Branch");
buildTime = readAttribute(attributes, "Build-Time");
}
} catch (IOException e) {
logger().error("Failed to read manifest", e);
}
versionInfo = new VersionInfo(version, gitCommit, gitBranch, buildTime);
}
private String readAttribute(Attributes attributes, String key) {

View File

@ -27,6 +27,7 @@ public interface IBootstrap {
Logger logger();
ClasspathAppender classpathAppender();
ClassLoader classLoader();
LifecycleManager lifecycleManager();
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.LoggingBackend;
import org.apache.commons.collections4.bidimap.DualHashBidiMap;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.jetbrains.annotations.NotNull;
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) {
Level logLevel = LEVELS.getKey(level);
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;
}
@Override
public ClassLoader classLoader() {
return MockDiscordSRV.class.getClassLoader();
}
@Override
public LifecycleManager lifecycleManager() {
return null;

View File

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

View File

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

View File

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