JsonLogParser extends AbstractPluginableElement implements LogParser { @Override public LogData parse(String line, ParsingContext parsingContext) { final StringBuilder unmatchedLog = parsingContext.getUnmatchedLog(); if (unmatchedLog.length() == 0 && !line.startsWith("{")) { return null; } unmatchedLog.append(line).append("\n"); final Optional<LogData> logData = jsonExtractor.parseJsonLog(unmatchedLog.toString(), parsingContext.getDateFormat()); if (logData.isPresent()) { unmatchedLog.setLength(0); return logData.get(); } return null; } JsonLogParser(); @Override void init(Properties properties); @Override void initParsingContext(ParsingContext parsingContext); @Override LogData parse(String line, ParsingContext parsingContext); @Override TableColumns[] getTableColumnsToUse(); @Override ParserDescription getParserDescription(); @Override int getVersion(); @Override int getApiVersion(); @Override String getName(); @Override String getDescription();  }
@Test(dataProvider = "testParse") public void testParse(String fileName, String desc, Properties properties) throws Exception { final JsonLogParser jsonParser = new JsonLogParser(); jsonParser.init(properties); final ParsingContext parsingContext = new ParsingContext(); jsonParser.initParsingContext(parsingContext); LogImporterUsingParser importerUsingParser = new LogImporterUsingParser(jsonParser); LogDataCollector ld = new MemoryLogDataStore(); importerUsingParser.importLogs(this.getClass().getClass().getResourceAsStream("/jsonLog/" + fileName), ld, parsingContext); final LogData[] logDatas = ld.getLogData(); assertEquals(logDatas.length, 5); final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy"); for (int i = 0; i < logDatas.length; i++) { final LogData logData = logDatas[i]; assertEquals(logData.getMessage(), "some message" + i); assertEquals(logData.getThread(), "main" + i); assertEquals(logData.getMethod(), "method" + i); assertEquals(logData.getFile(), "file" + i); assertEquals(logData.getClazz(), "a.Some" + i); assertEquals(logData.getLoggerName(), "logger" + i); assertEquals(dateFormat.format(logData.getDate()), "201" + i); final Map<String, String> mdc = logData.getProperties(); assertEquals(mdc.get("appId"), "special_system" + i); assertEquals(mdc.get("hostname"), "host" + i); assertEquals(mdc.get("user"), "root" + i); } assertEquals(logDatas[0].getLevel().intValue(), Level.FINEST.intValue()); assertEquals(logDatas[1].getLevel().intValue(), Level.FINE.intValue()); assertEquals(logDatas[2].getLevel().intValue(), Level.INFO.intValue()); assertEquals(logDatas[3].getLevel().intValue(), Level.WARNING.intValue()); assertEquals(logDatas[4].getLevel().intValue(), Level.SEVERE.intValue()); }