1515import java .io .IOException ;
1616import java .net .URL ;
1717import java .net .URLClassLoader ;
18+ import java .nio .charset .StandardCharsets ;
1819import java .util .ArrayList ;
1920import java .util .Collection ;
2021import java .util .List ;
@@ -40,11 +41,13 @@ public class MirthLauncher {
4041 private static final String EXTENSIONS_DIR = "./extensions" ;
4142 private static final String SERVER_LAUNCHER_LIB_DIR = "./server-launcher-lib" ;
4243 private static final String MIRTH_PROPERTIES_FILE = "./conf/mirth.properties" ;
44+ private static final String LOG4J_PROPERTIES_FILE = "./conf/log4j2.properties" ;
4345 private static final String PROPERTY_APP_DATA_DIR = "dir.appdata" ;
4446 private static final String PROPERTY_INCLUDE_CUSTOM_LIB = "server.includecustomlib" ;
4547 private static final String [] LOG4J_JAR_FILES = { "./server-lib/log4j/log4j-core-2.25.3.jar" ,
4648 "./server-lib/log4j/log4j-api-2.25.3.jar" ,
4749 "./server-lib/log4j/log4j-1.2-api-2.25.3.jar" };
50+ private static final String INVALID_LOG4J_PROPERTY = "dir.logs" ;
4851
4952 private static String appDataDir = null ;
5053
@@ -53,6 +56,8 @@ public class MirthLauncher {
5356 public static void main (String [] args ) {
5457 JarFile mirthClientCoreJarFile = null ;
5558 try {
59+ sanitizeLog4jConfiguration (new File (LOG4J_PROPERTIES_FILE ));
60+
5661 List <URL > classpathUrls = new ArrayList <>();
5762 // Always add log4j
5863 for (String log4jJar : LOG4J_JAR_FILES ) {
@@ -128,6 +133,28 @@ public static void main(String[] args) {
128133 }
129134 }
130135
136+ private static void sanitizeLog4jConfiguration (File propertiesFile ) {
137+ if (!propertiesFile .exists () || !propertiesFile .isFile ()) {
138+ return ;
139+ }
140+
141+ try {
142+ List <String > lines = FileUtils .readLines (propertiesFile , StandardCharsets .UTF_8 );
143+ if (lines .removeIf (MirthLauncher ::isInvalidLog4jPropertyLine )) {
144+ FileUtils .writeLines (propertiesFile , StandardCharsets .UTF_8 .name (), lines , System .lineSeparator (), false );
145+ }
146+ } catch (IOException e ) {
147+ System .err .println ("Failed to sanitize Log4j configuration: " + propertiesFile .getAbsolutePath ());
148+ e .printStackTrace ();
149+ }
150+ }
151+
152+ private static boolean isInvalidLog4jPropertyLine (String line ) {
153+ String trimmedLine = line .trim ();
154+ int equalsIndex = trimmedLine .indexOf ('=' );
155+ return equalsIndex >= 0 && trimmedLine .substring (0 , equalsIndex ).trim ().equals (INVALID_LOG4J_PROPERTY );
156+ }
157+
131158 // if we have an uninstall file, uninstall the listed extensions
132159 private static void uninstallPendingExtensions () throws Exception {
133160 File extensionsDir = new File (EXTENSIONS_DIR );
0 commit comments