Skip to content

Commit

Permalink
Check for explicitly set logger config before overwriting it (#70)
Browse files Browse the repository at this point in the history
This change allows users to utilize the `log4j2.configurationFile`
system property for configuring the logging context to their liking.

This property is read by log4j2 to find the configuration file it should
load during initialization. Previously, setting this property only
affected logging output during the boot stage (anything before launching
Minecraft itself) due to the unconditional logger context reconfiguration.

Tying this logic to the already existing system property used by log4j2
seemed the most logical, considering that this property is the only
approachable way for specifying an alternative configuration in the
first place.
  • Loading branch information
niklaswimmer authored Jan 16, 2024
1 parent 937f14a commit 2713b89
Showing 1 changed file with 12 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,26 @@ public void configureTransformationClassLoader(final ITransformingClassLoaderBui
}

protected String[] preLaunch(String[] arguments, ModuleLayer layer) {
// do not overwrite the logging configuration if the user explicitly set another one
if (System.getProperty("log4j2.configurationFile") == null) {
overwriteLoggingConfiguration(layer);
}

return arguments;
}

/**
* Forces the log4j2 logging context to use the configuration shipped with fml_loader.
*/
private void overwriteLoggingConfiguration(final ModuleLayer layer) {
URI uri;
try (var reader = layer.configuration().findModule("fml_loader").orElseThrow().reference().open()) {
uri = reader.find("log4j2.xml").orElseThrow();
} catch (IOException e) {
throw new RuntimeException(e);
}

// Force the log4j2 configuration to be loaded from fmlloader
Configurator.reconfigure(ConfigurationFactory.getInstance().getConfiguration(LoggerContext.getContext(), ConfigurationSource.fromUri(uri)));

return arguments;
}

protected final String[] getLegacyClasspath() {
Expand Down

0 comments on commit 2713b89

Please sign in to comment.