0
# Logging System
1
2
Access to browser logs including console logs, network logs, and other browser-generated log messages. Provides structured access to Chrome's logging system with proper log level conversion.
3
4
## Capabilities
5
6
### Log Domain Control
7
8
Enable and manage the Chrome DevTools logging domain for comprehensive log access.
9
10
```java { .api }
11
/**
12
* Initialize logging handler
13
*/
14
public V105Log();
15
16
/**
17
* Enable logging domain to start receiving log events
18
* @return Command to enable logging domain
19
*/
20
public Command<Void> enable();
21
22
/**
23
* Clear all browser logs
24
* @return Command to clear browser log entries
25
*/
26
public Command<Void> clear();
27
```
28
29
**Usage Examples:**
30
31
```java
32
import org.openqa.selenium.devtools.v105.V105Log;
33
34
// Initialize logging
35
V105Log log = new V105Log();
36
37
// Enable logging domain
38
devTools.send(log.enable());
39
40
// Clear existing logs
41
devTools.send(log.clear());
42
43
// Navigate and generate some logs
44
driver.get("https://example.com");
45
46
// Disable logging when done
47
// (Note: V105Log doesn't expose disable, logs remain active until session ends)
48
```
49
50
### Log Entry Monitoring
51
52
Monitor and capture log entries as they are generated by the browser.
53
54
```java { .api }
55
/**
56
* Get log entry added event stream
57
* @return Event stream for new log entries
58
*/
59
public Event<org.openqa.selenium.devtools.idealized.log.model.LogEntry> entryAdded();
60
```
61
62
**Usage Examples:**
63
64
```java
65
import org.openqa.selenium.devtools.idealized.log.model.LogEntry;
66
import java.util.logging.Level;
67
68
// Set up log monitoring
69
log.entryAdded().addListener(logEntry -> {
70
System.out.println("Log Source: " + logEntry.getSource());
71
System.out.println("Level: " + logEntry.getLogEntry().getLevel());
72
System.out.println("Message: " + logEntry.getLogEntry().getMessage());
73
System.out.println("Timestamp: " + logEntry.getLogEntry().getMillis());
74
System.out.println("---");
75
});
76
77
// Generate different types of logs
78
devTools.send(Runtime.evaluate("console.log('Info message')"));
79
devTools.send(Runtime.evaluate("console.warn('Warning message')"));
80
devTools.send(Runtime.evaluate("console.error('Error message')"));
81
82
// Navigate to trigger network logs
83
driver.get("https://httpbin.org/status/404"); // Will generate network error logs
84
```
85
86
### Log Level Conversion
87
88
Automatic conversion between Chrome DevTools Protocol log levels and Java logging levels.
89
90
```java { .api }
91
/**
92
* Convert CDP log level to Java logging level
93
* @param level CDP log level
94
* @return Java logging Level
95
*/
96
private Level fromCdpLevel(LogEntry.Level level);
97
98
/**
99
* Convert CDP timestamp to epoch milliseconds
100
* @param timestamp CDP timestamp
101
* @return Epoch milliseconds as long
102
*/
103
private long fromCdpTimestamp(Timestamp timestamp);
104
```
105
106
**Usage Examples:**
107
108
```java
109
// The conversion happens automatically in entryAdded() events
110
log.entryAdded().addListener(logEntry -> {
111
Level javaLevel = logEntry.getLogEntry().getLevel();
112
113
// Handle different log levels
114
if (javaLevel.equals(Level.SEVERE)) {
115
handleError(logEntry);
116
} else if (javaLevel.equals(Level.WARNING)) {
117
handleWarning(logEntry);
118
} else if (javaLevel.equals(Level.INFO)) {
119
handleInfo(logEntry);
120
} else if (javaLevel.equals(Level.FINEST)) {
121
handleVerbose(logEntry);
122
}
123
});
124
125
private void handleError(LogEntry logEntry) {
126
System.err.println("ERROR: " + logEntry.getLogEntry().getMessage());
127
// Log to file, send alert, etc.
128
}
129
130
private void handleWarning(LogEntry logEntry) {
131
System.out.println("WARNING: " + logEntry.getLogEntry().getMessage());
132
// Track warnings for analysis
133
}
134
```
135
136
### Advanced Log Filtering
137
138
Filter and process logs based on source and content.
139
140
**Usage Examples:**
141
142
```java
143
import java.util.concurrent.ConcurrentHashMap;
144
import java.util.concurrent.atomic.AtomicInteger;
145
146
public class LogAnalyzer {
147
private final Map<String, AtomicInteger> logCounts = new ConcurrentHashMap<>();
148
private final List<LogEntry> errorLogs = new ArrayList<>();
149
150
public void startLogAnalysis(V105Log log) {
151
log.entryAdded().addListener(this::analyzeLogEntry);
152
}
153
154
private void analyzeLogEntry(LogEntry logEntry) {
155
String source = logEntry.getSource();
156
Level level = logEntry.getLogEntry().getLevel();
157
String message = logEntry.getLogEntry().getMessage();
158
159
// Count logs by source
160
logCounts.computeIfAbsent(source, k -> new AtomicInteger(0)).incrementAndGet();
161
162
// Collect error logs for analysis
163
if (level.equals(Level.SEVERE)) {
164
errorLogs.add(logEntry);
165
}
166
167
// Filter specific patterns
168
if (message.contains("Failed to load resource")) {
169
handleResourceLoadError(logEntry);
170
} else if (message.contains("CSP")) {
171
handleSecurityPolicyViolation(logEntry);
172
} else if (source.equals("network") && level.equals(Level.WARNING)) {
173
handleNetworkWarning(logEntry);
174
}
175
}
176
177
public void printLogSummary() {
178
System.out.println("Log Summary:");
179
logCounts.forEach((source, count) ->
180
System.out.println(source + ": " + count.get() + " entries"));
181
182
System.out.println("Total errors: " + errorLogs.size());
183
}
184
}
185
186
// Usage
187
LogAnalyzer analyzer = new LogAnalyzer();
188
analyzer.startLogAnalysis(log);
189
190
// Run tests or navigate pages
191
driver.get("https://example.com");
192
193
// Get summary
194
analyzer.printLogSummary();
195
```
196
197
## Types
198
199
### Core Logging Types
200
201
```java { .api }
202
// Logging implementation
203
public class V105Log implements org.openqa.selenium.devtools.idealized.log.Log {
204
public V105Log();
205
206
Command<Void> enable();
207
Command<Void> clear();
208
Event<org.openqa.selenium.devtools.idealized.log.model.LogEntry> entryAdded();
209
}
210
211
// Idealized log entry for Selenium
212
public class org.openqa.selenium.devtools.idealized.log.model.LogEntry {
213
LogEntry(String source, org.openqa.selenium.logging.LogEntry logEntry);
214
215
String getSource();
216
org.openqa.selenium.logging.LogEntry getLogEntry();
217
}
218
219
// Selenium log entry
220
public class org.openqa.selenium.logging.LogEntry {
221
LogEntry(Level level, long timestamp, String message);
222
223
Level getLevel();
224
long getMillis();
225
String getMessage();
226
}
227
```
228
229
### CDP Log Types
230
231
```java { .api }
232
// CDP log entry
233
public class org.openqa.selenium.devtools.v105.log.model.LogEntry {
234
LogEntry.Level getLevel();
235
String getText();
236
String getSource();
237
Timestamp getTimestamp();
238
Optional<String> getUrl();
239
Optional<Integer> getLineNumber();
240
Optional<StackTrace> getStackTrace();
241
}
242
243
// CDP log levels
244
public enum LogEntry.Level {
245
VERBOSE("verbose"),
246
INFO("info"),
247
WARNING("warning"),
248
ERROR("error");
249
250
String toString();
251
}
252
253
// CDP timestamp
254
public class Timestamp {
255
Timestamp(Number timestamp);
256
257
Number toJson();
258
String toString();
259
}
260
```
261
262
### Java Logging Integration
263
264
```java { .api }
265
// Java logging levels used in conversion
266
public class Level {
267
public static final Level FINEST; // Maps from CDP "verbose"
268
public static final Level INFO; // Maps from CDP "info"
269
public static final Level WARNING; // Maps from CDP "warning"
270
public static final Level SEVERE; // Maps from CDP "error"
271
}
272
```
273
274
### Log Sources
275
276
Common log sources you may encounter:
277
278
- **"console"** - Console API messages (console.log, console.error, etc.)
279
- **"network"** - Network-related messages and errors
280
- **"security"** - Security policy violations and warnings
281
- **"deprecation"** - Deprecated API usage warnings
282
- **"worker"** - Web Worker and Service Worker messages
283
- **"storage"** - Storage-related messages (localStorage, sessionStorage, etc.)
284
- **"appcache"** - Application Cache messages (deprecated)
285
- **"rendering"** - Rendering engine messages
286
- **"javascript"** - JavaScript engine messages
287
- **"xml"** - XML parsing messages