Skip to content

Commit fe6b3c5

Browse files
Amil Uslupujagani
andauthored
[java] Log Timestamp flag added
Co-authored-by: Puja Jagani <[email protected]>
1 parent 09cd7e5 commit fe6b3c5

4 files changed

Lines changed: 16 additions & 6 deletions

File tree

java/src/org/openqa/selenium/grid/log/LoggingFlags.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ public class LoggingFlags implements HasRoles {
8686
@ConfigValue(section = LOGGING_SECTION, name = "log-level", example = "INFO")
8787
private String logLevel = DEFAULT_LOG_LEVEL;
8888

89+
@Parameter(description = "Format of the timestamp in log output", names = "--log-timestamp-format", arity = 1)
90+
@ConfigValue(section = LOGGING_SECTION, name = "log-timestamp-format", example = "HH:mm:ss.SSS")
91+
private String logTimestampFormat;
92+
8993
@Override
9094
public Set<Role> getRoles() {
9195
return ALL_ROLES;

java/src/org/openqa/selenium/grid/log/LoggingOptions.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public class LoggingOptions {
4949
private static final Logger LOG = Logger.getLogger(LoggingOptions.class.getName());
5050
private final Config config;
5151
private Level level = Level.INFO;
52+
public static final String DEFAULT_LOG_TIMESTAMP_FORMAT = "HH:mm:ss.SSS";
5253

5354
public LoggingOptions(Config config) {
5455
this.config = Require.nonNull("Config", config);
@@ -119,7 +120,7 @@ public void configureLogging() {
119120

120121
if (isUsingPlainLogs()) {
121122
Handler handler = new FlushingHandler(out);
122-
handler.setFormatter(new TerseFormatter());
123+
handler.setFormatter(new TerseFormatter(getLogTimestampFormat()));
123124
handler.setLevel(level);
124125
configureLogEncoding(logger, encoding, handler);
125126
}
@@ -160,4 +161,8 @@ private OutputStream getOutputStream() {
160161
})
161162
.orElse(System.out);
162163
}
164+
165+
public String getLogTimestampFormat() {
166+
return config.get(LOGGING_SECTION, "log-timestamp-format").orElse(DEFAULT_LOG_TIMESTAMP_FORMAT);
167+
}
163168
}

java/src/org/openqa/selenium/grid/log/TerseFormatter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ public class TerseFormatter extends Formatter {
6060
private final StringBuilder buffer;
6161
private final SimpleDateFormat timestampFormatter;
6262

63-
public TerseFormatter() {
63+
public TerseFormatter(String logTimestampFormat) {
6464
buffer = new StringBuilder();
6565
buffer.append(PREFIX);
66-
timestampFormatter = new SimpleDateFormat("HH:mm:ss.SSS");
66+
timestampFormatter = new SimpleDateFormat(logTimestampFormat);
6767
}
6868

6969
/**

java/src/org/openqa/selenium/remote/server/log/LoggingManager.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package org.openqa.selenium.remote.server.log;
1919

20+
import org.openqa.selenium.grid.log.LoggingOptions;
2021
import org.openqa.selenium.grid.log.TerseFormatter;
2122

2223
import java.util.logging.ConsoleHandler;
@@ -32,7 +33,7 @@
3233
public class LoggingManager {
3334

3435
private static PerSessionLogHandler perSessionLogHandler =
35-
new PerSessionLogHandler(4000, new TerseFormatter(), false);
36+
new PerSessionLogHandler(4000, new TerseFormatter(LoggingOptions.DEFAULT_LOG_TIMESTAMP_FORMAT), false);
3637

3738
public static synchronized void configureLogging(boolean debugMode) {
3839
final Logger currentLogger;
@@ -67,14 +68,14 @@ public static void overrideSimpleFormatterWithTerseOneForConsoleHandler(
6768
* DGF - Nobody likes the SimpleFormatter; surely they wanted our terse formatter instead.
6869
*/
6970
originalLevel = handler.getLevel();
70-
handler.setFormatter(new TerseFormatter());
71+
handler.setFormatter(new TerseFormatter(LoggingOptions.DEFAULT_LOG_TIMESTAMP_FORMAT));
7172
handler.setLevel(Level.WARNING);
7273

7374
/*
7475
* Furthermore, we all want DEBUG/INFO on stdout and WARN/ERROR on stderr
7576
*/
7677
stdOutHandler = new StdOutHandler();
77-
stdOutHandler.setFormatter(new TerseFormatter());
78+
stdOutHandler.setFormatter(new TerseFormatter(LoggingOptions.DEFAULT_LOG_TIMESTAMP_FORMAT));
7879
stdOutHandler.setFilter(new MaxLevelFilter(Level.INFO));
7980
stdOutHandler.setLevel(originalLevel);
8081
logger.addHandler(stdOutHandler);

0 commit comments

Comments
 (0)