Skip to content

NullPointerException using logger org.slf4j:slf4j-log4j12:jar:1.7.21 with testcontainers #375

@islisava

Description

@islisava

From testcontainers documentation:
Testcontainers, and many of the libraries it uses, utilize slf4j for logging. In order to see logs from Testcontainers, your project should include an SLF4J implementation (Logback is recommended).

I expect testcontainers to support org.slf4j.slf4j-log4j12
The next NullPointerException appears:

Caused by: java.lang.NullPointerException
	at java.util.Hashtable.put(Hashtable.java:459)
	at org.apache.log4j.MDC.put0(MDC.java:150)
	at org.apache.log4j.MDC.put(MDC.java:85)
	at org.slf4j.impl.Log4jMDCAdapter.put(Log4jMDCAdapter.java:60)
	at org.slf4j.MDC.put(MDC.java:116)
	at org.testcontainers.utility.AuditLogger.doLog(AuditLogger.java:34)
	at org.testcontainers.dockerclient.AuditLoggingDockerClient.lambda$startContainerCmd$3(AuditLoggingDockerClient.java:43)
	at org.testcontainers.dockerclient.AuditLoggingDockerClient.lambda$wrappedCommand$14(AuditLoggingDockerClient.java:102)
	at com.sun.proxy.$Proxy20.exec(Unknown Source)
	at org.testcontainers.containers.GenericContainer.tryStart(GenericContainer.java:201)
	... 18 more

AuditLoggingDockerClient (for commands START,REMOVE,STOP,KILL,CREATE_NETWORK,REMOVE_NETWORK) has call like this:

(cmd, e) -> doLog("START", null, containerId, cmd, e));

doLog(String action, String image, String containerId, DockerCmd<?> cmd, @Nullable Exception e)

So image = null.
org.slf4j.impl.Log4jMDCAdapter uses Hashtable where therev is a check for value != null during call 'put'. So NullPointerException occurs.

Possible fix is to use image="" instead of image=null

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions