mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-15 20:51:34 +01:00
Added SLF4J and Log4J2 as logging platform.
This commit is contained in:
parent
de8ec680fb
commit
df253238fc
@ -33,12 +33,19 @@ dependencies {
|
||||
|
||||
api 'com.github.TheMode:CommandBuilder:f893cfbfe4'
|
||||
|
||||
// https://jitpack.io/#Articdive/Jnoise/1.0-SNAPSHOT
|
||||
// https://jitpack.io/#Articdive/Jnoise
|
||||
api 'com.github.Articdive:Jnoise:1.0-SNAPSHOT'
|
||||
|
||||
// https://mvnrepository.com/artifact/org.rocksdb/rocksdbjni
|
||||
api group: 'org.rocksdb', name: 'rocksdbjni', version: '6.8.1'
|
||||
|
||||
// https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core
|
||||
api group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.13.2'
|
||||
|
||||
// SLF4J is the base for logging so we can easily switch to any logging system later on.
|
||||
// https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-slf4j-impl
|
||||
api group: 'org.apache.logging.log4j', name: 'log4j-slf4j-impl', version: '2.13.2'
|
||||
|
||||
api 'net.kyori:text-api:3.0.3'
|
||||
api 'net.kyori:text-serializer-legacy:3.0.3'
|
||||
api 'net.kyori:text-serializer-gson:3.0.3'
|
||||
|
@ -17,13 +17,17 @@ import net.minestom.server.recipe.RecipeManager;
|
||||
import net.minestom.server.registry.RegistryMain;
|
||||
import net.minestom.server.registry.ResourceGatherer;
|
||||
import net.minestom.server.scoreboard.TeamManager;
|
||||
import net.minestom.server.storage.StorageFolder;
|
||||
import net.minestom.server.storage.StorageManager;
|
||||
import net.minestom.server.timer.SchedulerManager;
|
||||
import net.minestom.server.world.Difficulty;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class MinecraftServer {
|
||||
private final static Logger LOGGER = LoggerFactory.getLogger(MinecraftServer.class);
|
||||
|
||||
// Threads
|
||||
public static final String THREAD_NAME_BENCHMARK = "Ms-Benchmark";
|
||||
@ -109,8 +113,7 @@ public class MinecraftServer {
|
||||
try {
|
||||
ResourceGatherer.ensureResourcesArePresent(null); // TODO: provide a way to give a path override, probably via launch arguments?
|
||||
} catch (IOException e) {
|
||||
System.err.println("An error happened during resource gathering. Minestom will attempt to load anyway, but things may not work, and crashes can happen.");
|
||||
e.printStackTrace();
|
||||
LOGGER.error("An error happened during resource gathering. Minestom will attempt to load anyway, but things may not work, and crashes can happen.", e);
|
||||
}
|
||||
RegistryMain.registerBlocks();
|
||||
RegistryMain.registerItems();
|
||||
@ -197,19 +200,23 @@ public class MinecraftServer {
|
||||
}
|
||||
|
||||
public void start(String address, int port, ResponseDataConsumer responseDataConsumer) {
|
||||
LOGGER.info("Starting Minestom server.");
|
||||
MinecraftServer.responseDataConsumer = responseDataConsumer;
|
||||
updateManager.start();
|
||||
nettyServer.start(address, port);
|
||||
LOGGER.info("Minestom server started successfully.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops this server properly (saves if needed, kicking players, etc.)
|
||||
*/
|
||||
public static void stopCleanly() {
|
||||
LOGGER.info("Stopping Minestom server.");
|
||||
updateManager.stop();
|
||||
nettyServer.stop();
|
||||
schedulerManager.shutdown();
|
||||
storageManager.getLoadedFolders().forEach(storageFolder -> storageFolder.close());
|
||||
storageManager.getLoadedFolders().forEach(StorageFolder::close);
|
||||
LOGGER.info("Minestom server stopped successfully.");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,20 +1,28 @@
|
||||
package net.minestom.server.registry;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URL;
|
||||
import java.nio.file.*;
|
||||
import java.nio.file.FileVisitResult;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.SimpleFileVisitor;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Responsible for making sure Minestom has the necessary files to run (notably registry files)
|
||||
*/
|
||||
public class ResourceGatherer {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(ResourceGatherer.class);
|
||||
|
||||
public static final File DATA_FOLDER = new File("./minecraft_data/");
|
||||
private static final File TMP_FOLDER = new File("./.minestom_tmp/");
|
||||
@ -28,35 +36,35 @@ public class ResourceGatherer {
|
||||
if(DATA_FOLDER.exists()) {
|
||||
return;
|
||||
}
|
||||
System.out.println(DATA_FOLDER +" folder does not exist. Minestom will now generate the necessary files.");
|
||||
LOGGER.info(DATA_FOLDER +" folder does not exist. Minestom will now generate the necessary files.");
|
||||
|
||||
if(!TMP_FOLDER.exists() && !TMP_FOLDER.mkdirs()) {
|
||||
throw new IOException("Failed to create tmp folder.");
|
||||
}
|
||||
|
||||
final String version = "1.15.2"; // TODO: Do not hardcode
|
||||
|
||||
System.out.println("Starting download of Minecraft server jar for version "+version+" from Mojang servers...");
|
||||
|
||||
LOGGER.info("Starting download of Minecraft server jar for version " + version + " from Mojang servers...");
|
||||
File minecraftFolder = getMinecraftFolder(minecraftFolderOverride);
|
||||
if(!minecraftFolder.exists()) {
|
||||
throw new IOException("Could not find Minecraft installation folder, attempted location "+minecraftFolder+". If this location is not the correct one, please supply the correct one as argument of ResourceGatherer#ensureResourcesArePresent");
|
||||
}
|
||||
File serverJar = downloadServerJar(minecraftFolder, version);
|
||||
System.out.println("Download complete.");
|
||||
LOGGER.info("Download complete.");
|
||||
|
||||
runDataGenerator(serverJar);
|
||||
|
||||
moveAndCleanup(version);
|
||||
System.out.println("Resource gathering done!");
|
||||
LOGGER.info("Resource gathering done!");
|
||||
}
|
||||
|
||||
private static void moveAndCleanup(String version) throws IOException {
|
||||
Path dataFolderPath = DATA_FOLDER.toPath();
|
||||
Path tmpFolderPath = TMP_FOLDER.toPath();
|
||||
Path generatedFolder = tmpFolderPath.resolve("generated");
|
||||
System.out.println("Data generator successful, removing server jar");
|
||||
Files.delete(tmpFolderPath.resolve("server_"+version+".jar"));
|
||||
System.out.println("Removal successful, now moving data to "+DATA_FOLDER);
|
||||
LOGGER.info("Data generator successful, removing server jar");
|
||||
Files.delete(tmpFolderPath.resolve("server_" + version + ".jar"));
|
||||
LOGGER.info("Removal successful, now moving data to " + DATA_FOLDER);
|
||||
Files.walkFileTree(tmpFolderPath, new SimpleFileVisitor<>() {
|
||||
|
||||
@Override
|
||||
@ -64,7 +72,7 @@ public class ResourceGatherer {
|
||||
Path relativePath = generatedFolder.relativize(dir);
|
||||
if(dir.startsWith(generatedFolder)) { // don't copy logs
|
||||
Path resolvedPath = dataFolderPath.resolve(relativePath);
|
||||
System.out.println("> Creating sub-folder "+relativePath);
|
||||
LOGGER.info("> Creating sub-folder " + relativePath);
|
||||
Files.createDirectories(resolvedPath);
|
||||
}
|
||||
return FileVisitResult.CONTINUE;
|
||||
@ -72,7 +80,7 @@ public class ResourceGatherer {
|
||||
|
||||
@Override
|
||||
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
|
||||
System.out.println("> Deleting folder "+dir);
|
||||
LOGGER.info("> Deleting folder " + dir);
|
||||
Files.delete(dir);
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
@ -82,10 +90,10 @@ public class ResourceGatherer {
|
||||
Path relativePath = generatedFolder.relativize(file);
|
||||
if(file.startsWith(generatedFolder)) { // don't copy logs
|
||||
Path resolvedPath = dataFolderPath.resolve(relativePath);
|
||||
System.out.println("> Moving "+relativePath);
|
||||
LOGGER.info("> Moving " + relativePath);
|
||||
Files.move(file, resolvedPath);
|
||||
} else {
|
||||
System.out.println("> Deleting "+relativePath);
|
||||
LOGGER.info("> Deleting " + relativePath);
|
||||
Files.delete(file);
|
||||
}
|
||||
return FileVisitResult.CONTINUE;
|
||||
@ -96,11 +104,16 @@ public class ResourceGatherer {
|
||||
private static void runDataGenerator(File serverJar) throws IOException {
|
||||
ProcessBuilder dataGenerator = new ProcessBuilder("java", "-cp", serverJar.getName(), "net.minecraft.data.Main", "--all", "--server", "--dev");
|
||||
dataGenerator.directory(TMP_FOLDER);
|
||||
dataGenerator.inheritIO();
|
||||
System.out.println("Now running data generator with options '--dev', '--server', '--all'");
|
||||
System.out.println("Executing: "+dataGenerator.command().stream().collect(Collectors.joining(" ")));
|
||||
System.out.println("Minestom will now wait for it to finish, here's its output:");
|
||||
LOGGER.info("Now running data generator with options '--dev', '--server', '--all'");
|
||||
LOGGER.info("Executing: " + String.join(" ", dataGenerator.command()));
|
||||
LOGGER.info("Minestom will now wait for it to finish, here's its output:");
|
||||
LOGGER.info("");
|
||||
Process dataGeneratorProcess = dataGenerator.start();
|
||||
new BufferedReader(
|
||||
new InputStreamReader(dataGeneratorProcess.getInputStream())
|
||||
).lines().forEach(LOGGER::info);
|
||||
LOGGER.info("");
|
||||
|
||||
try {
|
||||
int resultCode = dataGeneratorProcess.waitFor();
|
||||
if(resultCode != 0) {
|
||||
@ -128,8 +141,8 @@ public class ResourceGatherer {
|
||||
VersionInfo versionInfo = gson.fromJson(fileReader, VersionInfo.class);
|
||||
VersionInfo.DownloadObject serverJarInfo = versionInfo.getDownloadableFiles().get("server");
|
||||
String downloadURL = serverJarInfo.getUrl();
|
||||
|
||||
System.out.println("Found URL, starting download from "+downloadURL+"...");
|
||||
|
||||
LOGGER.info("Found URL, starting download from " + downloadURL + "...");
|
||||
return download(version, downloadURL);
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,8 @@
|
||||
package net.minestom.server.storage;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
@ -7,6 +10,7 @@ import java.util.Map;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class StorageManager {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(StorageManager.class);
|
||||
|
||||
private Supplier<StorageSystem> defaultStorageSystemSupplier = null;
|
||||
|
||||
@ -47,8 +51,9 @@ public class StorageManager {
|
||||
}
|
||||
|
||||
public void defineDefaultStorageSystem(Supplier<StorageSystem> storageSystemSupplier) {
|
||||
if (this.defaultStorageSystemSupplier != null)
|
||||
System.out.println("WARNING: the default StorageSystem is being changed, could lead to issue!");
|
||||
if (this.defaultStorageSystemSupplier != null) {
|
||||
LOGGER.error("The default storage-system has been changed. This could lead to issues!");
|
||||
}
|
||||
this.defaultStorageSystemSupplier = storageSystemSupplier;
|
||||
}
|
||||
}
|
||||
|
14
src/main/resources/log4j2.xml
Normal file
14
src/main/resources/log4j2.xml
Normal file
@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Configuration status="WARN" strict="true" name="Minestom">
|
||||
<Appenders>
|
||||
<Appender type="Console" name="STDOUT">
|
||||
<Layout type="PatternLayout" pattern="[%t] [%d{HH:mm:ss}] %p - %m%n"/>
|
||||
</Appender>
|
||||
</Appenders>
|
||||
|
||||
<Loggers>
|
||||
<Root level="trace">
|
||||
<AppenderRef ref="STDOUT" level="info"/>
|
||||
</Root>
|
||||
</Loggers>
|
||||
</Configuration>
|
Loading…
Reference in New Issue
Block a user