-
-
Notifications
You must be signed in to change notification settings - Fork 8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow logs to be output in single-line json format to stdout
- Loading branch information
Showing
20 changed files
with
225 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
java_library( | ||
name = "log", | ||
srcs = glob(["*.java"]), | ||
deps = [ | ||
"//java/client/src/org/openqa/selenium/remote:remote", | ||
"//java/client/src/org/openqa/selenium/remote/tracing:tracing", | ||
"//java/server/src/org/openqa/selenium/grid/config:config", | ||
], | ||
visibility = [ | ||
"//java/server/src/org/openqa/...", | ||
"//java/server/test/org/openqa/...", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
java_library( | ||
name = "log", | ||
srcs = glob(["*.java"]), | ||
deps = [ | ||
"//java/client/src/org/openqa/selenium/remote", | ||
"//java/client/src/org/openqa/selenium/remote/tracing", | ||
"//java/server/src/org/openqa/selenium/grid/config", | ||
], | ||
visibility = [ | ||
"//java/server/src/org/openqa:__subpackages__", | ||
"//java/server/test/org/openqa:__subpackages__", | ||
], | ||
) |
37 changes: 37 additions & 0 deletions
37
java/server/src/org/openqa/selenium/grid/log/FlushingHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package org.openqa.selenium.grid.log; | ||
|
||
import java.io.OutputStream; | ||
import java.util.Objects; | ||
import java.util.logging.LogRecord; | ||
import java.util.logging.StreamHandler; | ||
|
||
class FlushingHandler extends StreamHandler { | ||
|
||
private OutputStream out; | ||
|
||
FlushingHandler(OutputStream out) { | ||
setOutputStream(out); | ||
} | ||
|
||
@Override | ||
protected synchronized void setOutputStream(OutputStream out) throws SecurityException { | ||
super.setOutputStream(out); | ||
this.out = out; | ||
} | ||
|
||
@Override | ||
public synchronized void publish(LogRecord record) { | ||
super.publish(record); | ||
flush(); | ||
} | ||
|
||
@Override | ||
public synchronized void close() throws SecurityException { | ||
// Avoid closing sysout or syserr | ||
if (Objects.equals(System.out, out) || Objects.equals(System.err, out)) { | ||
flush(); | ||
} else { | ||
super.close(); | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
java/server/src/org/openqa/selenium/grid/log/JsonFormatter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package org.openqa.selenium.grid.log; | ||
|
||
import static java.time.ZoneOffset.UTC; | ||
import static java.time.format.DateTimeFormatter.ISO_OFFSET_DATE_TIME; | ||
|
||
import org.openqa.selenium.json.Json; | ||
import org.openqa.selenium.json.JsonOutput; | ||
|
||
import java.time.Instant; | ||
import java.time.ZoneId; | ||
import java.time.ZonedDateTime; | ||
import java.util.Map; | ||
import java.util.TreeMap; | ||
import java.util.logging.Formatter; | ||
import java.util.logging.LogRecord; | ||
|
||
class JsonFormatter extends Formatter { | ||
|
||
public static final Json JSON = new Json(); | ||
|
||
@Override | ||
public String format(LogRecord record) { | ||
Map<String, Object> logRecord = new TreeMap<>(); | ||
|
||
Instant instant = Instant.ofEpochMilli(record.getMillis()); | ||
ZonedDateTime local = ZonedDateTime.ofInstant(instant, ZoneId.systemDefault()); | ||
|
||
logRecord.put("log-time-local", ISO_OFFSET_DATE_TIME.format(local)); | ||
logRecord.put("log-time-utc", ISO_OFFSET_DATE_TIME.format(local.withZoneSameInstant(UTC))); | ||
|
||
String[] split = record.getSourceClassName().split("\\."); | ||
logRecord.put("class", split[split.length - 1]); | ||
logRecord.put("method", record.getSourceMethodName()); | ||
logRecord.put("log-name", record.getLoggerName()); | ||
logRecord.put("log-level", record.getLevel()); | ||
logRecord.put("log-message", record.getMessage()); | ||
|
||
StringBuilder text = new StringBuilder(); | ||
try (JsonOutput json = JSON.newOutput(text).setPrettyPrint(false)) { | ||
json.write(logRecord); | ||
text.append('\n'); | ||
} | ||
return text.toString(); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
java/server/src/org/openqa/selenium/grid/log/LoggingOptions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package org.openqa.selenium.grid.log; | ||
|
||
import org.openqa.selenium.grid.config.Config; | ||
import org.openqa.selenium.remote.tracing.DistributedTracer; | ||
|
||
import java.util.Arrays; | ||
import java.util.Enumeration; | ||
import java.util.Objects; | ||
import java.util.logging.Handler; | ||
import java.util.logging.LogManager; | ||
import java.util.logging.Logger; | ||
|
||
public class LoggingOptions { | ||
|
||
private final Config config; | ||
|
||
public LoggingOptions(Config config) { | ||
this.config = Objects.requireNonNull(config); | ||
} | ||
|
||
public boolean isUsingStructuredLogging() { | ||
return config.getBool("logging", "structured-logs").orElse(false); | ||
} | ||
|
||
public boolean isUsingPlainLogs() { | ||
return config.getBool("logging", "plain-logs").orElse(true); | ||
} | ||
|
||
public DistributedTracer getTracer() { | ||
return DistributedTracer.builder().detect().build(); | ||
} | ||
|
||
public void configureLogging() { | ||
if (!config.getBool("logging", "enable").orElse(true)) { | ||
return; | ||
} | ||
|
||
// Remove all handlers from existing loggers | ||
LogManager logManager = LogManager.getLogManager(); | ||
Enumeration<String> names = logManager.getLoggerNames(); | ||
while (names.hasMoreElements()) { | ||
Logger logger = logManager.getLogger(names.nextElement()); | ||
Arrays.stream(logger.getHandlers()).forEach(logger::removeHandler); | ||
} | ||
|
||
// Now configure the root logger, since everything should flow up to that | ||
Logger logger = logManager.getLogger(""); | ||
|
||
if (isUsingPlainLogs()) { | ||
Handler handler = new FlushingHandler(System.out); | ||
handler.setFormatter(new TerseFormatter()); | ||
logger.addHandler(handler); | ||
} | ||
|
||
if (isUsingStructuredLogging()) { | ||
Handler handler = new FlushingHandler(System.out); | ||
handler.setFormatter(new JsonFormatter()); | ||
logger.addHandler(handler); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.