0
# Logging Domain
1
2
Log domain implementation for browser log capture and management. Provides access to browser console logs with level filtering, timestamp information, and automatic conversion between CDP log formats and Java logging standards.
3
4
## Capabilities
5
6
### Log Control
7
8
Enable and disable browser logging capture.
9
10
```java { .api }
11
public Command<Void> enable();
12
public Command<Void> clear();
13
```
14
15
Usage example:
16
```java
17
v111Log log = domains.log();
18
19
// Enable log capture
20
devTools.send(log.enable());
21
22
// Clear existing logs
23
devTools.send(log.clear());
24
25
// Later disable when done (optional - usually left enabled)
26
// Note: There's no disable() method, logs remain enabled once started
27
```
28
29
### Log Entry Monitoring
30
31
Monitor log entries added to the browser console and receive them as structured events.
32
33
```java { .api }
34
public Event<org.openqa.selenium.devtools.idealized.log.model.LogEntry> entryAdded();
35
```
36
37
Usage example:
38
```java
39
// Listen for log entries
40
devTools.addListener(log.entryAdded(), logEntry -> {
41
System.out.printf("[%s] %s: %s%n",
42
logEntry.getSource(),
43
logEntry.getEntry().getLevel(),
44
logEntry.getEntry().getMessage());
45
46
// Access timestamp
47
long timestamp = logEntry.getEntry().getTimestamp();
48
System.out.println("Logged at: " + Instant.ofEpochMilli(timestamp));
49
});
50
51
// Browser actions that generate logs:
52
// - JavaScript errors: throw new Error("Something failed");
53
// - Network errors: fetch('/nonexistent-endpoint');
54
// - Security violations: Access denied errors
55
// - Extension messages: Chrome extension logging
56
```
57
58
### Log Level Handling
59
60
The logging domain automatically converts CDP log levels to Java logging levels:
61
62
```java
63
// CDP levels -> Java levels mapping:
64
// "verbose" -> Level.FINEST
65
// "info" -> Level.INFO
66
// "warning" -> Level.WARNING
67
// "error" -> Level.SEVERE
68
// default -> Level.INFO
69
```
70
71
Usage example with level filtering:
72
```java
73
devTools.addListener(log.entryAdded(), logEntry -> {
74
LogEntry entry = logEntry.getEntry();
75
Level level = entry.getLevel();
76
77
// Only process errors and warnings
78
if (level.intValue() >= Level.WARNING.intValue()) {
79
System.err.println("Important log: " + entry.getMessage());
80
81
// Could save to file, send alert, etc.
82
if (level.equals(Level.SEVERE)) {
83
alertErrorMonitoring(entry);
84
}
85
}
86
});
87
```
88
89
### Timestamp Conversion
90
91
Automatic conversion of CDP timestamps to Java milliseconds with fallback handling:
92
93
```java
94
private long fromCdpTimestamp(Timestamp timestamp) {
95
try {
96
return Long.parseLong(timestamp.toString());
97
} catch (NumberFormatException e) {
98
return System.currentTimeMillis(); // Fallback to current time
99
}
100
}
101
```
102
103
## Complete Logging Example
104
105
```java
106
public class BrowserLogMonitor {
107
private final DevTools devTools;
108
private final v111Log log;
109
private final List<LogEntry> collectedLogs = new ArrayList<>();
110
111
public void startMonitoring() {
112
// Enable logging
113
devTools.send(log.enable());
114
115
// Clear any existing logs
116
devTools.send(log.clear());
117
118
// Set up log collection
119
devTools.addListener(log.entryAdded(), this::handleLogEntry);
120
}
121
122
private void handleLogEntry(org.openqa.selenium.devtools.idealized.log.model.LogEntry logEntry) {
123
LogEntry entry = logEntry.getEntry();
124
String source = logEntry.getSource();
125
126
// Collect log for later analysis
127
collectedLogs.add(entry);
128
129
// Real-time processing based on level
130
Level level = entry.getLevel();
131
String message = entry.getMessage();
132
133
switch (level.getName()) {
134
case "SEVERE":
135
System.err.println("[ERROR] " + source + ": " + message);
136
// Could trigger alerts, save to error log file, etc.
137
handleError(source, message, entry.getTimestamp());
138
break;
139
140
case "WARNING":
141
System.out.println("[WARN] " + source + ": " + message);
142
break;
143
144
case "INFO":
145
if (isVerboseLogging()) {
146
System.out.println("[INFO] " + source + ": " + message);
147
}
148
break;
149
150
case "FINEST":
151
if (isDebugLogging()) {
152
System.out.println("[DEBUG] " + source + ": " + message);
153
}
154
break;
155
}
156
}
157
158
public List<LogEntry> getErrorLogs() {
159
return collectedLogs.stream()
160
.filter(log -> log.getLevel().equals(Level.SEVERE))
161
.collect(Collectors.toList());
162
}
163
164
public List<LogEntry> getLogsAfter(long timestamp) {
165
return collectedLogs.stream()
166
.filter(log -> log.getTimestamp() > timestamp)
167
.collect(Collectors.toList());
168
}
169
170
public void clearCollectedLogs() {
171
collectedLogs.clear();
172
devTools.send(log.clear()); // Clear browser logs too
173
}
174
}
175
```
176
177
## Types
178
179
### Log Entry Types
180
181
```java { .api }
182
// Idealized log entry (what you receive in events)
183
class org.openqa.selenium.devtools.idealized.log.model.LogEntry {
184
public LogEntry(String source, org.openqa.selenium.logging.LogEntry entry);
185
public String getSource(); // Log source: "javascript", "network", "security", etc.
186
public org.openqa.selenium.logging.LogEntry getEntry(); // Standard Selenium log entry
187
}
188
189
// Standard Selenium log entry
190
class org.openqa.selenium.logging.LogEntry {
191
public Level getLevel(); // Java logging level
192
public long getTimestamp(); // Timestamp in milliseconds
193
public String getMessage(); // Log message text
194
}
195
```
196
197
### CDP Log Types
198
199
```java { .api }
200
// CDP-specific log entry (internal)
201
class LogEntry {
202
public enum Level {
203
VERBOSE, INFO, WARNING, ERROR
204
}
205
206
public Level getLevel(); // CDP log level
207
public String getText(); // Log message
208
public String getSource(); // Log source
209
public Timestamp getTimestamp(); // CDP timestamp
210
}
211
212
class Timestamp {
213
// CDP timestamp representation
214
// Converted to long milliseconds by the domain
215
}
216
```
217
218
## Log Sources
219
220
Common log sources you'll encounter:
221
222
- **javascript**: JavaScript runtime errors, console.error() calls
223
- **network**: Failed HTTP requests, CORS errors, certificate issues
224
- **security**: Content Security Policy violations, mixed content warnings
225
- **deprecation**: Deprecated API usage warnings
226
- **intervention**: Browser interventions (e.g., blocking slow scripts)
227
- **recommendation**: Performance recommendations
228
- **other**: Miscellaneous browser messages
229
230
## Integration with Console Events
231
232
The Log domain is complementary to the Events domain:
233
234
- **Log Domain**: Captures broader browser logging including network, security, and system messages
235
- **Events Domain**: Captures specific console API calls (console.log, console.error) with detailed argument information
236
237
For comprehensive monitoring, use both:
238
239
```java
240
// Monitor broad browser logs
241
devTools.addListener(log.entryAdded(), this::handleBrowserLog);
242
243
// Monitor specific console API calls
244
devTools.addListener(events.consoleEvent(), this::handleConsoleCall);
245
```