Skip to content

Commit

Permalink
[grid] Add log-level config option (#8968)
Browse files Browse the repository at this point in the history
* [grid] Add log-level config option

* [grid] Remove extra checks for configuring log-levels. Undo event-log level change.
  • Loading branch information
pujagani committed Dec 15, 2020
1 parent 2cf77cc commit 6ac3db6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.openqa.selenium.grid.config.Role;

import java.util.Set;
import java.util.logging.Level;

import static org.openqa.selenium.grid.config.StandardGridRoles.ALL_ROLES;

Expand Down Expand Up @@ -54,6 +55,12 @@ public class LoggingFlags implements HasRoles {
@ConfigValue(section = "logging", name = "log-encoding", example = "UTF-8")
private String logEncoding = null;

@Parameter(description = "Log level. Default logging level is INFO." +
"Log levels are described here https://docs.oracle.com/javase/7/docs/api/java/util/logging/Level.html ",
names = "--log-level", arity = 1)
@ConfigValue(section = "logging", name = "log-level", example = "INFO")
private String logLevel = Level.INFO.getName();

@Override
public Set<Role> getRoles() {
return ALL_ROLES;
Expand Down
28 changes: 28 additions & 0 deletions java/server/src/org/openqa/selenium/grid/log/LoggingOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ public class LoggingOptions {

public static final Json JSON = new Json();

private Level level = Level.INFO;

private final Config config;

public LoggingOptions(Config config) {
Expand All @@ -93,6 +95,28 @@ public String getLogEncoding() {
return config.get(LOGGING_SECTION, "log-encoding").orElse(null);
}

public void setLoggingLevel() {
String configLevel = config.get(LOGGING_SECTION, "log-level").orElse(Level.INFO.getName());

if (Level.ALL.getName().equalsIgnoreCase(configLevel)) {
level = Level.ALL;
} else if (Level.CONFIG.getName().equalsIgnoreCase(configLevel)) {
level = Level.CONFIG;
} else if (Level.FINE.getName().equalsIgnoreCase(configLevel)) {
level = Level.FINE;
} else if (Level.FINER.getName().equalsIgnoreCase(configLevel)) {
level = Level.FINER;
} else if (Level.FINEST.getName().equalsIgnoreCase(configLevel)) {
level = Level.FINEST;
} else if (Level.OFF.getName().equalsIgnoreCase(configLevel)) {
level = Level.OFF;
} else if (Level.SEVERE.getName().equalsIgnoreCase(configLevel)) {
level = Level.SEVERE;
} else if (Level.WARNING.getName().equalsIgnoreCase(configLevel)) {
level = Level.WARNING;
}
}

public Tracer getTracer() {
boolean tracingEnabled = config.getBool(LOGGING_SECTION, "tracing").orElse(true);
if (!tracingEnabled) {
Expand Down Expand Up @@ -209,18 +233,22 @@ public void configureLogging() {

// Now configure the root logger, since everything should flow up to that
Logger logger = logManager.getLogger("");
setLoggingLevel();
logger.setLevel(level);
OutputStream out = getOutputStream();
String encoding = getLogEncoding();

if (isUsingPlainLogs()) {
Handler handler = new FlushingHandler(out);
handler.setFormatter(new TerseFormatter());
handler.setLevel(level);
configureLogEncoding(logger, encoding, handler);
}

if (isUsingStructuredLogging()) {
Handler handler = new FlushingHandler(out);
handler.setFormatter(new JsonFormatter());
handler.setLevel(level);
configureLogEncoding(logger, encoding, handler);
}
}
Expand Down

0 comments on commit 6ac3db6

Please sign in to comment.