Skip to content

Commit 47e5725

Browse files
committed
Implementing ability to redirect geckodriver logs to a file
1 parent 83dbb87 commit 47e5725

2 files changed

Lines changed: 25 additions & 8 deletions

File tree

java/client/src/org/openqa/selenium/firefox/GeckoDriverService.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.openqa.selenium.remote.service.DriverService;
2828

2929
import java.io.File;
30+
import java.io.FileOutputStream;
3031
import java.io.IOException;
3132
import java.net.MalformedURLException;
3233

@@ -78,6 +79,7 @@ public static class Builder extends DriverService.Builder<
7879
GeckoDriverService, GeckoDriverService.Builder> {
7980

8081
private FirefoxBinary binary;
82+
8183
public Builder() {
8284
this(new FirefoxBinary());
8385
}
@@ -101,10 +103,6 @@ protected File findDefaultExecutable() {
101103
protected ImmutableList<String> createArgs() {
102104
ImmutableList.Builder<String> argsBuilder = ImmutableList.builder();
103105
argsBuilder.add(String.format("--port=%d", getPort()));
104-
if (getLogFile() != null) {
105-
// TODO: watch https://github.com/mozilla/geckodriver/issues/415
106-
//argsBuilder.add(String.format("--log-file=\"%s\"", getLogFile().getAbsolutePath()));
107-
}
108106
try {
109107
argsBuilder.add("-b");
110108
argsBuilder.add(binary.getPath());
@@ -117,10 +115,23 @@ protected ImmutableList<String> createArgs() {
117115

118116
@Override
119117
protected GeckoDriverService createDriverService(File exe, int port,
120-
ImmutableList<String> args,
121-
ImmutableMap<String, String> environment) {
118+
ImmutableList<String> args,
119+
ImmutableMap<String, String> environment) {
122120
try {
123-
return new GeckoDriverService(exe, port, args, environment);
121+
GeckoDriverService service = new GeckoDriverService(exe, port, args, environment);
122+
if (getLogFile() != null) {
123+
service.sendOutputTo(new FileOutputStream(getLogFile()));
124+
} else {
125+
String firefoxLogFile = System.getProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE);
126+
if (firefoxLogFile != null) {
127+
if ("/dev/stdout".equals(firefoxLogFile)) {
128+
service.sendOutputTo(System.out);
129+
}
130+
service.sendOutputTo(new FileOutputStream(firefoxLogFile));
131+
}
132+
133+
}
134+
return service;
124135
} catch (IOException e) {
125136
throw new WebDriverException(e);
126137
}

java/client/src/org/openqa/selenium/remote/service/DriverService.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
import java.io.File;
3636
import java.io.IOException;
37+
import java.io.OutputStream;
3738
import java.net.MalformedURLException;
3839
import java.net.URL;
3940
import java.util.Map;
@@ -68,6 +69,7 @@ public class DriverService {
6869
private final String executable;
6970
private final ImmutableList<String> args;
7071
private final ImmutableMap<String, String> environment;
72+
private OutputStream outputStream = System.err;
7173

7274
/**
7375
*
@@ -160,7 +162,7 @@ public void start() throws IOException {
160162
}
161163
process = new CommandLine(this.executable, args.toArray(new String[] {}));
162164
process.setEnvironmentVariables(environment);
163-
process.copyOutputTo(System.err);
165+
process.copyOutputTo(outputStream);
164166
process.executeAsync();
165167

166168
waitUntilAvailable();
@@ -204,6 +206,10 @@ public void stop() {
204206
}
205207
}
206208

209+
public void sendOutputTo(OutputStream outputStream) {
210+
this.outputStream = outputStream;
211+
}
212+
207213
public static abstract class Builder<DS extends DriverService, B extends Builder> {
208214

209215
private int port = 0;

0 commit comments

Comments
 (0)