Search in sources :

Example 1 with SizeBasedRollingPolicy

use of net.morimekta.providence.mio.rolling.SizeBasedRollingPolicy in project providence by morimekta.

the class RollingFileMessageWriterTest method testSizeBasedRolling.

@Test
public void testSizeBasedRolling() throws IOException {
    CompactFields cf = new CompactFields(" * the message writer MUST be assigned a rolling policy", 1234567890, "Also note that");
    RollingFileMessageWriter writer = new RollingFileMessageWriter(tmp.getRoot(), new JsonSerializer().named(), "my-log.txt", new SizeBasedRollingPolicy(tmp.getRoot(), 1000, "my-log-%03d.txt"), new KeepLastNCleanupPolicy(5, "my-log-[\\d]{3}.txt"));
    writer.write(cf);
    for (int i = 0; i < 100; ++i) {
        writer.write(cf);
    }
    String[] files = tmp.getRoot().list();
    assertThat(files, is(notNullValue()));
    assertThat(ImmutableSortedSet.copyOf(files), is(ImmutableSortedSet.of("my-log.txt", "my-log-005.txt", "my-log-006.txt", "my-log-007.txt", "my-log-008.txt", "my-log-009.txt")));
    assertThat(Files.isSymbolicLink(tmp.getRoot().toPath().resolve("my-log.txt")), is(true));
    assertThat(Files.readSymbolicLink(tmp.getRoot().toPath().resolve("my-log.txt")).toFile().getName(), is("my-log-009.txt"));
    writer = new RollingFileMessageWriter(tmp.getRoot(), new JsonSerializer().named(), "my-log.txt", new SizeBasedRollingPolicy(tmp.getRoot(), 1000, "my-log-%03d.txt"), new KeepLastNCleanupPolicy(5, "my-log-[\\d]{3}.txt"));
    writer.write(cf);
    for (int i = 0; i < 30; ++i) {
        writer.write(cf);
    }
    files = tmp.getRoot().list();
    assertThat(files, is(notNullValue()));
    assertThat(ImmutableSortedSet.copyOf(files), is(ImmutableSortedSet.of("my-log.txt", "my-log-001.txt", "my-log-002.txt", "my-log-003.txt", "my-log-008.txt", "my-log-009.txt")));
    assertThat(Files.isSymbolicLink(tmp.getRoot().toPath().resolve("my-log.txt")), is(true));
    assertThat(Files.readSymbolicLink(tmp.getRoot().toPath().resolve("my-log.txt")).toFile().getName(), is("my-log-003.txt"));
    writer = new RollingFileMessageWriter(tmp.getRoot(), new JsonSerializer().named(), "my-log.txt", new SizeBasedRollingPolicy(tmp.getRoot(), 1000, "my-log-%03d.txt"), new KeepLastNCleanupPolicy(5, "my-log-[\\d]{3}.txt"));
    writer.write(cf);
    for (int i = 0; i < 30; ++i) {
        writer.write(cf);
    }
    files = tmp.getRoot().list();
    assertThat(files, is(notNullValue()));
    assertThat(ImmutableSortedSet.copyOf(files), is(ImmutableSortedSet.of("my-log.txt", "my-log-002.txt", "my-log-003.txt", "my-log-004.txt", "my-log-005.txt", "my-log-006.txt")));
    assertThat(Files.isSymbolicLink(tmp.getRoot().toPath().resolve("my-log.txt")), is(true));
    assertThat(Files.readSymbolicLink(tmp.getRoot().toPath().resolve("my-log.txt")).toFile().getName(), is("my-log-006.txt"));
}
Also used : SizeBasedRollingPolicy(net.morimekta.providence.mio.rolling.SizeBasedRollingPolicy) KeepLastNCleanupPolicy(net.morimekta.providence.mio.rolling.KeepLastNCleanupPolicy) JsonSerializer(net.morimekta.providence.serializer.JsonSerializer) CompactFields(net.morimekta.test.providence.core.CompactFields) Test(org.junit.Test)

Example 2 with SizeBasedRollingPolicy

use of net.morimekta.providence.mio.rolling.SizeBasedRollingPolicy in project providence by morimekta.

the class RollingFileMessageWriterTest method testCallAndSeparator.

@Test
public void testCallAndSeparator() throws IOException {
    RollingFileMessageWriter writer = new RollingFileMessageWriter(tmp.getRoot(), new JsonSerializer().named(), "my-log.txt", new SizeBasedRollingPolicy(tmp.getRoot(), 10000, "my-log-%03d.txt"));
    writer.write(new PServiceCall<>("test", PServiceCallType.EXCEPTION, 73, new PApplicationException("boo", PApplicationExceptionType.BAD_SEQUENCE_ID)));
    assertThat(writer.separator(), is(0));
    assertThat(writer.separator(), is(0));
    assertThat(writer.separator(), is(0));
    writer.close();
    String[] files = tmp.getRoot().list();
    assertThat(files, is(notNullValue()));
    assertThat(ImmutableSortedSet.copyOf(files), is(ImmutableSortedSet.of("my-log.txt", "my-log-001.txt")));
    assertThat(Files.isSymbolicLink(tmp.getRoot().toPath().resolve("my-log.txt")), is(true));
    assertThat(Files.readSymbolicLink(tmp.getRoot().toPath().resolve("my-log.txt")).toFile().getName(), is("my-log-001.txt"));
    String content = new String(Files.readAllBytes(tmp.getRoot().toPath().resolve("my-log.txt")), StandardCharsets.UTF_8);
    assertThat(content, is("[\"test\",\"exception\",73,{\"message\":\"boo\",\"id\":\"BAD_SEQUENCE_ID\"}]\n"));
}
Also used : SizeBasedRollingPolicy(net.morimekta.providence.mio.rolling.SizeBasedRollingPolicy) PApplicationException(net.morimekta.providence.PApplicationException) JsonSerializer(net.morimekta.providence.serializer.JsonSerializer) Test(org.junit.Test)

Aggregations

SizeBasedRollingPolicy (net.morimekta.providence.mio.rolling.SizeBasedRollingPolicy)2 JsonSerializer (net.morimekta.providence.serializer.JsonSerializer)2 Test (org.junit.Test)2 PApplicationException (net.morimekta.providence.PApplicationException)1 KeepLastNCleanupPolicy (net.morimekta.providence.mio.rolling.KeepLastNCleanupPolicy)1 CompactFields (net.morimekta.test.providence.core.CompactFields)1