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: use initialized logging option in constructor #843

Merged
merged 5 commits into from
Jan 18, 2022
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 @@ -241,7 +241,7 @@ public LoggingHandler(
setLevel(level);
baseLevel = level.equals(Level.ALL) ? Level.FINEST : level;
flushLevel = config.getFlushLevel();
Boolean f1 = options.getAutoPopulateMetadata();
Boolean f1 = loggingOptions.getAutoPopulateMetadata();
Boolean f2 = config.getAutoPopulateMetadata();
autoPopulateMetadata = isTrueOrNull(f1) && isTrueOrNull(f2);
redirectToStdout = firstNonNull(config.getRedirectToStdout(), Boolean.FALSE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static org.easymock.EasyMock.reset;
import static org.easymock.EasyMock.verify;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import com.google.api.client.util.Strings;
Expand Down Expand Up @@ -52,6 +53,7 @@ public class LoggingHandlerTest {
private static final String LOG_NAME = "java.log";
private static final String MESSAGE = "message";
private static final String PROJECT = "project";
private static final String PROJECT_ENV_NAME = "GOOGLE_CLOUD_PROJECT";

private static final MonitoredResource DEFAULT_RESOURCE =
MonitoredResource.of("global", ImmutableMap.of("project_id", PROJECT));
Expand Down Expand Up @@ -205,9 +207,9 @@ public void setUp() {
expect(options.getService()).andStubReturn(logging);
expect(options.getAutoPopulateMetadata()).andStubReturn(Boolean.FALSE);
logging.setFlushSeverity(EasyMock.anyObject(Severity.class));
expectLastCall().once();
expectLastCall().anyTimes();
logging.setWriteSynchronicity(EasyMock.anyObject(Synchronicity.class));
expectLastCall().once();
expectLastCall().anyTimes();
}

@After
Expand All @@ -221,6 +223,19 @@ private static LogRecord newLogRecord(Level level, String message) {
return record;
}

@Test
public void testDefaultHandlerCreation() {
String oldProject = System.getProperty(PROJECT_ENV_NAME);
System.setProperty(PROJECT_ENV_NAME, PROJECT);
replay(options, logging);
assertNotNull(new LoggingHandler());
if (oldProject != null) {
System.setProperty(PROJECT_ENV_NAME, oldProject);
} else {
System.clearProperty(PROJECT_ENV_NAME);
}
}

@Test
public void testPublishLevels() {
logging.write(ImmutableList.of(FINEST_ENTRY), DEFAULT_OPTIONS);
Expand Down Expand Up @@ -483,6 +498,7 @@ public void testFlushLevel() {

@Test
public void testSyncWrite() {
reset(logging);
LogEntry entry =
LogEntry.newBuilder(Payload.StringPayload.of(MESSAGE))
.setSeverity(Severity.DEBUG)
Expand All @@ -491,10 +507,10 @@ public void testSyncWrite() {
.setTimestamp(123456789L)
.build();

logging.setWriteSynchronicity(Synchronicity.ASYNC);
logging.setWriteSynchronicity(Synchronicity.SYNC);
expectLastCall().once();
logging.write(ImmutableList.of(entry), DEFAULT_OPTIONS);
expectLastCall().once();
logging.setFlushSeverity(Severity.ERROR);
replay(options, logging);

LoggingHandler handler = new LoggingHandler(LOG_NAME, options, DEFAULT_RESOURCE);
Expand Down