testcontainers / testcontainers-java

Testcontainers is a Java library that supports JUnit tests, providing lightweight, throwaway instances of common databases, Selenium web browsers, or anything else that can run in a Docker container.
https://testcontainers.org
MIT License
8.02k stars 1.65k forks source link

Loggers provided by DockerLoggerFactory inherit from root logger instead of custom loggers #3016

Closed vcvitaly closed 1 year ago

vcvitaly commented 4 years ago

Example configuration from modules/mysql/src/test/resources/logback-test.xml:

<root level="INFO">
    <appender-ref ref="STDOUT"/>
</root>

<logger name="org.testcontainers" level="DEBUG"/>

However DockerLoggerFactory provides names that either start with 🐳 or "docker" and they go under the root hierarchy. So effectively any logging level lower (ex. "DEBUG") than one declared in root ("INFO" in this case) is swallowed.

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you believe this is a mistake, please reply to this comment to keep it open. If there isn't one already, a PR to fix or at least reproduce the problem in a test case will always help us get back on track to tackle this.

Controlix commented 2 years ago

I am struggling with this too. It is simply not possible easily to set a log level for each container by configuration. The problem seems to be exactly what is described above:

okorz001 commented 2 years ago

This is a huge pain point for me this week. The whale is cute for trivial usage but these loggers are annoying to configure in a serious application where you need to have strict root level logging to filter out other annoying noisy loggers that you don't control.

A simple non-unicode prefix in DockerLoggerFactory would be a huge improvement.

seregamorph commented 1 year ago

As a workaround I defined logging this way in logback-test.xml:

    <!--
    Sample full logger name: "🐳 [artifactory.acme.com/docker-virtual/testcontainers/ryuk:0.3.4]"
    (formatted in DockerLoggerFactory.getLogger)
    -->
    <logger name="🐳 [artifactory" level="DEBUG"/>
    <logger name="🐳 [docker" level="DEBUG"/>
    <logger name="🐳 [quay" level="DEBUG"/>
    <logger name="🐳 [alpine:3.16]" level="DEBUG"/>
    <!-- Note: will not work after migration to dynamic docker image tags based on hashsum -->
    <logger name="🐳 [postgres-it-main:latest]" level="DEBUG"/>
    <logger name="🐳 [postgres-it-audit:latest]" level="DEBUG"/>
    <logger name="🐳 [postgres-it-sole:latest]" level="DEBUG"/>
    <logger name="🐳 [postgres-it-apps:latest]" level="DEBUG"/>
    <logger name="🐳 [postgres-it-dumper:latest]" level="DEBUG"/>
    <logger name="🐳 [postgres-it-shards:latest]" level="DEBUG"/>
    <logger name="🐳 [postgres-it-jiracards:latest]" level="DEBUG"/>
    <logger name="🐳 [postgres-it-mfa:latest]" level="DEBUG"/>

Note, that all docker images are fetched from internal proxy, hence they all have artifactory. prefix (except quay and alpine) as they are proxied to avoid loading from docker hub.

But for sure this needs a general solution.

seregamorph commented 1 year ago

Submitted two proposed solutions:

eddumelendez commented 1 year ago

@seregamorph thanks for raising the PRs. However, next time consider discuss those proposals in issues like this or raise a discussion in GitHub. As you probably already know, we are considering #3018 but has not been merged yet. Most important, we don't want you to jump into code which involve effort and this could be in vain. We really appreciate everything the community does 🙂 and IMHO, this is just a small thing to make the process smooth for everyone.

seregamorph commented 1 year ago

@eddumelendez sure, but it was not mentioned in this thread (EDIT: well, I see it now, sorry). I understand your statement, that's why my PR are PoC proposals simplified as much as possible not to bother reviewers too much and as a material to discuss. Hope any of proposals will be merged soon as my project is also suffering of this issue 🙏

mikaelhg commented 1 year ago

If for any reason you can't upgrade TC to 1.18.0 or newer, this is also a solution:

package io.mikael.ingest.logging

import ch.qos.logback.classic.spi.ILoggingEvent
import ch.qos.logback.core.filter.Filter
import ch.qos.logback.core.spi.FilterReply.DENY
import ch.qos.logback.core.spi.FilterReply.NEUTRAL

/**
 * Configure LogBack to omit the 🐳 whale log rows produced by Testcontainers.
 *
 * In your `logback-test.xml` element `<appender ...>`, add the filter declaration
 * ```
 *     <filter class="io.mikael.ingest.logging.TestcontainersFilter" />
 *```
 */
class TestcontainersFilter : Filter<ILoggingEvent>() {
    override fun decide(event: ILoggingEvent) = when {
        event.loggerName.startsWith("\uD83D\uDC33") -> DENY
        event.loggerName.startsWith("docker[") -> DENY
        event.loggerName.startsWith("tc.") -> DENY
        else -> NEUTRAL
    }
}