LoggerConfigUtil { @NotNull public static Set<String> extractLayoutPatterns(String content) { final LayoutEncoderConverter logbackLayoutEncoderConverter = new LogbackLayoutEncoderConverter(); Set<String> result = new HashSet<>(); final Matcher logbackMatcher = Pattern.compile("<pattern>\\s*(.*?)\\s*</pattern>", Pattern.MULTILINE).matcher(content); while (logbackMatcher.find()) { result.add(logbackMatcher.group(1)); } final List<String> barePatterns = Splitter .onPattern("\r?\n") .trimResults() .omitEmptyStrings() .splitToList(content) .stream() .filter(line -> !line.contains("value=\"")) .filter(line -> !line.contains("ConversionPattern=\"")) .filter(line -> !line.contains("<pattern>\"")) .filter(line -> !line.contains("<Pattern>\"")) .filter(line -> !line.contains("<PatternLayout>\"")) .filter(line -> { try { logbackLayoutEncoderConverter.convert(line); return true; } catch (Exception e) { return false; } }).collect(Collectors.toList()); result.addAll(barePatterns); final Matcher log4jMatcher = Pattern.compile("<param\\s*name=\"ConversionPattern\"\\s*value=\"(.*?)\".*", Pattern.MULTILINE).matcher(content); while (log4jMatcher.find()) { result.add(log4jMatcher.group(1)); } final Matcher log4j2Matcher = Pattern.compile("<Pattern>\\s*(.*?)\\s*</Pattern>", Pattern.MULTILINE).matcher(content); while (log4j2Matcher.find()) { result.add(log4j2Matcher.group(1)); } final Matcher log4jMatcher2 = Pattern.compile("<PatternLayout.*?pattern=\"(.*?)\".*", Pattern.MULTILINE).matcher(content); while (log4jMatcher2.find()) { result.add(log4jMatcher2.group(1)); } try { final Properties properties = new Properties(); properties.load(new StringReader(content)); List<String> patterns = properties .<String>keySet() .stream() .map(Object::toString) .filter(key -> key.endsWith("ConversionPattern")) .map(properties::getProperty) .collect(Collectors.toList()); result.addAll(patterns); } catch (IOException e1) { LOGGER.warn("Can't read content as properties: " + e1.getMessage()); } return result; }  @NotNull static Set<String> extractLayoutPatterns(String content); static void addLog4jParser(OtrosApplication otrosApplication, String baseFileName, String name, Properties properties);  }
@Test public void testExtractLayoutPatternsLog4jXml() { String content = "<appender name=\"console\" class=\"org.apache.log4j.ConsoleAppender\">\n" + "\t\t<param name=\"Target\" value=\"System.out\" />\n" + "\t\t<layout class=\"org.apache.log4j.PatternLayout\">\n" + "\t\t\t<param name=\"ConversionPattern\" value=\"LOG4j11 %d %-5p: %c - %m%n\" />\n" + "\t\t</layout>\n" + "\t</appender>\n" + "\t<appender name=\"file\" class=\"org.apache.log4j.FileAppender\">\n" + "\t\t<param name=\"File\" value=\"blog-log.txt\" />\n" + "\t\t<param name=\"Append\" value=\"true\" />\n" + "\t\t<layout class=\"org.apache.log4j.PatternLayout\">\n" + "\t\t\t<param \n" + " name=\"ConversionPattern\" \n" + " value=\"LOG4j12 %d %-5p: %c - %m%n\" />\n" + "\t\t</layout>"; final Set<String> strings = LoggerConfigUtil.extractLayoutPatterns(content); assertThat(strings).containsOnly("LOG4j11 %d %-5p: %c - %m%n", "LOG4j12 %d %-5p: %c - %m%n"); }
@Test public void testExtractLayoutPatternsLog4jProperties() { String content = "log4j.appender.file.MaxBackupIndex=10\n" + "log4j.appender.file.layout=org.apache.log4j.PatternLayout\n" + "log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n\n" + "\n" + "# Direct log messages to stdout\n" + "log4j.appender.stdout=org.apache.log4j.ConsoleAppender\n" + "log4j.appender.stdout.Target=System.out\n" + "log4j.appender.stdout.layout=org.apache.log4j.PatternLayout\n" + "log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"; final Set<String> strings = LoggerConfigUtil.extractLayoutPatterns(content); assertThat(strings).containsOnly("%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"); }
@Test public void testExtractLayoutPatternsLogbackXml() { String content = "<configuration>\n" + "\n" + " <triggeringPolicy class=\"ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy\">\n" + " <maxFileSize>35 MB</maxFileSize>\n" + " </triggeringPolicy>\n" + " <encoder>\n" + " <pattern>%-5level %date{dd MMM yyyy;HH:mm:ss.SSS} [%thread] %logger %msg%n</pattern>\n" + " </encoder>\n" + " </appender>\n" + "\n" + " <triggeringPolicy class=\"ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy\">\n" + " <maxFileSize>35 MB</maxFileSize>\n" + " </triggeringPolicy>\n" + " <encoder>\n" + " <pattern>\n" + " %level %date{dd MMM yyyy;HH:mm:ss.SSS} %thread %file %logger %msg%n\n" + " </pattern>\n" + " </encoder>\n" + "</configuration>\n"; final Set<String> strings = LoggerConfigUtil.extractLayoutPatterns(content); assertThat(strings).containsOnly( "%-5level %date{dd MMM yyyy;HH:mm:ss.SSS} [%thread] %logger %msg%n", "%level %date{dd MMM yyyy;HH:mm:ss.SSS} %thread %file %logger %msg%n"); }
@Test public void testExtractLayoutPatternsJustPatterns() { String content = "%-5level %date{dd MMM yyyy;HH:mm:ss.SSS} [%thread] %logger %msg%n\n" + " %level %date{dd MMM yyyy;HH:mm:ss.SSS} %thread %file %logger %msg%n\n" + "some trash"; final Set<String> strings = LoggerConfigUtil.extractLayoutPatterns(content); assertThat(strings).containsOnly( "%-5level %date{dd MMM yyyy;HH:mm:ss.SSS} [%thread] %logger %msg%n", "%level %date{dd MMM yyyy;HH:mm:ss.SSS} %thread %file %logger %msg%n"); }