mirror of
https://github.com/elastic/elasticsearch.git
synced 2025-04-24 15:17:30 -04:00
Forward-port improvements to transform-log4j-config from 7.x
This commit is contained in:
parent
922c82ff64
commit
7ed43a9e1e
2 changed files with 75 additions and 12 deletions
|
@ -36,7 +36,7 @@ public class TransformLog4jConfig {
|
|||
public static void main(String[] args) throws IOException {
|
||||
List<String> lines = getConfigFile(args);
|
||||
|
||||
final List<String> output = transformConfig(lines);
|
||||
final List<String> output = skipBlanks(transformConfig(lines));
|
||||
|
||||
output.forEach(System.out::println);
|
||||
}
|
||||
|
@ -62,20 +62,50 @@ public class TransformLog4jConfig {
|
|||
return Files.readAllLines(configPath);
|
||||
}
|
||||
|
||||
public static List<String> transformConfig(List<String> lines) {
|
||||
/** Squeeze multiple empty lines into a single line. */
|
||||
static List<String> skipBlanks(List<String> lines) {
|
||||
boolean skipNextEmpty = false;
|
||||
|
||||
final List<String> output = new ArrayList<>(lines.size());
|
||||
|
||||
for (final String line : lines) {
|
||||
if (line.isEmpty()) {
|
||||
if (skipNextEmpty) {
|
||||
continue;
|
||||
} else {
|
||||
skipNextEmpty = true;
|
||||
}
|
||||
} else {
|
||||
skipNextEmpty = false;
|
||||
}
|
||||
|
||||
output.add(line);
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static List<String> transformConfig(List<String> lines) {
|
||||
final List<String> output = new ArrayList<>(lines.size());
|
||||
|
||||
// This flag provides a way to handle properties whose values are split
|
||||
// over multiple lines and we need to omit those properties.
|
||||
boolean skipNext = false;
|
||||
|
||||
for (final String line : lines) {
|
||||
for (String line : lines) {
|
||||
if (skipNext) {
|
||||
if (line.endsWith("\\") == false) {
|
||||
skipNext = false;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// Skip lines with this comment - we remove the relevant config
|
||||
if (line.contains("old style pattern")) {
|
||||
skipNext = line.endsWith("\\");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (line.startsWith("appender.")) {
|
||||
String[] parts = line.split("\\s*=\\s*");
|
||||
String key = parts[0];
|
||||
|
@ -84,8 +114,9 @@ public class TransformLog4jConfig {
|
|||
|
||||
// We don't need to explicitly define a console appender because the
|
||||
// "rolling" appender will become a console appender. We also don't
|
||||
// carry over "rolling_old"
|
||||
if (keyParts[1].equals("console") || keyParts[1].equals("rolling_old")) {
|
||||
// carry over "*_old" appenders
|
||||
if (keyParts[1].equals("console") || keyParts[1].endsWith("_old")) {
|
||||
skipNext = line.endsWith("\\");
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -94,7 +125,7 @@ public class TransformLog4jConfig {
|
|||
if (value.equals("RollingFile")) {
|
||||
value = "Console";
|
||||
}
|
||||
output.add(key + " = " + value);
|
||||
line = key + " = " + value;
|
||||
break;
|
||||
|
||||
case "fileName":
|
||||
|
@ -103,22 +134,31 @@ public class TransformLog4jConfig {
|
|||
case "strategy":
|
||||
// No longer applicable. Omit it.
|
||||
skipNext = line.endsWith("\\");
|
||||
break;
|
||||
continue;
|
||||
|
||||
default:
|
||||
output.add(line);
|
||||
break;
|
||||
}
|
||||
} else if (line.startsWith("rootLogger.appenderRef")) {
|
||||
String[] parts = line.split("\\s*=\\s*");
|
||||
|
||||
// The root logger only needs this appender
|
||||
if (parts[1].equals("rolling")) {
|
||||
output.add(line);
|
||||
if (parts[1].equals("rolling") == false) {
|
||||
skipNext = line.endsWith("\\");
|
||||
continue;
|
||||
}
|
||||
} else if (line.startsWith("logger.")) {
|
||||
String[] parts = line.split("\\s*=\\s*");
|
||||
String key = parts[0];
|
||||
String[] keyParts = key.split("\\.");
|
||||
|
||||
if (keyParts[2].equals("appenderRef") && keyParts[3].endsWith("_old")) {
|
||||
skipNext = line.endsWith("\\");
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
output.add(line);
|
||||
}
|
||||
|
||||
output.add(line);
|
||||
}
|
||||
|
||||
return output;
|
||||
|
|
|
@ -127,6 +127,29 @@ public class TransformLog4jConfigTests extends TestCase {
|
|||
runTest(input, expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that as well as skipping old appenders, logger references to them are also skipped.
|
||||
*/
|
||||
public void testTransformSkipsOldAppenderRefs() {
|
||||
List<String> input = List.of(
|
||||
"logger.index_indexing_slowlog.appenderRef.index_indexing_slowlog_rolling_old.ref = index_indexing_slowlog_rolling_old"
|
||||
);
|
||||
|
||||
runTest(input, List.of());
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that multiple blank lines are reduced to a single line.
|
||||
*/
|
||||
public void testMultipleBlanksReducedToOne() {
|
||||
List<String> input = List.of("status = error", "", "", "rootLogger.level = info");
|
||||
|
||||
List<String> expected = List.of("status = error", "", "rootLogger.level = info");
|
||||
|
||||
final List<String> transformed = TransformLog4jConfig.skipBlanks(input);
|
||||
assertThat(transformed, equalTo(expected));
|
||||
}
|
||||
|
||||
private void runTest(List<String> input, List<String> expected) {
|
||||
final List<String> transformed = TransformLog4jConfig.transformConfig(input);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue