惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The GitHub Blog
The GitHub Blog
阮一峰的网络日志
阮一峰的网络日志
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Apple Machine Learning Research
Apple Machine Learning Research
小众软件
小众软件
博客园 - 司徒正美
Last Week in AI
Last Week in AI
爱范儿
爱范儿
罗磊的独立博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园_首页
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Cloudflare Blog
雷峰网
雷峰网
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
WordPress大学
WordPress大学
Jina AI
Jina AI
人人都是产品经理
人人都是产品经理
量子位
V
V2EX
博客园 - 叶小钗
宝玉的分享
宝玉的分享
T
Tailwind CSS Blog

Maciej Walkowiak - Java & Spring

Blog Generating HTTP clients in Spring Boot application from OpenAPI spec PostgreSQL and UUID as primary key Dynamic Projections with Spring Data JPA Container logs with Spring Boot and Testcontainers Reified Generics in Java? Faster integration tests with reusable Testcontainers and Flyway Running one-time jobs with Quartz and Spring Boot The best way to use Testcontainers with Spring Boot Spring Boot & Flyway - clear database between integration tests What's new in Spring? Activate Maven Profile by Operating System Spring Boot with Thymeleaf and Tailwind CSS - Complete Guide How to publish a Java library to Maven Central - Complete Guide Docker Compose - waiting until containers are ready Single file Java applications with JBang Beautiful bash scripts with Gum Running Java on CRaC Spring Boot 3.0 & GraalVM Native Image - not a free lunch Creating Spring Cloud Function projects with AWS SAM Loading classpath resources to String with a custom JUnit extension Creating Project Templates with Cookiecutter Auto-Registering JUnit 5 extensions Spring Boot component scanning without annotations Listing Maven dependencies in Spring Boot Actuator Info endpoint Spring Cloud AWS 2.3 RC2 Released How I built vlad-cli - command line interface to Vlad Mihalcea The State of Java Relational Persistence On Choosing a Tech Stack
How to log PostgreSQL queries with Testcontainers
2022-10-25 · via Maciej Walkowiak - Java & Spring
Published on
  • Spring Boot
  • Graalvm

One of the benefits of Testcontainers is the ability to programmatically configure containers.

This blog posts shows how to configure PostgreSQL container to log SQL queries, but you can use similar approach to other containers and use cases.

How to log queries from PostgreSQL? ​

By default, Postgres does not log any queries. It can be configured by switching the log_statement configuration property to all. In similar way, if you want to log all incoming connections - switch log_connections to on. To log disconnections log_disconnections to on. There are quite a few logging related configuration options, I recommend looking into the official PostgreSQL docs.

To put it all together, to run Postgres with Docker and log all connections and statements, run following command:

$ docker run -p 5432:5432 -e POSTGRES_PASSWORD=password postgres:13.3 -c log_connections=on -c log_disconnections=on -c log_statement=all

Let's now see how it can be translated to Testcontainers.

Configuring Docker command with Testcontainers ​

Testcontainers comes higher level module for Postgres which saves us from any low level coding - if we want to stick to defaults:

java

try (var postgres = new PostgreSQLContainer("postgres:13.3")) {
    postgres.start();
}

Under the hood, Testcontainers run Postgres with command postgres -c fsync=off to speed up startup time (see more).

To enable logging, we need to modify Docker command:

java

try (var postgres = new PostgreSQLContainer("postgres:13.3")) {
    postgres.setCommand("postgres", "-c", "fsync=off", "-c", "log_statement=all");
    postgres.start();
}

Capturing container logs ​

Now we can hook into container and capture its logs.

java

postgres.followOutput(<consumer>);

If you use Slf4j, you can just pipe container logs into an Slf4j logger:

java

Logger LOGGER = LoggerFactory.getLogger(MyComponent.class);
//...

try (var postgres = new PostgreSQLContainer("postgres:13.3")) {
    postgres.setCommand("postgres", "-c", "fsync=off", "-c", "log_statement=all");
    postgres.start();
    postgres.followOutput(new Slf4jLogConsumer(LOGGER));
}

Note that followOutput must be called after container is started. Which also means that you will not get this way access to container startup logs that happen before postgres.start() method finished. To do it call postgres.getLogs() before followOutput(..).

java

Logger LOGGER = LoggerFactory.getLogger(MyComponent.class);
//...

try (var postgres = new PostgreSQLContainer("postgres:13.3")) {
    postgres.setCommand("postgres", "-c", "fsync=off", "-c", "log_statement=all");
    postgres.start();
    LOGGER.debug(postgres.getLogs()); // prints startup logs
    postgres.followOutput(new Slf4jLogConsumer(LOGGER));
}

Filtering logs ​

There is a chance that you want only selection of logs from Postgres to appear in your logs and you need some kind of filtering. Container#followOutput take simple Java Consumer as a parameter making it easy to plug in filtering:

java

public class FilteringConsumer implements Consumer<OutputFrame> {
    private final Consumer<OutputFrame> delegate;
    private final Predicate<OutputFrame> predicate;

    public FilteringConsumer(Predicate<OutputFrame> predicate, Consumer<OutputFrame> delegate) {
        this.delegate = delegate;
        this.predicate = predicate;
    }

    @Override 
    public void accept(OutputFrame outputFrame) {
        if (predicate.test(outputFrame)) {
            delegate.accept(outputFrame);
        }
    }
}

And then instead of using Sfl4jLogConsumer directly, we set it as a delegate of FilteringConsumer:

java

// likely you will use more sophisticated predicate than that
postgres.followOutput(new FilteringConsumer(frame -> frame.getUtf8String().contains("select"), new Slf4jLogConsumer(LOGGER)));

That's it!

Let's stay in touch and follow me on Twitter: @maciejwalkowiak

Subscribe to RSS feed