Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[java] Add nullness for logging #15108

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading