Skip to content

Commit

Permalink
Add support for setting the log-level of runs.
Browse files Browse the repository at this point in the history
  • Loading branch information
shartte committed May 26, 2024
1 parent 97bd632 commit ec3a590
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 21 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ neoForge {
// client()
// data()
// server()
// Optionally set the log-level used by the game
logLevel = org.slf4j.event.Level.INFO
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/net/neoforged/moddevgradle/dsl/RunModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.gradle.api.provider.MapProperty;
import org.gradle.api.provider.Property;
import org.gradle.api.provider.SetProperty;
import org.slf4j.event.Level;

import javax.inject.Inject;

Expand Down Expand Up @@ -38,6 +39,8 @@ public RunModel(String name, Project project) {
configuration.setCanBeResolved(false);
configuration.setCanBeConsumed(false);
});

getLogLevel().convention(Level.DEBUG);
}

@Override
Expand Down Expand Up @@ -86,6 +89,8 @@ public Configuration getAdditionalRuntimeClasspathConfiguration() {

public abstract DependencyCollector getAdditionalRuntimeClasspath();

public abstract Property<Level> getLogLevel();

@Override
public String toString() {
return "Run[" + getName() + "]";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.jetbrains.gradle.ext.ProjectSettings;
import org.jetbrains.gradle.ext.RunConfigurationContainer;
import org.jetbrains.gradle.ext.TaskTriggersConfig;
import org.slf4j.event.Level;

import java.io.File;
import java.net.URI;
Expand Down Expand Up @@ -362,6 +363,7 @@ public void apply(Project project) {
}));
task.getMainClass().set(run.getMainClass());
task.getProgramArguments().set(run.getProgramArguments());
task.getGameLogLevel().set(run.getLogLevel());
});
prepareRunTasks.put(run, prepareRunTask);
idePostSyncTask.configure(task -> task.dependsOn(prepareRunTask));
Expand Down Expand Up @@ -486,6 +488,7 @@ private void setupTesting(Project project,
task.getModules().from(neoForgeModDevModules);
task.getLegacyClasspathFile().set(writeLcpTask.get().getLegacyClasspathFile());
task.getAssetProperties().set(downloadAssets.flatMap(DownloadAssetsTask::getAssetPropertiesFile));
task.getGameLogLevel().set(Level.INFO);
});
idePostSyncTask.configure(task -> task.dependsOn(prepareRunTask));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@
import org.gradle.api.file.ConfigurableFileCollection;
import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.file.RegularFileProperty;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.Classpath;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.InputFile;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.OutputFile;
import org.gradle.api.tasks.PathSensitive;
import org.gradle.api.tasks.PathSensitivity;
import org.gradle.api.tasks.TaskAction;
import org.slf4j.event.Level;

import javax.inject.Inject;
import java.io.File;
Expand Down Expand Up @@ -52,6 +55,9 @@ abstract class PrepareArgsForTesting extends DefaultTask {
@InputFiles
abstract ConfigurableFileCollection getModules();

@Input
public abstract Property<Level> getGameLogLevel();

@Inject
public PrepareArgsForTesting() {
}
Expand Down Expand Up @@ -92,12 +98,7 @@ private void writeJvmArguments(File runDir, UserDevRunType runConfig) throws IOE
lines.addAll(getInterpolatedJvmArgs(runConfig));

// Write log4j2 configuration file
File log4j2xml;
try {
log4j2xml = RunUtils.writeLog4j2Configuration(runDir);
} catch (IOException e) {
throw new RuntimeException(e);
}
File log4j2xml = RunUtils.writeLog4j2Configuration(getGameLogLevel().get(), runDir);

lines.add(RunUtils.escapeJvmArg("-Dlog4j2.configurationFile=" + log4j2xml.getAbsolutePath()));
for (var prop : runConfig.props().entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.gradle.api.tasks.PathSensitive;
import org.gradle.api.tasks.PathSensitivity;
import org.gradle.api.tasks.TaskAction;
import org.slf4j.event.Level;

import javax.inject.Inject;
import java.io.File;
Expand Down Expand Up @@ -76,6 +77,9 @@ abstract class PrepareRunForIde extends DefaultTask {
@Input
public abstract ListProperty<String> getProgramArguments();

@Input
public abstract Property<Level> getGameLogLevel();

@Inject
public PrepareRunForIde() {
}
Expand Down Expand Up @@ -127,12 +131,7 @@ private void writeJvmArguments(File runDir, UserDevRunType runConfig) throws IOE
}

// Write log4j2 configuration file
File log4j2xml;
try {
log4j2xml = RunUtils.writeLog4j2Configuration(runDir);
} catch (IOException e) {
throw new RuntimeException(e);
}
File log4j2xml = RunUtils.writeLog4j2Configuration(getGameLogLevel().get(), runDir);

lines.add(RunUtils.escapeJvmArg("-Dlog4j2.configurationFile=" + log4j2xml.getAbsolutePath()));
for (var prop : runConfig.props().entrySet()) {
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/net/neoforged/moddevgradle/internal/RunUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.gradle.process.CommandLineArgumentProvider;
import org.intellij.lang.annotations.Language;
import org.jetbrains.annotations.Nullable;
import org.slf4j.event.Level;
import org.xml.sax.InputSource;

import javax.inject.Inject;
Expand Down Expand Up @@ -74,7 +75,7 @@ public static AssetProperties loadAssetProperties(File file) {
);
}

public static File writeLog4j2Configuration(File runDir) throws IOException {
public static File writeLog4j2Configuration(Level rootLevel, File runDir) throws IOException {
var log4j2Xml = new File(runDir, "log4j2.xml");

Files.writeString(log4j2Xml.toPath(), """
Expand All @@ -94,9 +95,9 @@ public static File writeLog4j2Configuration(File runDir) throws IOException {
<MarkerFilter marker="REGISTRYDUMP" onMatch="${sys:forge.logging.marker.registrydump:-DENY}" onMismatch="NEUTRAL"/>
<MarkerFilter marker="SPLASH" onMatch="${sys:forge.logging.marker.splash:-DENY}" onMismatch="NEUTRAL"/>
<MarkerFilter marker="RESOURCE-CACHE" onMatch="${sys:forge.logging.marker.resource.cache:-DENY}" onMismatch="NEUTRAL"/>
<MarkerFilter marker="FORGEMOD" onMatch="${sys:forge.logging.marker.forgemod:-ACCEPT}" onMismatch="NEUTRAL"/>
<MarkerFilter marker="LOADING" onMatch="${sys:forge.logging.marker.loading:-ACCEPT}" onMismatch="NEUTRAL"/>
<MarkerFilter marker="CORE" onMatch="${sys:forge.logging.marker.core:-ACCEPT}" onMismatch="NEUTRAL"/>
<MarkerFilter marker="FORGEMOD" onMatch="${sys:forge.logging.marker.forgemod:-NEUTRAL}" onMismatch="NEUTRAL"/>
<MarkerFilter marker="LOADING" onMatch="${sys:forge.logging.marker.loading:-NEUTRAL}" onMismatch="NEUTRAL"/>
<MarkerFilter marker="CORE" onMatch="${sys:forge.logging.marker.core:-NEUTRAL}" onMismatch="NEUTRAL"/>
</filters>
<Appenders>
<Console name="Console">
Expand Down Expand Up @@ -148,16 +149,15 @@ public static File writeLog4j2Configuration(File runDir) throws IOException {
</filters>
</Logger>
<Root level="debug">
<AppenderRef ref="Console" level="${sys:forge.logging.console.level:-debug}"/>
<Root level="$ROOTLEVEL$">
<AppenderRef ref="Console" />
<AppenderRef ref="ServerGuiConsole" level="${sys:forge.logging.console.level:-info}"/>
<AppenderRef ref="File" level="${sys:forge.logging.file.level:-info}"/>
<AppenderRef ref="DebugFile" level="${sys:forge.logging.debugFile.level:-debug}"/>
<AppenderRef ref="DebugFile" />
</Root>
</Loggers>
</Configuration>
""");
""".replace("$ROOTLEVEL$", rootLevel.name()));

return log4j2Xml;
}
Expand Down
3 changes: 3 additions & 0 deletions testproject/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ neoForge {
version = project.neoforge_version

runs {
configureEach {
logLevel = org.slf4j.event.Level.INFO
}
client {
client()
}
Expand Down

0 comments on commit ec3a590

Please sign in to comment.