@Test public void testTimeBasedRolling() throws IOException { FakeClock clock = FakeClock.forCurrentTimeMillis(1234567890000L) .withZone(ZoneId.of("Europe/Oslo")); RollingFileMessageWriter writer = new RollingFileMessageWriter(tmp.getRoot(), new JsonSerializer().named(), "my-log.txt", new TimeBasedRollingPolicy(5, TimeUnit.MINUTES, "my-log-%d{yyyy-MM-dd_HH-mm}.txt", clock), new TimeBasedCleanupPolicy(15, TimeUnit.MINUTES, "my-log-%d{yyyy-MM-dd_HH-mm}.txt", clock)); writer.write(generator.generate(CompactFields.kDescriptor)); for (int i = 0; i < 10; ++i) { clock.tick(107, TimeUnit.SECONDS); writer.write(generator.generate(CompactFields.kDescriptor)); } String[] files = tmp.getRoot() .list(); assertThat(files, is(notNullValue())); assertThat(ImmutableSortedSet.copyOf(files), is(ImmutableSortedSet.of("my-log.txt", "my-log-2009-02-13_22-35.txt", "my-log-2009-02-13_22-40.txt", "my-log-2009-02-13_22-45.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-2009-02-13_22-45.txt")); for (int i = 0; i < 10; ++i) { clock.tick(107, TimeUnit.SECONDS); writer.write(generator.generate(CompactFields.kDescriptor)); } files = tmp.getRoot() .list(); assertThat(files, is(notNullValue())); assertThat(ImmutableSortedSet.copyOf(files), is(ImmutableSortedSet.of("my-log.txt", "my-log-2009-02-13_22-55.txt", "my-log-2009-02-13_23-00.txt", "my-log-2009-02-13_23-05.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-2009-02-13_23-05.txt")); writer = new RollingFileMessageWriter(tmp.getRoot(), new JsonSerializer().named(), "my-log.txt", new TimeBasedRollingPolicy(TimeUnit.HOURS, "my-log-%d{yyyy-MM-dd_HH-mm}.txt", clock)); writer.write(generator.generate(CompactFields.kDescriptor)); 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-2009-02-13_23-00.txt")); }
@Override public <Message extends PMessage<Message, Field>, Field extends PField> int write(Message message) throws IOException { FileMessageWriter writer = getWriter(); int i = writer.write(message); i += writer.separator(); return i; }
RollingFileMessageWriter implements MessageWriter { @Override public <Message extends PMessage<Message, Field>, Field extends PField> int write(Message message) throws IOException { FileMessageWriter writer = getWriter(); int i = writer.write(message); i += writer.separator(); return i; } }
RollingFileMessageWriter implements MessageWriter { @Override public <Message extends PMessage<Message, Field>, Field extends PField> int write(Message message) throws IOException { FileMessageWriter writer = getWriter(); int i = writer.write(message); i += writer.separator(); return i; } RollingFileMessageWriter(@Nonnull File directory,
                                    @Nonnull Serializer serializer,
                                    @Nonnull String currentName,
                                    @Nonnull RollingPolicy rollingPolicy);  RollingFileMessageWriter(@Nonnull File directory,
                                    @Nonnull Serializer serializer,
                                    @Nonnull String currentName,
                                    @Nonnull RollingPolicy rollingPolicy,
                                    @Nullable CleanupPolicy cleanupPolicy); }
RollingFileMessageWriter implements MessageWriter { @Override public <Message extends PMessage<Message, Field>, Field extends PField> int write(Message message) throws IOException { FileMessageWriter writer = getWriter(); int i = writer.write(message); i += writer.separator(); return i; } RollingFileMessageWriter(@Nonnull File directory,
                                    @Nonnull Serializer serializer,
                                    @Nonnull String currentName,
                                    @Nonnull RollingPolicy rollingPolicy);  RollingFileMessageWriter(@Nonnull File directory,
                                    @Nonnull Serializer serializer,
                                    @Nonnull String currentName,
                                    @Nonnull RollingPolicy rollingPolicy,
                                    @Nullable CleanupPolicy cleanupPolicy); @Override int write(Message message); @Override int write(PServiceCall<Message, Field> call); @Override int separator(); @Override void close(); }
RollingFileMessageWriter implements MessageWriter { @Override public <Message extends PMessage<Message, Field>, Field extends PField> int write(Message message) throws IOException { FileMessageWriter writer = getWriter(); int i = writer.write(message); i += writer.separator(); return i; } RollingFileMessageWriter(@Nonnull File directory,
                                    @Nonnull Serializer serializer,
                                    @Nonnull String currentName,
                                    @Nonnull RollingPolicy rollingPolicy);  RollingFileMessageWriter(@Nonnull File directory,
                                    @Nonnull Serializer serializer,
                                    @Nonnull String currentName,
                                    @Nonnull RollingPolicy rollingPolicy,
                                    @Nullable CleanupPolicy cleanupPolicy); @Override int write(Message message); @Override int write(PServiceCall<Message, Field> call); @Override int separator(); @Override void close();  }