Log4jUtil { public static void parsePattern(Properties p) throws InitializationException { String conversionPattern = p.getProperty(CONVERSION_PATTERN); if (conversionPattern == null) { throw new InitializationException("Log " + CONVERSION_PATTERN + " is not set."); } Matcher dateFormatMatcher = Pattern.compile("%-?\\d*(?:\\.\\d+)?d(?:\\{([^}]+)})?").matcher(conversionPattern); String dateFormat = "yyyy-MM-dd HH:mm:ss,SSS"; if (dateFormatMatcher.find() && dateFormatMatcher.groupCount() >= 1) { if (dateFormatMatcher.group(1).equals("ABSOLUTE")) { dateFormat = "HH:mm:ss,SSS"; } else { dateFormat = dateFormatMatcher.group(1); } } p.setProperty("dateFormat", dateFormat); String parserPattern = p.getProperty(CONVERSION_PATTERN, ""); Map<Character, String> conversionCharacters = new HashMap<>(); conversionCharacters.put('C', "CLASS"); conversionCharacters.put('c', "CLASS"); conversionCharacters.put('d', "TIMESTAMP"); conversionCharacters.put('F', "FILE"); conversionCharacters.put('L', "LINE"); conversionCharacters.put('m', "MESSAGE"); conversionCharacters.put('M', "METHOD"); conversionCharacters.put('n', ""); conversionCharacters.put('p', "LEVEL"); conversionCharacters.put('t', "THREAD"); conversionCharacters.put('x', "NDC"); for (Map.Entry<Character, String> conversion : conversionCharacters.entrySet()) { parserPattern = parserPattern.replaceAll("%-?\\d*(?:\\.\\d+)?" + conversion.getKey() + "(?:\\{([^}]+)})?", conversion.getValue()); } p.setProperty("pattern", parserPattern); }  static LogData translateLog4j(LoggingEvent event); static Level parseLevel(String s); static void parsePattern(Properties p); static final String CONVERSION_PATTERN; }
@Test public void testParsePattern() { Properties p = new Properties(); p.setProperty(Log4jUtil.CONVERSION_PATTERN, "%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p [%t] %c{2}: %m%n"); try { Log4jUtil.parsePattern(p); } catch (InitializationException ex) { fail(ex.getMessage()); } assertEquals("yyyy-MM-dd HH:mm:ss.SSS", p.getProperty("dateFormat")); assertEquals("TIMESTAMP LEVEL [THREAD] CLASS: MESSAGE", p.getProperty("pattern")); }