Skip to content

Commit

Permalink
fix: Log dev server output using standard logger (#12321)
Browse files Browse the repository at this point in the history
Rename DevServerOutputFinder to the more descriptive DevServerOutputTracker

Fixes #12281
  • Loading branch information
Artur- committed Nov 10, 2021
1 parent e791eca commit 36307ca
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 18 deletions.
Expand Up @@ -68,7 +68,7 @@ protected Stream<String> getExcludedPatterns() {
".*\\.demo\\..*", "com\\.vaadin\\..*Util(s)?(\\$\\w+)?$",
"com\\.vaadin\\.flow\\.osgi\\.support\\..*",
"com\\.vaadin\\.flow\\.server\\.osgi\\..*",
"com\\.vaadin\\.base\\.devserver\\.DevServerOutputFinder.*",
"com\\.vaadin\\.base\\.devserver\\.DevServerOutputTracker.*",
"com\\.vaadin\\.flow\\.internal\\.VaadinContextInitializer",
"com\\.vaadin\\.flow\\.internal\\.ApplicationClassLoaderAccess",
"com\\.vaadin\\.base\\.devserver\\.BrowserLiveReloadAccessorImpl",
Expand Down
Expand Up @@ -37,7 +37,7 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.vaadin.base.devserver.DevServerOutputFinder.Result;
import com.vaadin.base.devserver.DevServerOutputTracker.Result;
import com.vaadin.flow.di.Lookup;
import com.vaadin.flow.internal.BrowserLiveReload;
import com.vaadin.flow.internal.BrowserLiveReloadAccessor;
Expand Down Expand Up @@ -362,16 +362,16 @@ protected Process doStartDevServer() {
*/
Runtime.getRuntime().addShutdownHook(new Thread(this::stop));

DevServerOutputFinder finder = new DevServerOutputFinder(
DevServerOutputTracker outputTracker = new DevServerOutputTracker(
process.getInputStream(), getServerSuccessPattern(),
getServerFailurePattern(), this::onDevServerCompilation);
finder.find();
outputTracker.find();
getLogger().info(LOG_START, getServerName());

int timeout = Integer.parseInt(config.getStringProperty(
InitParameters.SERVLET_PARAMETER_DEVMODE_WEBPACK_TIMEOUT,
DEFAULT_TIMEOUT_FOR_PATTERN));
finder.awaitFirstMatch(timeout);
outputTracker.awaitFirstMatch(timeout);

return process;
} catch (IOException e) {
Expand Down
Expand Up @@ -24,8 +24,6 @@
import java.util.function.Consumer;
import java.util.regex.Pattern;

import com.vaadin.flow.server.frontend.FrontendUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -35,7 +33,7 @@
* <p>
* Triggers an event whenever a success or failure pattern is found on a row.
*/
public class DevServerOutputFinder {
public class DevServerOutputTracker {

private static class Finder implements Runnable {

Expand Down Expand Up @@ -74,7 +72,6 @@ private void readLinesLoop(InputStreamReader reader)
StringBuilder line = new StringBuilder();
for (int i; (i = reader.read()) >= 0;) {
char ch = (char) i;
FrontendUtils.console("%c", ch);
line.append(ch);
if (ch == '\n') {
processLine(line.toString());
Expand All @@ -88,11 +85,20 @@ private void processLine(String line) {
if (line.contains("\b")) {
return;
}

// remove color escape codes for console
// remove color escape codes
String cleanLine = line.replaceAll("(\u001b\\[[;\\d]*m|[\b\r]+)",
"");

// Remove newline and timestamp as logger will add it
String logLine = cleanLine;
logLine = logLine.replaceAll("\n$", "");

// Vite preprends a timestamp
logLine = logLine.replaceAll("[0-9]+:[0-9]+:[0-9]+ [AP]M ", "");
// Webpack often prepends with <i>
logLine = logLine.replaceAll("^<i> ", "");
getLogger().info(logLine);

boolean succeed = success != null && success.matcher(line).find();
boolean failed = failure != null && failure.matcher(line).find();

Expand All @@ -110,10 +116,6 @@ private void processLine(String line) {
}
}

private static Logger getLogger() {
return LoggerFactory.getLogger(Finder.class);
}

}

/**
Expand Down Expand Up @@ -158,7 +160,7 @@ public String getOutput() {
* @param onMatch
* callback triggered when either success or failure is found
*/
public DevServerOutputFinder(InputStream inputStream, Pattern success,
public DevServerOutputTracker(InputStream inputStream, Pattern success,
Pattern failure, Consumer<Result> onMatch) {
monitor = new CountDownLatch(1);
finder = new Finder(inputStream, success, failure, result -> {
Expand All @@ -176,7 +178,7 @@ public DevServerOutputFinder(InputStream inputStream, Pattern success,
public void find() {
Thread finderThread = new Thread(finder);
finderThread.setDaemon(true);
finderThread.setName("dev-server-output-reader");
finderThread.setName("dev-server-output");
finderThread.start();
}

Expand All @@ -195,4 +197,8 @@ public boolean awaitFirstMatch(int timeoutInSeconds)
return monitor.await(timeoutInSeconds, TimeUnit.SECONDS);
}

private static Logger getLogger() {
return LoggerFactory.getLogger(DevServerOutputTracker.class);
}

}
Expand Up @@ -22,7 +22,7 @@
import java.util.concurrent.CompletableFuture;
import java.util.regex.Pattern;

import com.vaadin.base.devserver.DevServerOutputFinder.Result;
import com.vaadin.base.devserver.DevServerOutputTracker.Result;
import com.vaadin.flow.di.Lookup;
import com.vaadin.flow.server.InitParameters;
import com.vaadin.flow.server.frontend.FrontendUtils;
Expand Down

0 comments on commit 36307ca

Please sign in to comment.