Skip to content

Commit 266ed83

Browse files
authored
Merge pull request #46 from avast/CaptureContainerOutput
Optionally capture output of all containers and send it to Gradle output
2 parents c82e083 + 974c54f commit 266ed83

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ dockerCompose.isRequiredBy(test) // hooks 'dependsOn composeUp' and 'finalizedBy
3838
3939
dockerCompose {
4040
// useComposeFiles = ['docker-compose.yml', 'docker-compose.prod.yml'] // like 'docker-compose -f <file>'
41+
// captureContainersOutput = true // prinnts output of all containers to Gradle output - very useful for debugging
4142
// stopContainers = false // useful for debugging
4243
// removeContainers = false
4344
// removeImages = "None" // Other accepted values are: "All" and "Local"

src/main/groovy/com/avast/gradle/dockercompose/ComposeExtension.groovy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class ComposeExtension {
1717

1818
boolean buildBeforeUp = true
1919
boolean waitForTcpPorts = true
20+
boolean captureContainersOutput = false
2021
Duration waitAfterTcpProbeFailure = Duration.ofSeconds(1)
2122
Duration waitForTcpPortsTimeout = Duration.ofMinutes(15)
2223
Duration waitForTcpPortsDisconnectionProbeTimeout = Duration.ofMillis(1000)

src/main/groovy/com/avast/gradle/dockercompose/tasks/ComposeUp.groovy

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import org.gradle.process.ExecSpec
1010
import org.yaml.snakeyaml.Yaml
1111

1212
import java.time.Instant
13+
import java.util.concurrent.Executors
1314

1415
class ComposeUp extends DefaultTask {
1516

@@ -40,6 +41,9 @@ class ComposeUp extends DefaultTask {
4041
e.commandLine extension.composeCommand('up', '-d')
4142
}
4243
try {
44+
if (extension.captureContainersOutput) {
45+
captureContainersOutput()
46+
}
4347
servicesInfos = loadServicesInfo().collectEntries { [(it.name): (it)] }
4448
waitForHealthyContainers(servicesInfos.values())
4549
if (extension.waitForTcpPorts) {
@@ -52,6 +56,34 @@ class ComposeUp extends DefaultTask {
5256
}
5357
}
5458

59+
protected void captureContainersOutput() {
60+
def t = Executors.defaultThreadFactory().newThread(new Runnable() {
61+
@Override
62+
void run() {
63+
project.exec { ExecSpec e ->
64+
e.commandLine extension.composeCommand('logs', '-f', '--no-color')
65+
e.standardOutput = new OutputStream() {
66+
def buffer = new ArrayList<Byte>()
67+
@Override
68+
void write(int b) throws IOException {
69+
if (b == 10 || b == 13) {
70+
if (buffer.size() > 0) {
71+
def toPrint = buffer.collect { it as byte }.toArray() as byte[]
72+
logger.lifecycle(new String(toPrint))
73+
buffer.clear()
74+
}
75+
} else {
76+
buffer.add(b as Byte)
77+
}
78+
}
79+
}
80+
}
81+
}
82+
})
83+
t.daemon = true
84+
t.start()
85+
}
86+
5587
protected Iterable<ServiceInfo> loadServicesInfo() {
5688
getServiceNames().collect { createServiceInfo(it) }
5789
}

0 commit comments

Comments
 (0)