Skip to content

Commit 66d7b8f

Browse files
Copilotmgaffigan
authored andcommitted
fix: migrate legacy dir.logs log4j property
Fixes a crash on boot caused by dir.logs set in log4j2.properties, which is not supported in log4j2. Cannot use normal migration infra since log4j is initialized early in the boot processs (before classpath is fully set up). Issue: #267 Signed-off-by: Mitch Gaffigan <mgaffigan@users.noreply.github.com>
1 parent bfe3348 commit 66d7b8f

2 files changed

Lines changed: 61 additions & 0 deletions

File tree

server/src/com/mirth/connect/server/launcher/MirthLauncher.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.io.IOException;
1616
import java.net.URL;
1717
import java.net.URLClassLoader;
18+
import java.nio.charset.StandardCharsets;
1819
import java.util.ArrayList;
1920
import java.util.Collection;
2021
import 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);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.mirth.connect.server.launcher;
2+
3+
import static org.junit.Assert.assertFalse;
4+
import static org.junit.Assert.assertTrue;
5+
6+
import java.io.File;
7+
import java.lang.reflect.Method;
8+
import java.nio.charset.StandardCharsets;
9+
import java.nio.file.Files;
10+
11+
import org.junit.Test;
12+
13+
public class MirthLauncherTest {
14+
15+
@Test
16+
public void testSanitizeLog4jRemovesDirLogs() throws Exception {
17+
File file = File.createTempFile("log4j2", ".properties");
18+
file.deleteOnExit();
19+
Files.writeString(file.toPath(), String.join(System.lineSeparator(),
20+
"rootLogger = ERROR,stdout,fout",
21+
"dir.logs = logs",
22+
"property.log.dir = logs",
23+
"appender.rolling.fileName = ${log.dir}/mirth.log"), StandardCharsets.UTF_8);
24+
25+
Method method = MirthLauncher.class.getDeclaredMethod("sanitizeLog4jConfiguration", File.class);
26+
method.setAccessible(true);
27+
method.invoke(null, file);
28+
29+
String fileContents = Files.readString(file.toPath(), StandardCharsets.UTF_8);
30+
assertFalse(fileContents.contains("dir.logs"));
31+
assertTrue(fileContents.contains("property.log.dir = logs"));
32+
assertTrue(fileContents.contains("appender.rolling.fileName = ${log.dir}/mirth.log"));
33+
}
34+
}

0 commit comments

Comments
 (0)