Skip to content

Commit

Permalink
Merge pull request #15054 from phillip-kruger/logstream-jackson-fix
Browse files Browse the repository at this point in the history
Fix Jackson Not available at runtime for Dev UI Logstream
  • Loading branch information
gsmet authored Feb 15, 2021
2 parents e99ea11 + 3f5dc89 commit 516db26
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ private static void createFile(InputStream source, Path targetFile) throws IOExc
}
}

private static Path createResourcesDirectory(AppArtifact userApplication, AppArtifact resourcesArtifact) {
public static Path createResourcesDirectory(AppArtifact userApplication, AppArtifact resourcesArtifact) {
try {
Path path = Paths.get(TMP_DIR, "quarkus", userApplication.getGroupId(), userApplication.getArtifactId(),
resourcesArtifact.getGroupId(), resourcesArtifact.getArtifactId(), resourcesArtifact.getVersion());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,13 @@ public JsonArrayBuilder add(Long value) {
return this;
}

public JsonArrayBuilder addAll(List<JsonObjectBuilder> value) {
if (value != null && !value.isEmpty()) {
values.addAll(value);
}
return this;
}

private void addInternal(Object value) {
if (value != null) {
values.add(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
import org.jboss.logmanager.ExtFormatter;
import org.jboss.logmanager.ExtLogRecord;

import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.quarkus.vertx.http.runtime.devmode.Json;

/**
* Formatting log records into a json format
Expand All @@ -19,13 +18,14 @@ public class JsonFormatter extends ExtFormatter {

@Override
public String format(final ExtLogRecord logRecord) {
return toJsonObject(logRecord).toString();
return toJsonObject(logRecord).build();
}

private JsonObject toJsonObject(ExtLogRecord logRecord) {
private Json.JsonObjectBuilder toJsonObject(ExtLogRecord logRecord) {
String formattedMessage = formatMessage(logRecord);

JsonObject jsonObject = new JsonObject();
Json.JsonObjectBuilder jsonObject = Json.object();

jsonObject.put(TYPE, LOG_LINE);
if (logRecord.getLoggerName() != null) {
jsonObject.put(LOGGER_NAME_SHORT, getShortFullClassName(logRecord.getLoggerName(), ""));
Expand Down Expand Up @@ -71,11 +71,11 @@ private JsonObject toJsonObject(ExtLogRecord logRecord) {
return jsonObject;
}

private JsonArray getStacktraces(Throwable t) {
private Json.JsonArrayBuilder getStacktraces(Throwable t) {
List<String> traces = new LinkedList<>();
addStacktrace(traces, t);

JsonArray jsonArray = new JsonArray();
Json.JsonArrayBuilder jsonArray = Json.array();

traces.forEach((trace) -> {
jsonArray.add(trace);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,65 +1,72 @@
package io.quarkus.vertx.http.runtime.logstream;

import static java.util.logging.Level.ALL;
import static java.util.logging.Level.CONFIG;
import static java.util.logging.Level.FINE;
import static java.util.logging.Level.FINER;
import static java.util.logging.Level.FINEST;
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.OFF;
import static java.util.logging.Level.SEVERE;
import static java.util.logging.Level.WARNING;
import static org.jboss.logmanager.Level.ALL;
import static org.jboss.logmanager.Level.CONFIG;
import static org.jboss.logmanager.Level.DEBUG;
import static org.jboss.logmanager.Level.ERROR;
import static org.jboss.logmanager.Level.FATAL;
import static org.jboss.logmanager.Level.FINE;
import static org.jboss.logmanager.Level.FINER;
import static org.jboss.logmanager.Level.FINEST;
import static org.jboss.logmanager.Level.INFO;
import static org.jboss.logmanager.Level.OFF;
import static org.jboss.logmanager.Level.SEVERE;
import static org.jboss.logmanager.Level.TRACE;
import static org.jboss.logmanager.Level.WARN;
import static org.jboss.logmanager.Level.WARNING;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;

import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import org.jboss.logmanager.Level;
import org.jboss.logmanager.LogContext;
import org.jboss.logmanager.Logger;

import io.quarkus.vertx.http.runtime.devmode.Json;

/**
* Allow controlling to the log levels
*/
public class LogController {
private static final org.jboss.logging.Logger LOG = org.jboss.logging.Logger.getLogger(LogController.class);

private LogController() {
}

public JsonArray getLevels() {
return new JsonArray()
.add(OFF.getName())
.add(SEVERE.getName())
.add(WARNING.getName())
.add(INFO.getName())
.add(CONFIG.getName())
.add(FINE.getName())
.add(FINER.getName())
.add(FINEST.getName())
.add(ALL.getName());
public static Json.JsonArrayBuilder getLevels() {
Json.JsonArrayBuilder array = Json.array();
for (String level : LEVELS) {
array.add(level);
}
return array;
}

public JsonArray getLoggers() {
TreeMap<String, JsonObject> loggerMap = new TreeMap<>();
LogManager manager = LogManager.getLogManager();
Enumeration<String> loggerNames = manager.getLoggerNames();
public static Json.JsonArrayBuilder getLoggers() {
LogContext logContext = LogContext.getLogContext();
TreeMap<String, Json.JsonObjectBuilder> loggerMap = new TreeMap<>();

Enumeration<String> loggerNames = logContext.getLoggerNames();
while (loggerNames.hasMoreElements()) {
String loggerName = loggerNames.nextElement();
JsonObject jsonObject = getLogger(loggerName);
Json.JsonObjectBuilder jsonObject = getLogger(loggerName);
if (jsonObject != null) {
loggerMap.put(loggerName, jsonObject);
}
}

List<JsonObject> orderedLoggers = new ArrayList<>(loggerMap.values());
JsonArray jsonArray = new JsonArray(orderedLoggers);
List<Json.JsonObjectBuilder> orderedLoggers = new ArrayList<>(loggerMap.values());
Json.JsonArrayBuilder jsonArray = Json.array();
jsonArray.addAll(orderedLoggers);
return jsonArray;
}

public JsonObject getLogger(String loggerName) {
public static Json.JsonObjectBuilder getLogger(String loggerName) {
LogContext logContext = LogContext.getLogContext();
if (loggerName != null && !loggerName.isEmpty()) {
Logger logger = Logger.getLogger(loggerName);
JsonObject jsonObject = new JsonObject();
Logger logger = logContext.getLogger(loggerName);
Json.JsonObjectBuilder jsonObject = Json.object();
jsonObject.put("name", loggerName);
jsonObject.put("effectiveLevel", getEffectiveLogLevel(logger));
jsonObject.put("configuredLevel", getConfiguredLogLevel(logger));
Expand All @@ -68,20 +75,22 @@ public JsonObject getLogger(String loggerName) {
return null;
}

public void updateLogLevel(String loggerName, String levelVal) {
Logger logger = Logger.getLogger(loggerName);
public static void updateLogLevel(String loggerName, String levelValue) {
LogContext logContext = LogContext.getLogContext();
Logger logger = logContext.getLogger(loggerName);
if (logger != null) {
Level level = Level.parse(levelVal);
java.util.logging.Level level = Level.parse(levelValue);
logger.setLevel(level);
LOG.info("Log level updated [" + loggerName + "] changed to [" + levelValue + "]");
}
}

private String getConfiguredLogLevel(Logger logger) {
Level level = logger.getLevel();
private static String getConfiguredLogLevel(Logger logger) {
java.util.logging.Level level = logger.getLevel();
return level != null ? level.getName() : null;
}

private String getEffectiveLogLevel(Logger logger) {
private static String getEffectiveLogLevel(Logger logger) {
if (logger == null) {
return null;
}
Expand All @@ -90,4 +99,23 @@ private String getEffectiveLogLevel(Logger logger) {
}
return getEffectiveLogLevel(logger.getParent());
}

public static final List<String> LEVELS = new ArrayList<>();

static {
LEVELS.add(OFF.getName());
LEVELS.add(SEVERE.getName());
LEVELS.add(ERROR.getName());
LEVELS.add(FATAL.getName());
LEVELS.add(WARNING.getName());
LEVELS.add(WARN.getName());
LEVELS.add(INFO.getName());
LEVELS.add(DEBUG.getName());
LEVELS.add(TRACE.getName());
LEVELS.add(CONFIG.getName());
LEVELS.add(FINE.getName());
LEVELS.add(FINER.getName());
LEVELS.add(FINEST.getName());
LEVELS.add(ALL.getName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
import org.jboss.logmanager.LogContext;

import io.netty.handler.codec.http.HttpHeaderNames;
import io.quarkus.vertx.http.runtime.devmode.Json;
import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;
import io.vertx.core.http.ServerWebSocket;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.RoutingContext;

/**
Expand All @@ -22,24 +22,19 @@ public class LogStreamWebSocket implements Handler<RoutingContext> {
private static final Logger log = Logger.getLogger(LogStreamWebSocket.class.getName());
private final HistoryHandler historyHandler = new HistoryHandler();

private final JsonObject initMessage = new JsonObject();
private final String initMessage;

private final ExtHandler rootHandler;
private final org.jboss.logmanager.Logger rootLogger;

private final LogController logController = new LogController();

public LogStreamWebSocket() {

// Add history handler
final LogContext logContext = LogContext.getLogContext();
rootLogger = logContext.getLogger("");
rootHandler = findCorrectHandler(rootLogger.getHandlers());
addHandler(historyHandler);

initMessage.put(TYPE, INIT);
initMessage.put("loggers", logController.getLoggers());
initMessage.put("levels", logController.getLevels());
initMessage = createInitMessage();
}

@Override
Expand Down Expand Up @@ -122,7 +117,7 @@ private void update(String message) {
if (p.length == 3) {
String loggerName = p[1];
String levelVal = p[2];
logController.updateLogLevel(loggerName, levelVal);
LogController.updateLogLevel(loggerName, levelVal);
}
}

Expand Down Expand Up @@ -173,6 +168,14 @@ private ExtHandler getConsoleHandler(java.util.logging.Handler[] handlers) {
return null;
}

private String createInitMessage() {
Json.JsonObjectBuilder initMessage = Json.object();
initMessage.put(TYPE, INIT);
initMessage.put("loggers", LogController.getLoggers());
initMessage.put("levels", LogController.getLevels());
return initMessage.build();
}

private static final String TYPE = "type";
private static final String INIT = "init";
private static final String START = "start";
Expand Down

0 comments on commit 516db26

Please sign in to comment.