Skip to content

Commit

Permalink
wip(app): rotate AppNoteSinkOfFile #697
Browse files Browse the repository at this point in the history
$source=55b3e5737affa2d3e97ffc9e8994aa07e3b62779$
  • Loading branch information
marcioendo committed Oct 14, 2024
1 parent fabad84 commit 79b7723
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 2 deletions.
19 changes: 18 additions & 1 deletion main/objectos/way/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -1027,8 +1027,25 @@ final AppNoteSinkOfFile build() throws IOException {
Files.createDirectories(parent);
}

if (Files.exists(file)) {
LocalDateTime now;
now = LocalDateTime.now(clock);

String suffix;
suffix = DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(now);

Path target;
target = file.resolveSibling(file.getFileName().toString() + "." + suffix);

Files.copy(file, target);
}

SeekableByteChannel channel;
channel = Files.newByteChannel(file, StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.APPEND);
channel = Files.newByteChannel(
file,

StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE
);

return new AppNoteSinkOfFile(clock, filter, buffer, channel);
}
Expand Down
56 changes: 55 additions & 1 deletion test/objectos/way/AppNoteSinkTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.PrintStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.time.LocalDate;
import org.testng.annotations.Test;

Expand Down Expand Up @@ -144,8 +145,8 @@ public void console02() {

@Test(description = """
- no filtering
- parent directories exists
- log file does not exist
- parent directories exist
""")
public void file01() throws IOException {
Path parent;
Expand Down Expand Up @@ -182,6 +183,59 @@ public void file01() throws IOException {
);
}

@Test(description = """
- no filtering
- parent directory exists
- log file exists and should be rotated
""")
public void file02() throws IOException {
Path parent;
parent = directory.resolve("file02");

Files.createDirectory(parent);

Path logFile;
logFile = parent.resolve("file.log");

Files.writeString(logFile, "Existing content", StandardOpenOption.CREATE);

IncrementingClock clock;
clock = new IncrementingClock(2023, 10, 31);

App.NoteSink2.OfFile noteSink;
noteSink = App.NoteSink2.OfFile.create(logFile, config -> {
config.clock(clock);
});

clock.reset();

try (noteSink) {
sendAll(noteSink);
}

assertEquals(
Files.readString(parent.resolve("file.log.2023-10-31T10:00:00")),

"Existing content"
);

assertEquals(
Files.readString(logFile),

"""
2023-10-31 10:00:00.000 TRACE --- [main ] objectos.way.AppNoteSinkTest : int1 1001
2023-10-31 10:01:00.000 DEBUG --- [main ] objectos.way.AppNoteSinkTest : int2 2002 3003
2023-10-31 10:02:00.000 INFO --- [main ] objectos.way.AppNoteSinkTest : int3 4000 5000 6000
2023-10-31 10:03:00.000 WARN --- [main ] objectos.way.AppNoteSinkTest : long1 1000
2023-10-31 10:04:00.000 ERROR --- [main ] objectos.way.AppNoteSinkTest : long2 2123 3456
2023-10-31 10:05:00.000 TRACE --- [main ] objectos.way.AppNoteSinkTest : ref0
2023-10-31 10:06:00.000 DEBUG --- [main ] objectos.way.AppNoteSinkTest : ref1 A
2023-10-31 10:07:00.000 INFO --- [main ] objectos.way.AppNoteSinkTest : ref2 id 2024-10-11
2023-10-31 10:08:00.000 WARN --- [main ] objectos.way.AppNoteSinkTest : ref3 FOO 2010-05-23 BAR
"""
);
}

private void sendAll(Note.Sink sink) {
sink.send(notes.int1, 1001);
sink.send(notes.int2, 2002, 3003);
Expand Down
4 changes: 4 additions & 0 deletions test/objectos/way/IncrementingClock.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public final ZoneId getZone() {
return startTime.getZone();
}

public final void reset() {
minutes = 0;
}

@Override
public Clock withZone(ZoneId zone) {
throw new UnsupportedOperationException();
Expand Down

0 comments on commit 79b7723

Please sign in to comment.