Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b69H5rTewUKPyhGS8r
48 lines
2.5 KiB
Java
48 lines
2.5 KiB
Java
package com.ankurm.logs;
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.util.List;
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.boot.builder.SpringApplicationBuilder;
|
|
import org.springframework.context.ConfigurableApplicationContext;
|
|
|
|
/** Starts a real Spring Boot application, lets it write its own log file, and parses that file. Not synthetic. */
|
|
class RealSpringBootLogTest {
|
|
|
|
@Test
|
|
void theParserReadsWhatSpringBootActuallyWrites() throws Exception {
|
|
Path log = Path.of("target", "real-springboot.log");
|
|
Files.deleteIfExists(log);
|
|
try (ConfigurableApplicationContext ctx = new SpringApplicationBuilder(LogAnomalyApplication.class)
|
|
.properties("logging.file.name=" + log, "spring.application.name=shop", "server.port=0", "spring.main.banner-mode=off").run()) {
|
|
LoggerFactory.getLogger("com.ankurm.shop.OrderController")
|
|
.error("Failed to process order 42", new IllegalStateException("quantity must be greater than 0"));
|
|
}
|
|
List<String> lines = Files.readAllLines(log);
|
|
LogParser.Result r = new LogParser().parse(lines.stream());
|
|
LogEvent err = r.events().stream().filter(e -> e.level().equals("ERROR")).findFirst().orElseThrow();
|
|
|
|
Transcript t = new Transcript("12-real-spring-boot-log.txt", "A log file written by Spring Boot 4.2.0 itself (spring.application.name=shop)");
|
|
t.section("The first three lines, and the ERROR with its first stack-trace lines");
|
|
lines.stream().limit(3).forEach(t::line);
|
|
int at = lines.indexOf(lines.stream().filter(l -> l.contains(" ERROR ")).findFirst().orElseThrow());
|
|
lines.subList(at, Math.min(at + 4, lines.size())).forEach(t::line);
|
|
t.section("Parsed");
|
|
t.line("%d physical lines, %d events, %d orphan lines", r.physicalLines(), r.events().size(), r.orphanLines());
|
|
t.line("ERROR event: logger=%s message=%s continuation lines=%d", err.shortLogger(), err.message(), err.continuation().size());
|
|
t.line("exception line: %s", err.exceptionLine());
|
|
t.write();
|
|
|
|
assertThat(r.orphanLines()).isZero();
|
|
assertThat(r.events()).isNotEmpty();
|
|
assertThat(err.continuation()).isNotEmpty();
|
|
assertThat(err.continuation().getFirst()).as("Spring Boot 4.2 puts a blank line before the stack trace").isEmpty();
|
|
assertThat(err.exceptionLine()).startsWith("java.lang.IllegalStateException");
|
|
}
|
|
}
|