Skip to content

Commit 71ffcd5

Browse files
committed
Add example for chrome logging
1 parent 1a25380 commit 71ffcd5

File tree

5 files changed

+120
-13
lines changed

5 files changed

+120
-13
lines changed

cdt-examples/pom.xml

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,13 @@
4545
<artifactId>jackson-core</artifactId>
4646
<version>${jackson.version}</version>
4747
</dependency>
48-
</dependencies>
4948

50-
<profiles>
51-
<profile>
52-
<id>logging</id>
53-
<dependencies>
54-
<dependency>
55-
<groupId>ch.qos.logback</groupId>
56-
<artifactId>logback-classic</artifactId>
57-
<version>${logback.version}</version>
58-
</dependency>
59-
</dependencies>
60-
</profile>
61-
</profiles>
49+
<dependency>
50+
<groupId>ch.qos.logback</groupId>
51+
<artifactId>logback-classic</artifactId>
52+
<version>${logback.version}</version>
53+
</dependency>
54+
</dependencies>
6255

6356
<build>
6457
<plugins>
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package com.github.kklisura.cdt.examples;
2+
3+
/*-
4+
* #%L
5+
* cdt-examples
6+
* %%
7+
* Copyright (C) 2018 - 2021 Kenan Klisura
8+
* %%
9+
* Licensed under the Apache License, Version 2.0 (the "License");
10+
* you may not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS,
17+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
* #L%
21+
*/
22+
23+
import ch.qos.logback.classic.Level;
24+
import ch.qos.logback.classic.LoggerContext;
25+
import com.github.kklisura.cdt.launch.ChromeArguments;
26+
import com.github.kklisura.cdt.launch.ChromeLauncher;
27+
import com.github.kklisura.cdt.services.ChromeDevToolsService;
28+
import com.github.kklisura.cdt.services.ChromeService;
29+
import com.github.kklisura.cdt.services.types.ChromeTab;
30+
import org.slf4j.LoggerFactory;
31+
32+
/**
33+
* By setting logger `com.github.kklisura.cdt.launch.chrome.output` to DEBUG level, either
34+
* programmatically or via logging configuration (see logback.xml in the example) and by introducing
35+
* arguments to chrome to enable more verbose logging one can observe detailed chrome output. This
36+
* should be used to debug some issues with chrome and should not generally not be used in normal
37+
* course of operations. Note that, when you set this logger to DEBUG, a
38+
* chrome-launcher:read-line-thread thread will be alive, until browser gets closed.
39+
*
40+
* <p>Logger `com.github.kklisura.cdt.launch.chrome.output` can be set to DEBUG level
41+
* programmatically as in an example below (enableDebugChromeOutput method) or by introducing
42+
*
43+
* <pre>
44+
* <logger name="com.github.kklisura.cdt.launch.chrome.output" level="DEBUG" />
45+
* </pre>
46+
*
47+
* logger to logging configuration.
48+
*
49+
* @author Kenan Klisura
50+
*/
51+
public class ChromeLoggingExample {
52+
53+
/**
54+
* Programmatically set `com.github.kklisura.cdt.launch.chrome.output` to DEBUG level so chrome
55+
* output can be observed.
56+
*/
57+
private static void enableDebugChromeOutput() {
58+
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
59+
loggerContext.getLogger("com.github.kklisura.cdt.launch.chrome.output").setLevel(Level.DEBUG);
60+
}
61+
62+
public static void main(String[] args) throws InterruptedException {
63+
enableDebugChromeOutput();
64+
65+
// Create chrome launcher.
66+
final ChromeLauncher launcher = new ChromeLauncher();
67+
68+
// Launch chrome either as headless (true) or regular (false).
69+
final ChromeService chromeService =
70+
launcher.launch(
71+
ChromeArguments.defaults(false)
72+
// Sets the correct arguments: enable-logging and logging level
73+
.enableLogging("stderr")
74+
.additionalArguments("v", "1")
75+
.build());
76+
77+
// Create empty tab ie about:blank.
78+
final ChromeTab tab = chromeService.createTab();
79+
80+
// Get DevTools service to this tab
81+
final ChromeDevToolsService devToolsService = chromeService.createDevToolsService(tab);
82+
83+
// Wait a bit and then close the browser
84+
Thread.sleep(2000);
85+
86+
devToolsService.close();
87+
launcher.close();
88+
}
89+
}

cdt-examples/src/resources/logback.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
</encoder>
99
</appender>
1010

11+
<!-- <logger name="com.github.kklisura.cdt.launch.chrome.output" level="DEBUG" />-->
12+
1113
<root level="INFO">
1214
<appender-ref ref="STDOUT" />
1315
</root>

cdt-java-client/src/main/java/com/github/kklisura/cdt/launch/ChromeArguments.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ public class ChromeArguments {
9595
@ChromeArgument("safebrowsing-disable-auto-update")
9696
private Boolean safebrowsingDisableAutoUpdate;
9797

98+
@ChromeArgument("enable-logging")
99+
private String enableLogging;
100+
98101
private Map<String, Object> additionalArguments;
99102

100103
/**
@@ -299,6 +302,15 @@ public Boolean getSafebrowsingDisableAutoUpdate() {
299302
return safebrowsingDisableAutoUpdate;
300303
}
301304

305+
/**
306+
* Gets the enable-logging argument.
307+
*
308+
* @return Enable logging argument.
309+
*/
310+
public String getEnableLogging() {
311+
return enableLogging;
312+
}
313+
302314
/**
303315
* Builder builder.
304316
*
@@ -777,6 +789,16 @@ public Builder safebrowsingDisableAutoUpdate() {
777789
return safebrowsingDisableAutoUpdate(Boolean.TRUE);
778790
}
779791

792+
/**
793+
* Sets the enable-logging argument.
794+
*
795+
* @return This builder.
796+
*/
797+
public Builder enableLogging(String enableLogging) {
798+
arguments.enableLogging = enableLogging;
799+
return this;
800+
}
801+
780802
/**
781803
* Build chrome arguments.
782804
*

cdt-java-client/src/main/java/com/github/kklisura/cdt/launch/ChromeLauncher.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,7 @@ private int waitForDevToolsServer(final Process process) throws ChromeProcessTim
374374
}
375375
});
376376

377+
readLineThread.setName("chrome-launcher:read-line-thread");
377378
readLineThread.start();
378379

379380
try {

0 commit comments

Comments
 (0)