Skip to content

Commit

Permalink
fix: use initialized logging option in constructor (#843)
Browse files Browse the repository at this point in the history
Add unit test to validate LoggingHandler construction with default args.

Fixes #842

* 🦉 Updates from OwlBot

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
minherz and gcf-owl-bot[bot] committed Jan 18, 2022
1 parent 66f80b2 commit 99fb678
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
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
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

0 comments on commit 99fb678

Please sign in to comment.