0
# Logging
1
2
The v129Log class provides browser log management and console message capture through the Chrome DevTools Protocol. It enables log domain control, log clearing, and real-time log entry monitoring.
3
4
## Capabilities
5
6
### Log Domain
7
8
Core log domain for browser log management.
9
10
```java { .api }
11
/**
12
* Log domain implementation for browser log access
13
* Implements the idealized Log interface
14
*/
15
public class v129Log implements org.openqa.selenium.devtools.idealized.log.Log {
16
public v129Log();
17
}
18
```
19
20
### Log Domain Control
21
22
Enable and manage the Log domain for capturing browser logs.
23
24
```java { .api }
25
/**
26
* Enables the Log domain to start capturing log entries
27
* @return Command to enable Log domain
28
*/
29
public Command<Void> enable();
30
31
/**
32
* Clears all existing log entries
33
* @return Command to clear browser logs
34
*/
35
public Command<Void> clear();
36
```
37
38
### Log Entry Monitoring
39
40
Monitor and capture log entries as they are generated.
41
42
```java { .api }
43
/**
44
* Event fired when a new log entry is added
45
* @return Event for log entry additions with processed LogEntry
46
*/
47
public Event<org.openqa.selenium.devtools.idealized.log.model.LogEntry> entryAdded();
48
```
49
50
**Usage Examples:**
51
52
```java
53
import org.openqa.selenium.devtools.v129.v129Log;
54
import org.openqa.selenium.devtools.DevTools;
55
import org.openqa.selenium.devtools.idealized.log.model.LogEntry;
56
import java.util.logging.Level;
57
58
// Create log domain
59
DevTools devTools = driver.getDevTools();
60
v129Log log = new v129Log();
61
62
// Enable log domain
63
devTools.send(log.enable());
64
65
// Listen for log entries
66
devTools.addListener(log.entryAdded(), logEntry -> {
67
System.out.println("Log [" + logEntry.getLevel() + "] " +
68
logEntry.getSource() + ": " +
69
logEntry.getText());
70
71
// Process different log levels
72
if (logEntry.getLevel() == Level.SEVERE) {
73
System.err.println("SEVERE log detected: " + logEntry.getText());
74
}
75
});
76
77
// Clear existing logs
78
devTools.send(log.clear());
79
80
// Navigate to generate logs
81
driver.get("https://example.com");
82
83
// Generate browser logs through JavaScript
84
driver.executeScript("console.log('Test log message');");
85
driver.executeScript("console.error('Test error message');");
86
driver.executeScript("console.warn('Test warning message');");
87
```
88
89
### Advanced Log Processing
90
91
```java
92
import java.util.concurrent.ConcurrentLinkedQueue;
93
import java.util.Queue;
94
95
// Collect logs for analysis
96
Queue<LogEntry> collectedLogs = new ConcurrentLinkedQueue<>();
97
98
devTools.addListener(log.entryAdded(), logEntry -> {
99
collectedLogs.offer(logEntry);
100
101
// Real-time log analysis
102
analyzeLogEntry(logEntry);
103
});
104
105
private void analyzeLogEntry(LogEntry entry) {
106
String source = entry.getSource();
107
String text = entry.getText();
108
Level level = entry.getLevel();
109
110
// Filter by source
111
switch (source) {
112
case "console-api":
113
handleConsoleLog(entry);
114
break;
115
case "javascript":
116
handleJavaScriptLog(entry);
117
break;
118
case "network":
119
handleNetworkLog(entry);
120
break;
121
case "security":
122
handleSecurityLog(entry);
123
break;
124
default:
125
handleGenericLog(entry);
126
}
127
128
// Filter by level
129
if (level == Level.SEVERE) {
130
// Alert on severe errors
131
alertOnSevereError(entry);
132
}
133
}
134
135
// Log aggregation and reporting
136
public void generateLogReport() {
137
Map<String, Long> logsBySource = collectedLogs.stream()
138
.collect(Collectors.groupingBy(
139
LogEntry::getSource,
140
Collectors.counting()
141
));
142
143
Map<Level, Long> logsByLevel = collectedLogs.stream()
144
.collect(Collectors.groupingBy(
145
LogEntry::getLevel,
146
Collectors.counting()
147
));
148
149
System.out.println("Log Summary:");
150
System.out.println("By Source: " + logsBySource);
151
System.out.println("By Level: " + logsByLevel);
152
}
153
```
154
155
### Log Level Conversion
156
157
The v129Log class automatically converts CDP log levels to Java logging levels.
158
159
```java
160
// CDP Level -> Java Level conversion
161
// "verbose" -> Level.FINEST
162
// "info" -> Level.INFO
163
// "warning" -> Level.WARNING
164
// "error" -> Level.SEVERE
165
// default -> Level.INFO
166
167
private Level fromCdpLevel(LogEntry.Level level) {
168
switch (level.toString()) {
169
case "verbose":
170
return Level.FINEST;
171
case "info":
172
return Level.INFO;
173
case "warning":
174
return Level.WARNING;
175
case "error":
176
return Level.SEVERE;
177
default:
178
return Level.INFO;
179
}
180
}
181
```
182
183
## Types
184
185
### Log Entry Types
186
187
```java { .api }
188
// From v129.log.model package
189
import org.openqa.selenium.devtools.v129.log.model.LogEntry;
190
191
// From idealized log model
192
import org.openqa.selenium.devtools.idealized.log.model.LogEntry as Idealized;
193
194
// Java logging types
195
import java.util.logging.Level;
196
import java.util.logging.LogRecord;
197
```
198
199
### Log Entry Structure
200
201
```java { .api }
202
// Idealized LogEntry returned by entryAdded()
203
class LogEntry {
204
public LogEntry(String source, java.util.logging.LogEntry entry);
205
206
public String getSource(); // Log source (console-api, javascript, etc.)
207
public String getText(); // Log message text
208
public Level getLevel(); // Java logging level
209
public long getTimestamp(); // Timestamp in milliseconds
210
}
211
```
212
213
### CDP Log Entry Details
214
215
```java { .api }
216
// Original CDP LogEntry structure
217
class LogEntry {
218
public enum Level {
219
VERBOSE, INFO, WARNING, ERROR
220
}
221
222
public String getSource();
223
public Level getLevel();
224
public String getText();
225
public Timestamp getTimestamp();
226
public Optional<String> getUrl();
227
public Optional<Integer> getLineNumber();
228
public Optional<Integer> getColumnNumber();
229
}
230
```
231
232
### Timestamp Handling
233
234
```java { .api }
235
// Timestamp conversion from CDP to Java
236
import org.openqa.selenium.devtools.v129.runtime.model.Timestamp;
237
238
// Conversion method in v129Log
239
private long fromCdpTimestamp(Timestamp timestamp) {
240
try {
241
return Long.parseLong(timestamp.toString());
242
} catch (NumberFormatException e) {
243
return System.currentTimeMillis();
244
}
245
}
246
```
247
248
### Command Types
249
250
```java { .api }
251
// Log domain commands from v129.log package
252
import org.openqa.selenium.devtools.v129.log.Log;
253
import org.openqa.selenium.devtools.Command;
254
import org.openqa.selenium.devtools.Event;
255
256
// Available commands
257
Log.enable(); // Enable log domain
258
Log.clear(); // Clear existing logs
259
Log.entryAdded(); // Log entry event
260
```