From 3e0ca57ac6de4ad26a77765da86593e483db072b Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Thu, 30 Sep 2021 13:47:45 -0500 Subject: [PATCH 1/2] chore: simplify initialization of LoggingHandler.logger getLogging() was called in the constructor of LoggingHandler. So, there's no reason to have lazy initialization for the logger. --- .../google/cloud/logging/LoggingHandler.java | 40 ++++++------------- .../cloud/logging/LoggingHandlerTest.java | 14 +------ 2 files changed, 14 insertions(+), 40 deletions(-) diff --git a/google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingHandler.java b/google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingHandler.java index 2607becde..de9b7376c 100644 --- a/google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingHandler.java +++ b/google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingHandler.java @@ -103,7 +103,7 @@ public class LoggingHandler extends Handler { private final List enhancers; private final LoggingOptions loggingOptions; - private volatile Logging logging; + private final Logging logging; // Logs with the same severity with the base could be more efficiently sent to Cloud. // Defaults to level of the handler or Level.FINEST if the handler is set to Level.ALL. @@ -168,6 +168,7 @@ public LoggingHandler( List enhancers) { try { loggingOptions = options != null ? options : LoggingOptions.getDefaultInstance(); + logging = loggingOptions.getService(); LoggingConfig config = new LoggingConfig(getClass().getName()); setFilter(config.getFilter()); setFormatter(config.getFormatter()); @@ -192,8 +193,8 @@ public LoggingHandler( String.valueOf(baseLevel.intValue()))) }; - getLogging().setFlushSeverity(severityFor(flushLevel)); - getLogging().setWriteSynchronicity(config.getSynchronicity()); + logging.setFlushSeverity(severityFor(flushLevel)); + logging.setWriteSynchronicity(config.getSynchronicity()); this.enhancers = new LinkedList<>(); @@ -234,7 +235,7 @@ public void publish(LogRecord record) { } if (logEntry != null) { try { - getLogging().write(ImmutableList.of(logEntry), defaultWriteOptions); + logging.write(ImmutableList.of(logEntry), defaultWriteOptions); } catch (Exception ex) { getErrorManager().error(null, ex, ErrorManager.WRITE_FAILURE); } @@ -264,7 +265,7 @@ private LogEntry logEntryFor(LogRecord record) throws Exception { @Override public void flush() { try { - getLogging().flush(); + logging.flush(); } catch (Exception ex) { getErrorManager().error(null, ex, ErrorManager.FLUSH_FAILURE); } @@ -273,14 +274,11 @@ public void flush() { /** Closes the handler and the associated {@link Logging} object. */ @Override public synchronized void close() throws SecurityException { - if (logging != null) { - try { - logging.close(); - } catch (Exception ex) { - // ignore - } + try { + logging.close(); + } catch (Exception ex) { + // ignore } - logging = null; } /** Get the flush log level. */ @@ -295,7 +293,7 @@ public Level getFlushLevel() { */ public void setFlushLevel(Level flushLevel) { this.flushLevel = flushLevel; - getLogging().setFlushSeverity(severityFor(flushLevel)); + logging.setFlushSeverity(severityFor(flushLevel)); } /** @@ -304,12 +302,12 @@ public void setFlushLevel(Level flushLevel) { * @param synchronicity {@link Synchronicity} */ public void setSynchronicity(Synchronicity synchronicity) { - getLogging().setWriteSynchronicity(synchronicity); + logging.setWriteSynchronicity(synchronicity); } /** Get the flush log level. */ public Synchronicity getSynchronicity() { - return getLogging().getWriteSynchronicity(); + return logging.getWriteSynchronicity(); } /** @@ -352,16 +350,4 @@ private static Severity severityFor(Level level) { return Severity.DEFAULT; } } - - /** Returns an instance of the logging service. */ - private Logging getLogging() { - if (logging == null) { - synchronized (this) { - if (logging == null) { - logging = loggingOptions.getService(); - } - } - } - return logging; - } } diff --git a/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingHandlerTest.java b/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingHandlerTest.java index 5d5285cb8..4ad6c300c 100644 --- a/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingHandlerTest.java +++ b/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingHandlerTest.java @@ -220,6 +220,7 @@ public void enhanceLogEntry(LogEntry.Builder builder) { public void setUp() { logging = EasyMock.createMock(Logging.class); options = EasyMock.createStrictMock(LoggingOptions.class); + expect(options.getService()).andReturn(logging); } @After @@ -236,7 +237,6 @@ private static LogRecord newLogRecord(Level level, String message) { @Test public void testPublishLevels() { expect(options.getProjectId()).andReturn(PROJECT).anyTimes(); - expect(options.getService()).andReturn(logging); logging.setFlushSeverity(Severity.ERROR); expectLastCall().once(); logging.setWriteSynchronicity(Synchronicity.ASYNC); @@ -291,7 +291,6 @@ public void testPublishLevels() { @Test public void testPublishCustomResource() { expect(options.getProjectId()).andReturn(PROJECT).anyTimes(); - expect(options.getService()).andReturn(logging); logging.setFlushSeverity(Severity.ERROR); expectLastCall().once(); logging.setWriteSynchronicity(Synchronicity.ASYNC); @@ -313,7 +312,6 @@ public void testPublishCustomResource() { @Test public void testPublishKubernetesContainerResource() { expect(options.getProjectId()).andReturn(PROJECT).anyTimes(); - expect(options.getService()).andReturn(logging); logging.setFlushSeverity(Severity.ERROR); expectLastCall().once(); logging.setWriteSynchronicity(Synchronicity.ASYNC); @@ -348,7 +346,6 @@ public void testPublishKubernetesContainerResource() { @Test public void testEnhancedLogEntry() { expect(options.getProjectId()).andReturn(PROJECT).anyTimes(); - expect(options.getService()).andReturn(logging); MonitoredResource resource = MonitoredResource.of("custom", ImmutableMap.of()); logging.setFlushSeverity(Severity.ERROR); expectLastCall().once(); @@ -378,7 +375,6 @@ public void enhanceLogEntry(Builder builder) { @Test public void testTraceEnhancedLogEntry() { expect(options.getProjectId()).andReturn(PROJECT).anyTimes(); - expect(options.getService()).andReturn(logging); MonitoredResource resource = MonitoredResource.of("custom", ImmutableMap.of()); logging.setFlushSeverity(Severity.ERROR); expectLastCall().once(); @@ -403,7 +399,6 @@ public void testTraceEnhancedLogEntry() { @Test public void testReportWriteError() { expect(options.getProjectId()).andReturn(PROJECT).anyTimes(); - expect(options.getService()).andReturn(logging); RuntimeException ex = new RuntimeException(); logging.setFlushSeverity(Severity.ERROR); expectLastCall().once(); @@ -427,7 +422,6 @@ public void testReportWriteError() { @Test public void testReportFlushError() { expect(options.getProjectId()).andReturn(PROJECT).anyTimes(); - expect(options.getService()).andReturn(logging); RuntimeException ex = new RuntimeException(); logging.setFlushSeverity(Severity.ERROR); expectLastCall().once(); @@ -454,7 +448,6 @@ public void testReportFlushError() { @Test public void testReportFormatError() { expect(options.getProjectId()).andReturn(PROJECT).anyTimes(); - expect(options.getService()).andReturn(logging); logging.setFlushSeverity(Severity.ERROR); expectLastCall().once(); logging.setWriteSynchronicity(Synchronicity.ASYNC); @@ -480,7 +473,6 @@ public void testReportFormatError() { // @Test public void testFlushLevel() { expect(options.getProjectId()).andReturn(PROJECT).anyTimes(); - expect(options.getService()).andReturn(logging); logging.setFlushSeverity(Severity.ERROR); expectLastCall().once(); logging.setWriteSynchronicity(Synchronicity.ASYNC); @@ -508,7 +500,6 @@ public void testFlushLevel() { @Test public void testSyncWrite() { expect(options.getProjectId()).andReturn(PROJECT).anyTimes(); - expect(options.getService()).andReturn(logging); LogEntry entry = LogEntry.newBuilder(Payload.StringPayload.of(MESSAGE)) .setSeverity(Severity.DEBUG) @@ -539,7 +530,6 @@ public void testSyncWrite() { @Test public void testAddHandler() { expect(options.getProjectId()).andReturn(PROJECT).anyTimes(); - expect(options.getService()).andReturn(logging); logging.setFlushSeverity(Severity.ERROR); expectLastCall().once(); logging.setWriteSynchronicity(Synchronicity.ASYNC); @@ -565,7 +555,6 @@ public void close() { @Test public void testClose() throws Exception { expect(options.getProjectId()).andReturn(PROJECT).anyTimes(); - expect(options.getService()).andReturn(logging); logging.setFlushSeverity(Severity.ERROR); expectLastCall().once(); logging.setWriteSynchronicity(Synchronicity.ASYNC); @@ -580,6 +569,5 @@ public void testClose() throws Exception { handler.setFormatter(new TestFormatter()); handler.publish(newLogRecord(Level.FINEST, MESSAGE)); handler.close(); - handler.close(); } } From fa4c935aeda15b906382dcd74f8bae86280743a0 Mon Sep 17 00:00:00 2001 From: Owl Bot Date: Mon, 1 Nov 2021 14:30:03 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=A6=89=20Updates=20from=20OwlBot?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c79271d87..022ae95a1 100644 --- a/README.md +++ b/README.md @@ -51,20 +51,20 @@ If you are using Maven without BOM, add this to your dependencies: If you are using Gradle 5.x or later, add this to your dependencies ```Groovy -implementation platform('com.google.cloud:libraries-bom:23.1.0') +implementation platform('com.google.cloud:libraries-bom:24.0.0') implementation 'com.google.cloud:google-cloud-logging' ``` If you are using Gradle without BOM, add this to your dependencies ```Groovy -implementation 'com.google.cloud:google-cloud-logging:3.2.0' +implementation 'com.google.cloud:google-cloud-logging:3.3.0' ``` If you are using SBT, add this to your dependencies ```Scala -libraryDependencies += "com.google.cloud" % "google-cloud-logging" % "3.2.0" +libraryDependencies += "com.google.cloud" % "google-cloud-logging" % "3.3.0" ``` ## Authentication