0
# Console Logging
1
2
Console log management and event streaming for Chrome DevTools Protocol v138. Provides access to browser console output with level filtering, timestamp conversion, and comprehensive log event handling.
3
4
## Capabilities
5
6
### Log Handler
7
8
Main console logging handler providing access to browser console output.
9
10
```java { .api }
11
/**
12
* Console logging functionality for CDP v138
13
* Default constructor - stateless implementation
14
*/
15
public class v138Log implements org.openqa.selenium.devtools.idealized.log.Log {
16
// No explicit constructor - uses default constructor
17
}
18
```
19
20
**Usage Example:**
21
22
```java
23
import org.openqa.selenium.devtools.v138.v138Log;
24
import org.openqa.selenium.devtools.DevTools;
25
26
v138Log log = new v138Log();
27
28
// Enable console logging
29
devTools.send(log.enable());
30
31
// Listen for log entries
32
devTools.addListener(log.entryAdded(), logEntry -> {
33
System.out.printf("[%s] %s: %s%n",
34
logEntry.getSource(),
35
logEntry.getEntry().getLevel(),
36
logEntry.getEntry().getMessage());
37
});
38
```
39
40
### Log Domain Control
41
42
Enables and manages the Log domain for console output capture.
43
44
```java { .api }
45
/**
46
* Enable Log domain to start capturing console output
47
* @return Command to enable log domain
48
*/
49
@Override
50
public Command<Void> enable();
51
52
/**
53
* Clear all console logs
54
* @return Command to clear console buffer
55
*/
56
@Override
57
public Command<Void> clear();
58
```
59
60
**Usage Example:**
61
62
```java
63
// Enable logging
64
devTools.send(log.enable());
65
66
// Navigate and generate some console output
67
driver.get("https://example.com");
68
driver.executeScript("console.log('Page loaded'); console.error('Test error');");
69
70
// Clear the console
71
devTools.send(log.clear());
72
73
// Generate more output - previous entries are cleared
74
driver.executeScript("console.warn('After clear');");
75
```
76
77
### Log Event Stream
78
79
Provides access to console log entries as they are generated.
80
81
```java { .api }
82
/**
83
* Stream of console log entry events
84
* @return Event stream for log entries from browser console
85
*/
86
@Override
87
public Event<LogEntry> entryAdded();
88
89
/**
90
* Convert CDP log level to Java logging level
91
* @param level CDP log level
92
* @return Java logging Level
93
*/
94
private Level fromCdpLevel(LogEntry.Level level);
95
96
/**
97
* Convert CDP timestamp to milliseconds
98
* @param timestamp CDP timestamp
99
* @return Timestamp in milliseconds
100
*/
101
private long fromCdpTimestamp(Timestamp timestamp);
102
```
103
104
**Usage Example:**
105
106
```java
107
// Listen for different log levels
108
devTools.addListener(log.entryAdded(), logEntry -> {
109
org.openqa.selenium.logging.LogEntry entry = logEntry.getEntry();
110
111
switch (entry.getLevel().getName()) {
112
case "SEVERE":
113
System.err.println("ERROR: " + entry.getMessage());
114
break;
115
case "WARNING":
116
System.out.println("WARN: " + entry.getMessage());
117
break;
118
case "INFO":
119
System.out.println("INFO: " + entry.getMessage());
120
break;
121
case "FINE":
122
case "FINER":
123
case "FINEST":
124
System.out.println("DEBUG: " + entry.getMessage());
125
break;
126
default:
127
System.out.println("LOG: " + entry.getMessage());
128
}
129
});
130
131
// Enable logging to start receiving events
132
devTools.send(log.enable());
133
```
134
135
## Log Level Mapping
136
137
The v138Log implementation maps Chrome DevTools Protocol log levels to Java logging levels:
138
139
- **verbose** → `Level.FINEST`
140
- **info** → `Level.INFO`
141
- **warning** → `Level.WARNING`
142
- **error** → `Level.SEVERE`
143
- **default** → `Level.INFO` (for unknown levels)
144
145
## Complete Console Logging Example
146
147
```java
148
import org.openqa.selenium.chrome.ChromeDriver;
149
import org.openqa.selenium.devtools.DevTools;
150
import org.openqa.selenium.devtools.v138.v138Log;
151
import org.openqa.selenium.logging.LogEntry;
152
import java.util.logging.Level;
153
154
ChromeDriver driver = new ChromeDriver();
155
DevTools devTools = driver.getDevTools();
156
devTools.createSession();
157
158
v138Log log = new v138Log();
159
160
// Enable console logging
161
devTools.send(log.enable());
162
163
// Set up comprehensive log monitoring
164
devTools.addListener(log.entryAdded(), logEntry -> {
165
LogEntry entry = logEntry.getEntry();
166
Level level = entry.getLevel();
167
String source = logEntry.getSource();
168
long timestamp = entry.getTimestamp();
169
String message = entry.getMessage();
170
171
// Format log output with full details
172
System.out.printf("[%d] [%s] [%s] %s%n",
173
timestamp, source, level.getName(), message);
174
175
// Handle different log levels
176
if (level.intValue() >= Level.SEVERE.intValue()) {
177
// Log errors to separate error handling
178
handleError(message, timestamp);
179
} else if (level.intValue() >= Level.WARNING.intValue()) {
180
// Track warnings for analysis
181
trackWarning(message, timestamp);
182
}
183
});
184
185
// Navigate to page and execute JavaScript that generates logs
186
driver.get("https://example.com");
187
188
// Generate various console outputs
189
driver.executeScript("""
190
console.log('Application initialized');
191
console.info('User session started');
192
console.warn('Deprecated API usage detected');
193
console.error('Failed to load resource');
194
195
// Generate logs over time
196
setTimeout(() => console.log('Delayed log message'), 1000);
197
setTimeout(() => console.error('Async error'), 2000);
198
""");
199
200
// Wait for delayed logs
201
Thread.sleep(3000);
202
203
// Clear console for fresh start
204
devTools.send(log.clear());
205
System.out.println("Console cleared - new logs will appear below:");
206
207
// Generate post-clear logs
208
driver.executeScript("console.log('Post-clear message');");
209
210
// Clean up
211
devTools.close();
212
driver.quit();
213
214
// Helper methods for log processing
215
private static void handleError(String message, long timestamp) {
216
// Error handling logic
217
System.err.println("ERROR HANDLER: " + message + " at " + timestamp);
218
}
219
220
private static void trackWarning(String message, long timestamp) {
221
// Warning tracking logic
222
System.out.println("WARNING TRACKER: " + message + " at " + timestamp);
223
}
224
```
225
226
## Types
227
228
```java { .api }
229
// Log entry wrapper
230
class LogEntry {
231
String getSource(); // Source of the log entry ("javascript", "network", etc.)
232
org.openqa.selenium.logging.LogEntry getEntry(); // Selenium log entry
233
}
234
235
// Selenium log entry structure
236
class org.openqa.selenium.logging.LogEntry {
237
Level getLevel(); // Java logging level
238
long getTimestamp(); // Timestamp in milliseconds
239
String getMessage(); // Log message content
240
}
241
242
// CDP log levels (internal)
243
enum LogEntry.Level {
244
VERBOSE, // Detailed debug information
245
INFO, // General information
246
WARNING, // Warning conditions
247
ERROR // Error conditions
248
}
249
250
// CDP timestamp (internal)
251
class Timestamp {
252
String toString(); // String representation of timestamp
253
JsonElement toJson(); // JSON representation for parsing
254
}
255
```