Skip to content

Commit 2061872

Browse files
authored
Fix #175: Add a RunModel property to replace the log4j2.xml file (#247)
1 parent 4117e4b commit 2061872

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

src/main/java/net/neoforged/moddevgradle/dsl/RunModel.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.gradle.api.artifacts.Configuration;
1515
import org.gradle.api.artifacts.dsl.Dependencies;
1616
import org.gradle.api.file.DirectoryProperty;
17+
import org.gradle.api.file.RegularFileProperty;
1718
import org.gradle.api.provider.ListProperty;
1819
import org.gradle.api.provider.MapProperty;
1920
import org.gradle.api.provider.Property;
@@ -240,9 +241,17 @@ public Configuration getAdditionalRuntimeClasspathConfiguration() {
240241

241242
/**
242243
* Changes the games log-level.
244+
*
245+
* <p>Note that this property is ignored if {@link #getLoggingConfigFile()} is set.
243246
*/
244247
public abstract Property<Level> getLogLevel();
245248

249+
/**
250+
* Overrides the {@code log4j2.xml} configuration file.
251+
* If unset, MDG will use a default configuration file with the chosen {@link #getLogLevel() log level}.
252+
*/
253+
public abstract RegularFileProperty getLoggingConfigFile();
254+
246255
/**
247256
* Sets the source set to be used as the main classpath of this run.
248257
* Defaults to the {@code main} source set.

src/main/java/net/neoforged/moddevgradle/internal/ModDevRunWorkflow.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ private static TaskProvider<PrepareRun> setupRunInGradle(
332332
task.getGameDirectory().set(run.getGameDirectory());
333333
task.getVmArgsFile().set(RunUtils.getArgFile(argFileDir, run, RunUtils.RunArgFile.VMARGS));
334334
task.getProgramArgsFile().set(RunUtils.getArgFile(argFileDir, run, RunUtils.RunArgFile.PROGRAMARGS));
335+
task.getLog4jConfigFileOverride().set(run.getLoggingConfigFile());
335336
task.getLog4jConfigFile().set(RunUtils.getArgFile(argFileDir, run, RunUtils.RunArgFile.LOG4J_CONFIG));
336337
task.getRunType().set(run.getType());
337338
task.getRunTypeTemplatesSource().from(runTemplatesFile);

src/main/java/net/neoforged/moddevgradle/internal/PrepareRunOrTest.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.io.IOException;
55
import java.nio.charset.StandardCharsets;
66
import java.nio.file.Files;
7+
import java.nio.file.StandardCopyOption;
78
import java.util.ArrayList;
89
import java.util.LinkedHashMap;
910
import java.util.List;
@@ -55,6 +56,17 @@ abstract class PrepareRunOrTest extends DefaultTask {
5556
@OutputFile
5657
public abstract RegularFileProperty getProgramArgsFile();
5758

59+
/**
60+
* A file to use for the {@code log4j2.xml} config file that will be written.
61+
* If absent, the standard log4j2.xml file produced by {@link RunUtils#writeLog4j2Configuration} will be used.
62+
*/
63+
@InputFile
64+
@Optional
65+
public abstract RegularFileProperty getLog4jConfigFileOverride();
66+
67+
/**
68+
* Where the {@code log4j2.xml} config file will be written.
69+
*/
5870
@OutputFile
5971
@Optional
6072
public abstract RegularFileProperty getLog4jConfigFile();
@@ -239,7 +251,11 @@ private void writeJvmArguments(UserDevRunType runConfig, Map<String, String> add
239251

240252
if (getLog4jConfigFile().isPresent()) {
241253
var log4jConfigFile = getLog4jConfigFile().get().getAsFile();
242-
RunUtils.writeLog4j2Configuration(getGameLogLevel().get(), log4jConfigFile.toPath());
254+
if (getLog4jConfigFileOverride().isPresent()) {
255+
Files.copy(getLog4jConfigFileOverride().get().getAsFile().toPath(), log4jConfigFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
256+
} else {
257+
RunUtils.writeLog4j2Configuration(getGameLogLevel().get(), log4jConfigFile.toPath());
258+
}
243259
lines.add(RunUtils.escapeJvmArg("-Dlog4j2.configurationFile=" + log4jConfigFile.getAbsolutePath()));
244260
}
245261

0 commit comments

Comments
 (0)