Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(java-core): capturing startup logs and writing them to the logger incase end of stream reached #9108

Merged
merged 6 commits into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,23 @@ private BlockingProcessStreamReader(
this.logger = logger;
this.emulatorTag = "[" + emulator + "]";
this.logLinePattern = Pattern.compile("(\\[" + emulator + "\\]\\s)?(\\w+):.*");
LogRecorder logRecorder = new LogRecorder(logger);
if (!Strings.isNullOrEmpty(blockUntil)) {
String line;
do {
line = errorReader.readLine();
if (line != null) {
logRecorder.record(line); // recording the logs as these might be the error logs.
}
} while (line != null && !line.contains(blockUntil));
}

/* If the stream is closed here, that means subprocess has been failed to start. In that case, we
should flush the recorded startup logs to help the user in debugging */
boolean streamClosed = errorReader.read() == -1;
if (streamClosed) {
logRecorder.flush();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.cloud.testing;

import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* This class records the logs in memory and flush them onto the {@link java.util.logging.Logger}
* when {@link #flush()} method is called.
*
* <p>This component is useful to hold logs for some time and flush them if needed. For example, we
jainsahab marked this conversation as resolved.
Show resolved Hide resolved
* would want to ignore the trivial startup logs of a sub-process if it is started successfully, but
* <b>in case any error occurs</b>, these logs should be propagated to the user for the debugging
* purpose.
*/
class LogRecorder {

private final Pattern logLinePattern = Pattern.compile("([A-Z]+):.*");
private final Logger logger;
private StringBuilder logs;

public LogRecorder(Logger logger) {
this.logger = logger;
this.logs = new StringBuilder();
}

/**
* Records a log line.
*
* @param logLine the log to record.
*/
public void record(String logLine) {
if (hasLevel(logLine)) {
String stripLevel = logLine.split(":")[1].trim();
this.logs.append(stripLevel);
} else {
this.logs.append(logLine);
}
this.logs.append(System.getProperty("line.separator"));
}

/** Flushes all the logs to the underlying {@link java.util.logging.Logger}. */
public void flush() {
if (this.logs.length() > 0) {
logger.log(Level.INFO, this.logs.toString());
this.logs = new StringBuilder();
}
}

private boolean hasLevel(String line) {
return getLevel(line) != null;
}

private Level getLevel(String line) {
try {
Matcher matcher = logLinePattern.matcher(line);
if (matcher.matches()) {
return Level.parse(matcher.group(1));
} else {
return null;
}
} catch (IllegalArgumentException e) {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ protected List<EmulatorRunner> getEmulatorRunners() {

@Override
protected Logger getLogger() {
return null;
return Logger.getLogger(TestEmulatorHelper.class.getName());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.google.cloud.testing;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;

import com.google.api.client.util.Charsets;
Expand Down Expand Up @@ -53,6 +54,8 @@ public class BlockingProcessStreamReaderTest {
+ "[emulator] log line 2\n"
+ "[emulator] Nov 08, 2016 2:05:44 PM io.netty.buffer.PooledByteBufAllocator <clinit>\n"
+ "[emulator] FINE: log line 3\n";
private static final String LOG_LINES_WITHOUT_BLOCK_UNTIL_TEXT =
"INFO: log line 1\n" + "log line 2\n" + "FINE: log line 3\n";

@Rule public Timeout globalTimeout = Timeout.seconds(10);

Expand Down Expand Up @@ -96,4 +99,21 @@ public void testForwardAlreadyTaggedLogs() throws IOException, InterruptedExcept
assertEquals("[emulator] log line 3", logger.getLogs().get(Level.FINE).iterator().next());
stream.close();
}

@Test
public void testStartUpLogs() throws IOException, InterruptedException {
TestLogger logger = new TestLogger();
InputStream stream =
new ByteArrayInputStream(LOG_LINES_WITHOUT_BLOCK_UNTIL_TEXT.getBytes(Charsets.UTF_8));
BlockingProcessStreamReader.start("emulator", stream, BLOCK_UNTIL, logger).join();
assertThat(logger.logs.get(Level.INFO).iterator().next())
.isEqualTo(
"log line 1"
+ System.lineSeparator()
+ "log line 2"
+ System.lineSeparator()
+ "log line 3"
+ System.lineSeparator());
stream.close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.cloud.testing;

import static com.google.common.truth.Truth.assertThat;

import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Multimap;
import com.google.common.truth.Truth;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.junit.Test;

public class LogRecorderTest {

private static final String LOG_LINES =
"INFO: log line 1\n" + "log line 2\n" + "FINE: log line 3\n";
private final TestLogger testLogger = new TestLogger();
private final LogRecorder logRecorder = new LogRecorder(testLogger);

@Test
public void shouldAggregateLogs() {
for (String logLine : LOG_LINES.split("\n")) {
logRecorder.record(logLine);
}
logRecorder.flush();

assertThat(testLogger.logs.get(Level.INFO).iterator().next())
.isEqualTo(
"log line 1"
+ System.lineSeparator()
+ "log line 2"
+ System.lineSeparator()
+ "log line 3"
+ System.lineSeparator());
}

@Test
public void shouldBeEmptyAfterFlushCall() {
for (String logLine : LOG_LINES.split("\n")) {
logRecorder.record(logLine);
}
// first flush and assertion
logRecorder.flush();
assertThat(testLogger.logs.get(Level.INFO).iterator().next())
.isEqualTo(
"log line 1"
+ System.lineSeparator()
+ "log line 2"
+ System.lineSeparator()
+ "log line 3"
+ System.lineSeparator());

// second flush and assertion
testLogger.reset();
logRecorder.flush();
Truth.assertThat(testLogger.logs.get(Level.INFO)).isEmpty();
}

private static final class TestLogger extends Logger {

private Multimap<Level, String> logs = LinkedHashMultimap.create();

private TestLogger() {
super("text-logger", null);
}

public void log(Level level, String msg) {
logs.put(level, msg);
}

public void reset() {
this.logs = LinkedHashMultimap.create();
}

Multimap<Level, String> getLogs() {
return logs;
}
}
}