Logging { public String format(String message, List<Object> params) { if (canLog()) { String formattedMessage = String.format(message, params.isEmpty() ? new Object[0] : params.toArray(new Object[params.size()])); if (ApocConfig.LoggingType.safe == apocConfig.getLoggingType()) { return formattedMessage.replaceAll("\\.| |\\t", "_").toLowerCase(); } return formattedMessage; } return null; }  @Procedure @Description("apoc.log.error(message, params) - logs error message") void error(@Name("message") String message,
                      @Name(value = "params", defaultValue = "[]") List<Object> params); @Procedure @Description("apoc.log.warn(message, params) - logs warn message") void warn(@Name("message") String message,
                     @Name(value = "params", defaultValue = "[]") List<Object> params); @Procedure @Description("apoc.log.info(message, params) - logs info message") void info(@Name("message") String message,
                     @Name(value = "params", defaultValue = "[]") List<Object> params); @Procedure @Description("apoc.log.debug(message, params) - logs debug message") void debug(@Name("message") String message,
                      @Name(value = "params", defaultValue = "[]") List<Object> params); String format(String message, List<Object> params); @Context
    public Log log; @Context
    public ApocConfig apocConfig; }
@Test public void shouldWriteSafeStrings() { Logging logging = initLogging(ApocConfig.LoggingType.safe, 10000, 10); String stringWithWhitespaces = logging.format("Test %s ", asList(1)); String stringWithWhitespacesAndTabs = logging.format("Test %s \t", asList(1)); String stringWithWhitespacesAndTabsAndNewLine = logging.format("Test %s \t\nNewLine", asList(1)); Assert.assertEquals("test_1_", stringWithWhitespaces); Assert.assertEquals("test_1__", stringWithWhitespacesAndTabs); Assert.assertEquals("test_1__\nnewline", stringWithWhitespacesAndTabsAndNewLine); }
@Test public void shouldWriteRawStrings() { Logging logging = initLogging(ApocConfig.LoggingType.raw, 10000, 10); String stringWithWhitespaces = logging.format("Test %s ", asList(1)); String stringWithWhitespacesAndTabs = logging.format("Test %s \t", asList(1)); String stringWithWhitespacesAndTabsAndNewLine = logging.format("Test %s \t\nNewLine", asList(1)); Assert.assertEquals("Test 1 ", stringWithWhitespaces); Assert.assertEquals("Test 1 \t", stringWithWhitespacesAndTabs); Assert.assertEquals("Test 1 \t\nNewLine", stringWithWhitespacesAndTabsAndNewLine); }
@Test public void shouldNotLog() { Logging logging = initLogging(ApocConfig.LoggingType.none, 10000, 10); String stringWithWhitespaces = logging.format("Test %s ", asList(1)); String stringWithWhitespacesAndTabs = logging.format("Test %s \t", asList(1)); String stringWithWhitespacesAndTabsAndNewLine = logging.format("Test %s \t\nNewLine", asList(1)); Assert.assertNull(stringWithWhitespaces); Assert.assertNull(stringWithWhitespacesAndTabs); Assert.assertNull(stringWithWhitespacesAndTabsAndNewLine); }
@Test public void shouldSkipMessagesFor10Seconds() { Logging logging = initLogging(ApocConfig.LoggingType.safe, 10000, 10); List<String> all = IntStream.range(0, 10).mapToObj(i -> "test_" + i + "_") .collect(Collectors.toList()); all.addAll(IntStream.range(50, 60).mapToObj(i -> "test_" + i + "_") .collect(Collectors.toList())); IntStream.range(0, 100).forEach(i -> { if (i < 10) { String msg = logging.format("Test %s ", asList(i)); Assert.assertEquals("test_" + i + "_", msg); all.remove(msg); return; } if (i >= 10 && i < 50) { String msg = logging.format("Test %s ", asList(i)); Assert.assertNull(msg); return; } if (i == 50) { try { Thread.sleep(11_000L); } catch (InterruptedException e) { e.printStackTrace(); } } if (i >= 50 && i < 60) { String msg = logging.format("Test %s ", asList(i)); Assert.assertEquals("test_" + i + "_", msg); all.remove(msg); return; } if (i >= 60) { String msg = logging.format("Test %s ", asList(i)); Assert.assertNull(msg); } }); Assert.assertTrue(all.isEmpty()); }
@Test public void shouldWrite20MessagesIn1Second() { Logging logging = initLogging(ApocConfig.LoggingType.safe, 1000, 20); List<String> all = IntStream.range(0, 20).mapToObj(i -> "test_" + i + "_") .collect(Collectors.toList()); all.addAll(IntStream.range(50, 70).mapToObj(i -> "test_" + i + "_") .collect(Collectors.toList())); IntStream.range(0, 100).forEach(i -> { if (i < 20) { String msg = logging.format("Test %s ", asList(i)); Assert.assertEquals("test_" + i + "_", msg); all.remove(msg); return; } if (i >= 20 && i < 50) { String msg = logging.format("Test %s ", asList(i)); Assert.assertNull(msg); return; } if (i == 50) { try { Thread.sleep(2_000L); } catch (InterruptedException e) { e.printStackTrace(); } } if (i >= 50 && i < 70) { String msg = logging.format("Test %s ", asList(i)); Assert.assertEquals("test_" + i + "_", msg); all.remove(msg); return; } if (i >= 70) { String msg = logging.format("Test %s ", asList(i)); Assert.assertNull(msg); } }); Assert.assertTrue(all.isEmpty()); }