Skip to content

Commit cf36b70

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 59674d0 commit cf36b70

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
importjava.io.IOException;
1616
importjava.net.URL;
1717
importjava.net.URLClassLoader;
18+
importjava.nio.charset.StandardCharsets;
1819
importjava.util.ArrayList;
1920
importjava.util.Collection;
2021
importjava.util.List;
@@ -40,11 +41,13 @@ public class MirthLauncher {
4041
privatestaticfinalStringEXTENSIONS_DIR = "./extensions";
4142
privatestaticfinalStringSERVER_LAUNCHER_LIB_DIR = "./server-launcher-lib";
4243
privatestaticfinalStringMIRTH_PROPERTIES_FILE = "./conf/mirth.properties";
44+
privatestaticfinalStringLOG4J_PROPERTIES_FILE = "./conf/log4j2.properties";
4345
privatestaticfinalStringPROPERTY_APP_DATA_DIR = "dir.appdata";
4446
privatestaticfinalStringPROPERTY_INCLUDE_CUSTOM_LIB = "server.includecustomlib";
4547
privatestaticfinalString[] 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+
privatestaticfinalStringINVALID_LOG4J_PROPERTY = "dir.logs";
4851

4952
privatestaticStringappDataDir = null;
5053

@@ -53,6 +56,8 @@ public class MirthLauncher {
5356
publicstaticvoidmain(String[] args) {
5457
JarFilemirthClientCoreJarFile = null;
5558
try {
59+
sanitizeLog4jConfiguration(newFile(LOG4J_PROPERTIES_FILE));
60+
5661
List<URL> classpathUrls = newArrayList<>();
5762
// Always add log4j
5863
for (Stringlog4jJar : LOG4J_JAR_FILES) {
@@ -128,6 +133,28 @@ public static void main(String[] args) {
128133
}
129134
}
130135

136+
privatestaticvoidsanitizeLog4jConfiguration(FilepropertiesFile) {
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 (IOExceptione) {
147+
System.err.println("Failed to sanitize Log4j configuration: " + propertiesFile.getAbsolutePath());
148+
e.printStackTrace();
149+
}
150+
}
151+
152+
privatestaticbooleanisInvalidLog4jPropertyLine(Stringline) {
153+
StringtrimmedLine = line.trim();
154+
intequalsIndex = trimmedLine.indexOf('=');
155+
returnequalsIndex >= 0 && trimmedLine.substring(0, equalsIndex).trim().equals(INVALID_LOG4J_PROPERTY);
156+
}
157+
131158
// if we have an uninstall file, uninstall the listed extensions
132159
privatestaticvoiduninstallPendingExtensions() throwsException {
133160
FileextensionsDir = newFile(EXTENSIONS_DIR);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
packagecom.mirth.connect.server.launcher;
2+
3+
importstaticorg.junit.Assert.assertFalse;
4+
importstaticorg.junit.Assert.assertTrue;
5+
6+
importjava.io.File;
7+
importjava.lang.reflect.Method;
8+
importjava.nio.charset.StandardCharsets;
9+
importjava.nio.file.Files;
10+
11+
importorg.junit.Test;
12+
13+
publicclassMirthLauncherTest {
14+
15+
@Test
16+
publicvoidtestSanitizeLog4jRemovesDirLogs() throwsException {
17+
Filefile = 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+
Methodmethod = MirthLauncher.class.getDeclaredMethod("sanitizeLog4jConfiguration", File.class);
26+
method.setAccessible(true);
27+
method.invoke(null, file);
28+
29+
StringfileContents = 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)