Skip to content

Commit

Permalink
[java] Add nullness for logging (#15108)
Browse files Browse the repository at this point in the history
Co-authored-by: Viet Nguyen Duc <[email protected]>
  • Loading branch information
mk868 and VietND96 authored Jan 20, 2025
1 parent 3c16d81 commit 4d02e6b
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 5 deletions.
2 changes: 2 additions & 0 deletions java/src/org/openqa/selenium/logging/LogEntries.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@
import java.util.Iterator;
import java.util.List;
import java.util.stream.StreamSupport;
import org.jspecify.annotations.NullMarked;
import org.openqa.selenium.Beta;

/**
* Represent a pool of {@link LogEntry}. This class also provides filtering mechanisms based on
* levels.
*/
@Beta
@NullMarked
public class LogEntries implements Iterable<LogEntry> {

private final List<LogEntry> entries;
Expand Down
2 changes: 2 additions & 0 deletions java/src/org/openqa/selenium/logging/LogEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import org.jspecify.annotations.NullMarked;

/** Represents a single log statement. */
@NullMarked
public class LogEntry {

private final Level level;
Expand Down
15 changes: 11 additions & 4 deletions java/src/org/openqa/selenium/logging/LogLevelMapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,20 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.logging.Level;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

@NullMarked
public class LogLevelMapping {

/** WebDriver log level DEBUG which is mapped to Level.FINE. */
private static final String DEBUG = "DEBUG";

// Default the log level to info.
private static final Level DEFAULT_LEVEL = Level.INFO;

private static final Map<Integer, Level> levelMap;

static {
Expand Down Expand Up @@ -70,15 +77,15 @@ public static String getName(Level level) {
return normalized == Level.FINE ? DEBUG : normalized.getName();
}

public static Level toLevel(String logLevelName) {
public static Level toLevel(@Nullable String logLevelName) {
if (logLevelName == null || logLevelName.isEmpty()) {
// Default the log level to info.
return Level.INFO;
return DEFAULT_LEVEL;
}

if (logLevelName.equals(DEBUG)) {
return Level.FINE;
}
return levelMap.get(Level.parse(logLevelName).intValue());
return Optional.ofNullable(levelMap.get(Level.parse(logLevelName).intValue()))
.orElse(DEFAULT_LEVEL);
}
}
5 changes: 4 additions & 1 deletion java/src/org/openqa/selenium/logging/SessionLogs.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.jspecify.annotations.NullMarked;
import org.openqa.selenium.Beta;
import org.openqa.selenium.internal.Require;

/** Contains the logs for a session divided by supported log types. */
@Beta
@NullMarked
public class SessionLogs {
private final Map<String, LogEntries> logTypeToEntriesMap;

Expand Down Expand Up @@ -66,7 +69,7 @@ public static SessionLogs fromJSON(Map<String, Object> rawSessionLogs) {
logEntries.add(
new LogEntry(
LogLevelMapping.toLevel(String.valueOf(rawEntry.get("level"))),
((Number) rawEntry.get("timestamp")).longValue(),
Require.nonNull("timestamp", (Number) rawEntry.get("timestamp")).longValue(),
String.valueOf(rawEntry.get("message"))));
}
sessionLogs.addLog(logType, new LogEntries(logEntries));
Expand Down
1 change: 1 addition & 0 deletions java/test/org/openqa/selenium/logging/LoggingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ void testLogLevelConversions() {
assertThat(toLevel("WARNING")).isEqualTo(WARNING);
assertThat(toLevel("SEVERE")).isEqualTo(SEVERE);
assertThat(toLevel("OFF")).isEqualTo(OFF);
assertThat(toLevel(null)).isEqualTo(INFO);
}

@Test
Expand Down

0 comments on commit 4d02e6b

Please sign in to comment.