最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - How to enable SQL logs in PostgreSQLContainer during tests - Stack Overflow

programmeradmin2浏览0评论

I have a Java Spring application that uses a PostgreSQL docker container for the tests. I would like to be able to see the SQL logs in that PostgreSQL container, but they do not show up. Can you please tell me what I need to fix?

Test Container Configuration class:

@SuppressWarnings("PMD.TestClassWithoutTestCases")
@TestConfiguration(proxyBeanMethods = false)
public class TestcontainersConfiguration {

    @Bean
    @ServiceConnection
    PostgreSQLContainer<?> sharedDatabaseContainer() {
        return new PostgreSQLContainer<>(DockerImageName.parse("postgres:17.2"))
                .withDatabaseName("<redacted>").withUsername("<redacted>").withPassword("<redacted>")
                .withCommand("postgres", "-c", "log_statement=all") // this seems to be ignored
                .withExposedPorts(1234);
    }
}

And here is how I am using that configuration in my test class:

@Import(TestcontainersConfiguration.class)
@SpringBootTest
@Tag("UnitTest")
public class RepositoryTest {
...
}

My tests run, but when I go to the logs for that testcontainer, I cannot see any SQL logs.

I have a Java Spring application that uses a PostgreSQL docker container for the tests. I would like to be able to see the SQL logs in that PostgreSQL container, but they do not show up. Can you please tell me what I need to fix?

Test Container Configuration class:

@SuppressWarnings("PMD.TestClassWithoutTestCases")
@TestConfiguration(proxyBeanMethods = false)
public class TestcontainersConfiguration {

    @Bean
    @ServiceConnection
    PostgreSQLContainer<?> sharedDatabaseContainer() {
        return new PostgreSQLContainer<>(DockerImageName.parse("postgres:17.2"))
                .withDatabaseName("<redacted>").withUsername("<redacted>").withPassword("<redacted>")
                .withCommand("postgres", "-c", "log_statement=all") // this seems to be ignored
                .withExposedPorts(1234);
    }
}

And here is how I am using that configuration in my test class:

@Import(TestcontainersConfiguration.class)
@SpringBootTest
@Tag("UnitTest")
public class RepositoryTest {
...
}

My tests run, but when I go to the logs for that testcontainer, I cannot see any SQL logs.

Share Improve this question asked Feb 7 at 23:21 alexgbelovalexgbelov 3,1515 gold badges31 silver badges45 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

In some older test container versions, it was reported as an issue that withCommand was ignored during container startup.

I would suggest you try as a workaround instead of .withCommand the .withEnv("POSTGRES_INITDB_ARGS", "--log_statement=all").

Otherwise you can try updating your test libraries to a more recent version in which, this function is respected from the docker container.

发布评论

评论列表(0)

  1. 暂无评论