Skip to content

Commit

Permalink
[grid] Add log-level config option
Browse files Browse the repository at this point in the history
  • Loading branch information
pujagani committed Dec 14, 2020
1 parent 6227b66 commit 2c1d00c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
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
34 changes: 33 additions & 1 deletion java/server/src/org/openqa/selenium/grid/log/LoggingOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,35 @@ public String getLogEncoding() {
return config.get(LOGGING_SECTION, "log-encoding").orElse(null);
}

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

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.INFO.getName().equalsIgnoreCase(configLevel)) {
level = Level.INFO;
} 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;
} else {
// Default logging level in case of incorrect input.
level = Level.INFO;
}
return level;
}

public Tracer getTracer() {
boolean tracingEnabled = config.getBool(LOGGING_SECTION, "tracing").orElse(true);
if (!tracingEnabled) {
Expand Down Expand Up @@ -151,7 +180,7 @@ public <T> void accept(AttributeKey<T> key, T value) {
map.put("attributes", attributeMap);
String jsonString = getJsonString(map);
if (status.isOk()) {
LOG.log(Level.INFO, jsonString);
LOG.log(Level.FINE, jsonString);
} else {
LOG.log(Level.WARNING, jsonString);
}
Expand Down Expand Up @@ -209,18 +238,21 @@ public void configureLogging() {

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

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

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

0 comments on commit 2c1d00c

Please sign in to comment.