Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-1607 - Logging - simplified specifying of logging config file to fix error messages

dragonmacher81a8691ec4d5
diff --git a/Ghidra/Features/Base/build.gradle b/Ghidra/Features/Base/build.gradle
index 6d3c5ad8a3..253422acbb 100644
--- a/Ghidra/Features/Base/build.gradle
+++ b/Ghidra/Features/Base/build.gradle
@@ -53,6 +53,9 @@ dependencies {
runtimeOnly "org.slf4j:slf4j-nop:1.7.25"
+ // use this if you want slf4j log messages sent to log4j
+ // runtimeOnly "org.apache.logging.log4j:log4j-slf4j-impl:2.16.0"
+
compileOnly "junit:junit:4.12"
// These have abstract test classes and stubs needed by this module
diff --git a/Ghidra/Features/GraphServices/build.gradle b/Ghidra/Features/GraphServices/build.gradle
index 13d2a6aba4..ba3cf21bfc 100644
--- a/Ghidra/Features/GraphServices/build.gradle
+++ b/Ghidra/Features/GraphServices/build.gradle
@@ -35,14 +35,6 @@ dependencies {
// not using jgrapht-io code that depends on antlr, so exclude antlr
api ("org.jgrapht:jgrapht-io:1.5.1") { exclude group: "org.antlr", module: "antlr4-runtime" }
- runtimeOnly "org.slf4j:slf4j-api:1.7.25"
-
- // use this if you want no slf4j log messages
- runtimeOnly "org.slf4j:slf4j-nop:1.7.25"
-
- // use this if you want slf4j log messages sent to log4j
- // runtimeOnly "org.apache.logging.log4j:log4j-slf4j-impl:2.16.0"
-
runtimeOnly "org.jheaps:jheaps:0.13"
helpPath project(path: ":Base", configuration: 'helpPath')
diff --git a/Ghidra/Framework/Generic/src/main/java/ghidra/framework/LoggingInitialization.java b/Ghidra/Framework/Generic/src/main/java/ghidra/framework/LoggingInitialization.java
index ee297ba12a..d73cf3c449 100644
--- a/Ghidra/Framework/Generic/src/main/java/ghidra/framework/LoggingInitialization.java
+++ b/Ghidra/Framework/Generic/src/main/java/ghidra/framework/LoggingInitialization.java
@@ -28,7 +28,6 @@ import resources.ResourceManager;
public class LoggingInitialization {
- private static final String LOG4J_CONFIGURATION_PROPERTY = "log4j.configuration";
private static final String LOG4J2_CONFIGURATION_PROPERTY = "log4j.configurationFile";
private static final String PRODUCTION_LOGGING_CONFIGURATION_FILE = "generic.log4j.xml";
@@ -44,28 +43,37 @@ public class LoggingInitialization {
return;
}
- URL resource = getLoggingConfigFileUrl();
- if (resource != null) {
- try {
- LoggerContext context = (LoggerContext) LogManager.getContext(false);
- context.setConfigLocation(resource.toURI());
-
- // make future, unresolved contexts (e.g. from OSGi bundles) use this configuration
- System.setProperty(LOG4J2_CONFIGURATION_PROPERTY, resource.toURI().toString());
- }
- catch (URISyntaxException e) {
- Msg.error(LoggingInitialization.class, "Unable to convert URL to URI", e);
- }
- }
+ URL configFileUrl = installConfigFile();
Msg.setErrorLogger(new Log4jErrorLogger());
String configFilename =
- (resource == null) ? "<no config file found>" : resource.toExternalForm();
+ (configFileUrl == null) ? "<no config file found>" : configFileUrl.toExternalForm();
Msg.info(LoggingInitialization.class, "Using log config file: " + configFilename);
Msg.info(LoggingInitialization.class, "Using log file: " + APPLICATION_LOG_FILE);
INITIALIZED = true;
}
+ private static URL installConfigFile() {
+ URL configFileUrl = getLoggingConfigFileUrl();
+ if (configFileUrl == null) {
+ return null;
+ }
+
+ try {
+
+ // Ensure this property is set. Some code paths set the property, but some do not.
+ System.setProperty(LOG4J2_CONFIGURATION_PROPERTY, configFileUrl.toURI().toString());
+
+ // force the log system to initialize
+ LogManager.getContext(false);
+ return configFileUrl;
+ }
+ catch (URISyntaxException e) {
+ Msg.error(LoggingInitialization.class, "Unable to convert URL to URI", e);
+ return null;
+ }
+ }
+
private static URL getLoggingConfigFileUrl() {
URL resource = getLogFileFromSystemProperty();
if (resource != null) {
@@ -73,6 +81,10 @@ public class LoggingInitialization {
}
// no system property resource defined...use one of our defaults
+ return getDefaultLoggingConfigFileUrl();
+ }
+
+ private static URL getDefaultLoggingConfigFileUrl() {
String loggingConfigFilename = PRODUCTION_LOGGING_CONFIGURATION_FILE;
if (SystemUtilities.isInDevelopmentMode()) {
loggingConfigFilename = DEVELOPMENT_LOGGING_CONFIGURATION_FILE;
@@ -82,7 +94,7 @@ public class LoggingInitialization {
}
private static URL getLogFileFromSystemProperty() {
- String configString = System.getProperty(LOG4J_CONFIGURATION_PROPERTY);
+ String configString = System.getProperty(LOG4J2_CONFIGURATION_PROPERTY);
if (configString == null) {
return null;
}
@@ -98,7 +110,6 @@ public class LoggingInitialization {
// 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;
@@ -109,7 +120,7 @@ public class LoggingInitialization {
}
// we have to reset the property so that the DOMConfigurator does not use it
- System.setProperty(LOG4J_CONFIGURATION_PROPERTY, "");
+ System.setProperty(LOG4J2_CONFIGURATION_PROPERTY, "");
System.err.println("Log config file does not exist: " + configString);
return null;
}
@@ -129,6 +140,7 @@ public class LoggingInitialization {
/**
* Returns the default file used for logging messages.
+ * @return the file
*/
public synchronized static File getApplicationLogFile() {
if (APPLICATION_LOG_FILE == null) {
@@ -141,8 +153,8 @@ public class LoggingInitialization {
}
/**
- * Use this to override the default application log file, before you
- * initialize the logging system.
+ * Use this to override the default application log file, before you initialize the logging
+ * system.
*
* @param file The file to use as the application log file
*/
@@ -156,9 +168,9 @@ public class LoggingInitialization {
}
APPLICATION_LOG_FILE = file;
- // Need to set the system property that the log4j2 configuration reads in
- // order to determine the log file name. Once that's set, the log
- // configuration must be 'kicked' to pick up the change.
+ // Need to set the system property that the log4j2 configuration reads in order to
+ // determine the log file name. Once that's set, the log configuration must be 'kicked' to
+ // pick up the change.
System.setProperty("logFilename", file.getAbsolutePath());
if (INITIALIZED) {
((LoggerContext) LogManager.getContext(false)).reconfigure();
@@ -167,6 +179,7 @@ public class LoggingInitialization {
/**
* Returns the default file used for logging messages.
+ * @return the file
*/
public synchronized static File getScriptLogFile() {
if (SCRIPT_LOG_FILE == null) {
@@ -192,9 +205,9 @@ public class LoggingInitialization {
}
SCRIPT_LOG_FILE = file;
- // Need to set the system property that the log4j2 configuration reads in
- // order to determine the script log file name. Once that's set, the log
- // configuration must be 'kicked' to pick up the change.
+ // Need to set the system property that the log4j2 configuration reads in order to
+ // determine the script log file name. Once that's set, the log configuration must be
+ // 'kicked' to pick up the change.
System.setProperty("scriptLogFilename", file.getAbsolutePath());
if (INITIALIZED) {
diff --git a/Ghidra/Framework/Generic/src/main/resources/log4j2.xml b/Ghidra/Framework/Generic/src/main/resources/log4j2.xml
deleted file mode 100644
index fcc05f6784..0000000000
--- a/Ghidra/Framework/Generic/src/main/resources/log4j2.xml
+++ /dev/null
@@ -1,15 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-
-<!--
- Dummy log4j2 configuration file provided so that we will not see the following
- error message on startup:
-
- "ERROR StatusLogger No log4j2 configuration file found. Using default configuration:
- logging only errors to the console"
-
- Log4j2 looks for a file named "log4j2.xml" on startup; if it's not provided this error is
- generated. We never use a file by this name but the error message is disconcerting; this
- seems to be the best way to ensure that we never see it.
- -->
-<Configuration xmlns:xi="http://www.w3.org/2001/XInclude">
-</Configuration>