NationalSecurityAgency/ghidra

Custom Log4j2 configuration location & loading

aa-software2112 opened this issue · 2 comments

Hello!

I am trying to setup a custom Log4j configuration for my script. I am developing in Eclipse.

The difficulty is that, even if setting the classpath to include the new log4j configuration, or by adding -Dlog4j.configuration to the run configuration, none of these solutions seem to work, and it appears the log file used is:

jar:file:/C:/Users/anthony/GHIDRA/ghidra_11.0.3_PUBLIC/Ghidra/Framework/Generic/lib/Generic.jar!/generic.log4j.xml

Can you give any insights on how I can get my own log4j configuration to work?

We look for this property: log4j.configurationFile

This is the code that reads that system property. If it is set, and the file is on the classpath, then it should get loaded.

        private static URL getLogFileFromSystemProperty() {
		String configString = System.getProperty(LOG4J2_CONFIGURATION_PROPERTY);
		if (configString == null) {
			return null;
		}

		// first see if the given filename is something that is in our classpath
		URL resource = LoggingInitialization.class.getClassLoader().getResource(configString);
		if (resource != null) {
			return resource;
		}

		File configFile = new File(configString);
		if (!configFile.exists()) {
			// maybe it is already in URL form: file://some/file/path
			try {
				URL url = new URL(configString);
				File file = new File(url.toURI());
				if (file.exists()) {
					return url;
				}
			}
			catch (Exception e) {
				// handled below
			}

			// we have to reset the property so that the DOMConfigurator does not use it
			System.setProperty(LOG4J2_CONFIGURATION_PROPERTY, "");
			System.err.println("Log config file does not exist: " + configString);
			return null;
		}

		URI URI = configFile.toURI();
		try {
			return URI.toURL();
		}
		catch (MalformedURLException e) {
			// not sure if this can happen, since we validated that the file already exists
			System.err.println("Unable to find requested log configuration file: " + configString);
			e.printStackTrace();
		}

		return null;
	}

For future reference, if you are setting the VM configuration through

-Dlog4j.configurationFile=file:/C:/Users/.../log4j.xml

Make sure that after the file: you include a forward slash! If this is missing it will not be accepted as a valid URL string. Second, make sure you are running the correct run configuration in Eclipse