From 646ee053071672313f73891cd548a6ca223d6214 Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Fri, 13 Jan 2017 22:27:11 +1100 Subject: [PATCH 01/11] Initialize the default MonitoredResource from a GAE environment --- .../google/cloud/logging/LoggingHandler.java | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) 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 3bdc301c624a..4789faace678 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 @@ -95,6 +95,7 @@ public class LoggingHandler extends Handler { private final LoggingOptions options; private final WriteOption[] writeOptions; + private final String gaeInstanceId; private List buffer = new LinkedList<>(); private volatile Logging logging; private Level flushLevel; @@ -131,7 +132,9 @@ public LoggingHandler(String log, LoggingOptions options) { * * @param log the name of the log to which log entries are written * @param options options for the Stackdriver Logging service - * @param monitoredResource the monitored resource to which log entries refer + * @param monitoredResource the monitored resource to which log entries refer. If null a default + * resource is created based on the project ID. If a Google App Engine environment is detected + * then a more comprehensive default resource may be created. */ public LoggingHandler(String log, LoggingOptions options, MonitoredResource monitoredResource) { LogConfigHelper helper = new LogConfigHelper(); @@ -143,6 +146,7 @@ public LoggingHandler(String log, LoggingOptions options, MonitoredResource moni setFilter(helper.getFilterProperty(className + ".filter", null)); setFormatter(helper.getFormatterProperty(className + ".formatter", new SimpleFormatter())); String logName = firstNonNull(log, helper.getProperty(className + ".log", "java.log")); + gaeInstanceId = System.getenv("GAE_INSTANCE"); MonitoredResource resource = firstNonNull(monitoredResource, getDefaultResource()); writeOptions = new WriteOption[]{WriteOption.logName(logName), WriteOption.resource(resource)}; } @@ -179,6 +183,19 @@ private static boolean hasLoggingHandler(Logger logger) { } private MonitoredResource getDefaultResource() { + // Are we running on a GAE instance? + if (gaeInstanceId!=null && options.getProjectId()!=null) { + MonitoredResource.Builder builder = MonitoredResource.newBuilder("gae_app") + .addLabel("project_id", options.getProjectId()); + if (System.getenv("GAE_SERVICE")!=null) { + builder.addLabel("module_id", System.getenv("GAE_SERVICE")); + } + if (System.getenv("GAE_VERSION")!=null) { + builder.addLabel("version_id", System.getenv("GAE_VERSION")); + } + return builder.build(); + } + return MonitoredResource.of("global", ImmutableMap.of("project_id", options.getProjectId())); } @@ -310,6 +327,10 @@ private LogEntry entryFor(LogRecord record) { .addLabel("levelName", level.getName()) .addLabel("levelValue", String.valueOf(level.intValue())) .setSeverity(severityFor(level)); + if (gaeInstanceId != null) { + builder.addLabel("appengine.googleapis.com/instance_name", gaeInstanceId); + } + enhanceLogEntry(builder, record); return builder.build(); } From 4801395fc4cc5f4cd0fcba74d0ab3b29c4ffb69d Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Mon, 16 Jan 2017 13:27:28 +1100 Subject: [PATCH 02/11] Logging Enhancer --- .../cloud/logging/GaeFlexLoggingEnhancer.java | 84 +++++++++++++++++++ .../google/cloud/logging/LoggingHandler.java | 81 +++++++++++++----- 2 files changed, 143 insertions(+), 22 deletions(-) create mode 100644 google-cloud-logging/src/main/java/com/google/cloud/logging/GaeFlexLoggingEnhancer.java diff --git a/google-cloud-logging/src/main/java/com/google/cloud/logging/GaeFlexLoggingEnhancer.java b/google-cloud-logging/src/main/java/com/google/cloud/logging/GaeFlexLoggingEnhancer.java new file mode 100644 index 000000000000..8f856919acf8 --- /dev/null +++ b/google-cloud-logging/src/main/java/com/google/cloud/logging/GaeFlexLoggingEnhancer.java @@ -0,0 +1,84 @@ +/* + * Copyright 2017 Google Inc. All Rights Reserved. + * + * 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.logging; + +import java.util.logging.LogRecord; + +import com.google.cloud.MonitoredResource.Builder; + +/** + * A Logging {@link Enhancer} that enhances the logging for + * GAE Flex environment. This enhance can be configured in + * a logging.properties file with: + *
+ * handlers=com.google.cloud.logging.LoggingHandler
+ * com.google.cloud.logging.LoggingHandler.log=gaeflex.log
+ * com.google.cloud.logging.LoggingHandler.resourceType=gae_app
+ * com.google.cloud.logging.LoggingHandler.enhancers=com.google.cloud.logging.GaeFlexLoggingEnhancer
+ * com.google.cloud.logging.LoggingHandler.formatter = java.util.logging.SimpleFormatter
+ * java.util.logging.SimpleFormatter.format=%3$s: %5$s%6$s
+ * 
+ * + */ +public class GaeFlexLoggingEnhancer implements LoggingHandler.Enhancer { + + private static final ThreadLocal traceId = new ThreadLocal<>(); + + /** + * Set the Trace ID associated with any logging done by + * the current thread. + * @param id The traceID + */ + public static void setCurrentTraceId(String id) { + traceId.set(id); + } + + /** + * Get the Trace ID associated with any logging done by + * the current thread. + * @return id The traceID + */ + public static String getCurrentTraceId() { + return traceId.get(); + } + + private String gaeInstanceId; + + @Override + public void enhanceMonitoredResource(Builder builder) { + gaeInstanceId = System.getenv("GAE_INSTANCE"); // Are we running on a GAE instance? + if (gaeInstanceId!=null) { + if (System.getenv("GAE_SERVICE")!=null) { + builder.addLabel("module_id", System.getenv("GAE_SERVICE")); + } + if (System.getenv("GAE_VERSION")!=null) { + builder.addLabel("version_id", System.getenv("GAE_VERSION")); + } + } + } + + @Override + public void enhanceLogEntry(com.google.cloud.logging.LogEntry.Builder builder, LogRecord record) { + if (gaeInstanceId != null) { + builder.addLabel("appengine.googleapis.com/instance_name", gaeInstanceId); + } + String traceId = getCurrentTraceId(); + if (traceId != null) { + builder.addLabel("appengine.googleapis.com/trace_id", traceId); + } + } +} 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 4789faace678..2db7d1179d62 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 @@ -21,7 +21,9 @@ import com.google.cloud.MonitoredResource; import com.google.cloud.logging.Logging.WriteOption; import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; + +import java.util.ArrayList; +import java.util.Collections; import java.util.LinkedList; import java.util.List; import java.util.logging.ErrorManager; @@ -95,11 +97,11 @@ public class LoggingHandler extends Handler { private final LoggingOptions options; private final WriteOption[] writeOptions; - private final String gaeInstanceId; private List buffer = new LinkedList<>(); private volatile Logging logging; private Level flushLevel; private long flushSize; + private final List enhancers; /** * Creates an handler that publishes messages to Stackdriver Logging. @@ -137,6 +139,20 @@ public LoggingHandler(String log, LoggingOptions options) { * then a more comprehensive default resource may be created. */ public LoggingHandler(String log, LoggingOptions options, MonitoredResource monitoredResource) { + this(log, options, monitoredResource,null); + } + + /** + * Creates a handler that publishes messages to Stackdriver Logging. + * + * @param log the name of the log to which log entries are written + * @param options options for the Stackdriver Logging service + * @param monitoredResource the monitored resource to which log entries refer. If null a default + * @param enhancers List of {@link Enhancer} instances. + * resource is created based on the project ID. If a Google App Engine environment is detected + * then a more comprehensive default resource may be created. + */ + public LoggingHandler(String log, LoggingOptions options, MonitoredResource monitoredResource, List enhancers) { LogConfigHelper helper = new LogConfigHelper(); String className = getClass().getName(); this.options = options != null ? options : LoggingOptions.getDefaultInstance(); @@ -146,8 +162,9 @@ public LoggingHandler(String log, LoggingOptions options, MonitoredResource moni setFilter(helper.getFilterProperty(className + ".filter", null)); setFormatter(helper.getFormatterProperty(className + ".formatter", new SimpleFormatter())); String logName = firstNonNull(log, helper.getProperty(className + ".log", "java.log")); - gaeInstanceId = System.getenv("GAE_INSTANCE"); - MonitoredResource resource = firstNonNull(monitoredResource, getDefaultResource()); + this.enhancers = firstNonNull(enhancers, helper.getEnhancerProperty(className + ".enhancers")); + String resourceType = helper.getProperty(className + ".resourceType", "global"); + MonitoredResource resource = firstNonNull(monitoredResource, getDefaultResource(resourceType)); writeOptions = new WriteOption[]{WriteOption.logName(logName), WriteOption.resource(resource)}; } @@ -182,21 +199,13 @@ private static boolean hasLoggingHandler(Logger logger) { return false; } - private MonitoredResource getDefaultResource() { - // Are we running on a GAE instance? - if (gaeInstanceId!=null && options.getProjectId()!=null) { - MonitoredResource.Builder builder = MonitoredResource.newBuilder("gae_app") - .addLabel("project_id", options.getProjectId()); - if (System.getenv("GAE_SERVICE")!=null) { - builder.addLabel("module_id", System.getenv("GAE_SERVICE")); - } - if (System.getenv("GAE_VERSION")!=null) { - builder.addLabel("version_id", System.getenv("GAE_VERSION")); - } - return builder.build(); + private MonitoredResource getDefaultResource(String resourceType) { + MonitoredResource.Builder builder = MonitoredResource.newBuilder(resourceType); + builder.addLabel("project_id", options.getProjectId()); + for (Enhancer enhancer : enhancers) { + enhancer.enhanceMonitoredResource(builder); } - - return MonitoredResource.of("global", ImmutableMap.of("project_id", options.getProjectId())); + return builder.build(); } private static class LogConfigHelper { @@ -254,6 +263,25 @@ Formatter getFormatterProperty(String name, Formatter defaultValue) { } return defaultValue; } + + List getEnhancerProperty(String name) { + String list = manager.getProperty(name); + try { + List enhancers = new ArrayList<>(); + if (list != null) { + String[] items = list.split(","); + for (String e_name : items) { + Class clz = (Class) ClassLoader.getSystemClassLoader().loadClass(e_name); + enhancers.add((Enhancer) clz.newInstance()); + } + } + return enhancers; + } catch (Exception ex) { + ex.printStackTrace(); + // If we cannot create the enhancers we fall back to the default + } + return Collections.emptyList(); + } } /** @@ -327,16 +355,15 @@ private LogEntry entryFor(LogRecord record) { .addLabel("levelName", level.getName()) .addLabel("levelValue", String.valueOf(level.intValue())) .setSeverity(severityFor(level)); - if (gaeInstanceId != null) { - builder.addLabel("appengine.googleapis.com/instance_name", gaeInstanceId); - } enhanceLogEntry(builder, record); return builder.build(); } protected void enhanceLogEntry(LogEntry.Builder builder, LogRecord record) { - // no-op in this class + for (Enhancer enhancer : enhancers) { + enhancer.enhanceLogEntry(builder, record); + } } private static Severity severityFor(Level level) { @@ -450,4 +477,14 @@ public synchronized long setFlushSize(long flushSize) { public static void addHandler(Logger logger, LoggingHandler handler) { logger.addHandler(handler); } + + /** + * A Log Enhancer. + * May be used to enhanced the {@link MonitoredResource} and/or the {@link LogEntry} + */ + interface Enhancer { + void enhanceMonitoredResource(MonitoredResource.Builder builder); + void enhanceLogEntry(LogEntry.Builder builder, LogRecord record); + } + } From e7daf1f32b8aa767ce791c4764feb60f078b2e0d Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Mon, 16 Jan 2017 13:37:14 +1100 Subject: [PATCH 03/11] Fixed codacy bot issues --- .../java/com/google/cloud/logging/GaeFlexLoggingEnhancer.java | 4 ++-- .../main/java/com/google/cloud/logging/LoggingHandler.java | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/google-cloud-logging/src/main/java/com/google/cloud/logging/GaeFlexLoggingEnhancer.java b/google-cloud-logging/src/main/java/com/google/cloud/logging/GaeFlexLoggingEnhancer.java index 8f856919acf8..71f1d2ae278b 100644 --- a/google-cloud-logging/src/main/java/com/google/cloud/logging/GaeFlexLoggingEnhancer.java +++ b/google-cloud-logging/src/main/java/com/google/cloud/logging/GaeFlexLoggingEnhancer.java @@ -37,6 +37,8 @@ public class GaeFlexLoggingEnhancer implements LoggingHandler.Enhancer { private static final ThreadLocal traceId = new ThreadLocal<>(); + + private String gaeInstanceId; /** * Set the Trace ID associated with any logging done by @@ -56,8 +58,6 @@ public static String getCurrentTraceId() { return traceId.get(); } - private String gaeInstanceId; - @Override public void enhanceMonitoredResource(Builder builder) { gaeInstanceId = System.getenv("GAE_INSTANCE"); // Are we running on a GAE instance? 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 2db7d1179d62..a40a8936b84c 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 @@ -277,7 +277,6 @@ List getEnhancerProperty(String name) { } return enhancers; } catch (Exception ex) { - ex.printStackTrace(); // If we cannot create the enhancers we fall back to the default } return Collections.emptyList(); From a0132cfa4c3d0c9e4d1e7d515d2ddbe416364842 Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Tue, 17 Jan 2017 18:07:10 +1100 Subject: [PATCH 04/11] Fixed review points --- .../cloud/logging/GaeFlexLoggingEnhancer.java | 38 ++++++++++--------- .../google/cloud/logging/LoggingHandler.java | 12 +++--- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/google-cloud-logging/src/main/java/com/google/cloud/logging/GaeFlexLoggingEnhancer.java b/google-cloud-logging/src/main/java/com/google/cloud/logging/GaeFlexLoggingEnhancer.java index 71f1d2ae278b..26e0b919249d 100644 --- a/google-cloud-logging/src/main/java/com/google/cloud/logging/GaeFlexLoggingEnhancer.java +++ b/google-cloud-logging/src/main/java/com/google/cloud/logging/GaeFlexLoggingEnhancer.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 Google Inc. All Rights Reserved. + * Copyright 2016 Google Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,9 +21,10 @@ import com.google.cloud.MonitoredResource.Builder; /** - * A Logging {@link Enhancer} that enhances the logging for - * GAE Flex environment. This enhance can be configured in - * a logging.properties file with: + * A Logging {@link Enhancer} that enhances the logging for the + * GAE Flex environment. This enhancer can + * be configured in a logging.properties file with: + * *
  * handlers=com.google.cloud.logging.LoggingHandler
  * com.google.cloud.logging.LoggingHandler.log=gaeflex.log
@@ -35,14 +36,14 @@
  *
  */
 public class GaeFlexLoggingEnhancer implements LoggingHandler.Enhancer {
-  
+
   private static final ThreadLocal traceId = new ThreadLocal<>();
-  
+
   private String gaeInstanceId;
 
   /**
-   * Set the Trace ID associated with any logging done by 
-   * the current thread.
+   * Set the Trace ID associated with any logging done by the current thread.
+   * 
    * @param id The traceID
    */
   public static void setCurrentTraceId(String id) {
@@ -50,22 +51,25 @@ public static void setCurrentTraceId(String id) {
   }
 
   /**
-   * Get the Trace ID associated with any logging done by 
-   * the current thread.
+   * Get the Trace ID associated with any logging done by the current thread.
+   * 
    * @return id The traceID
    */
   public static String getCurrentTraceId() {
     return traceId.get();
   }
-  
+
+  public GaeFlexLoggingEnhancer() {
+    gaeInstanceId = System.getenv("GAE_INSTANCE"); // Are we running on a GAE instance?
+  }
+
   @Override
   public void enhanceMonitoredResource(Builder builder) {
-    gaeInstanceId = System.getenv("GAE_INSTANCE");    // Are we running on a GAE instance?
-    if (gaeInstanceId!=null) {
-      if (System.getenv("GAE_SERVICE")!=null) {
+    if (gaeInstanceId != null) {
+      if (System.getenv("GAE_SERVICE") != null) {
         builder.addLabel("module_id", System.getenv("GAE_SERVICE"));
       }
-      if (System.getenv("GAE_VERSION")!=null) {
+      if (System.getenv("GAE_VERSION") != null) {
         builder.addLabel("version_id", System.getenv("GAE_VERSION"));
       }
     }
@@ -75,10 +79,10 @@ public void enhanceMonitoredResource(Builder builder) {
   public void enhanceLogEntry(com.google.cloud.logging.LogEntry.Builder builder, LogRecord record) {
     if (gaeInstanceId != null) {
       builder.addLabel("appengine.googleapis.com/instance_name", gaeInstanceId);
-    } 
+    }
     String traceId = getCurrentTraceId();
     if (traceId != null) {
       builder.addLabel("appengine.googleapis.com/trace_id", traceId);
-    } 
+    }
   }
 }
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 a40a8936b84c..45ac48701a15 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
@@ -134,8 +134,8 @@ public LoggingHandler(String log, LoggingOptions options) {
    *
    * @param log the name of the log to which log entries are written
    * @param options options for the Stackdriver Logging service
-   * @param monitoredResource the monitored resource to which log entries refer. If null a default
-   * resource is created based on the project ID.  If a Google App Engine environment is detected
+   * @param monitoredResource the monitored resource to which log entries refer. If it is null 
+   * then a default resource is created based on the project ID.  If a Google App Engine environment is detected
    * then a more comprehensive default resource may be created.
    */
   public LoggingHandler(String log, LoggingOptions options, MonitoredResource monitoredResource) {
@@ -147,10 +147,10 @@ public LoggingHandler(String log, LoggingOptions options, MonitoredResource moni
    *
    * @param log the name of the log to which log entries are written
    * @param options options for the Stackdriver Logging service
-   * @param monitoredResource the monitored resource to which log entries refer. If null a default
-   * @param enhancers List of {@link Enhancer} instances.
-   * resource is created based on the project ID.  If a Google App Engine environment is detected
+   * @param monitoredResource the monitored resource to which log entries refer. If it is null 
+   * then a default resource is created based on the project ID.  If a Google App Engine environment is detected
    * then a more comprehensive default resource may be created.
+   * @param enhancers List of {@link Enhancer} instances.
    */
   public LoggingHandler(String log, LoggingOptions options, MonitoredResource monitoredResource, List enhancers) {
     LogConfigHelper helper = new LogConfigHelper();
@@ -479,7 +479,7 @@ public static void addHandler(Logger logger, LoggingHandler handler) {
   
   /**
    * A Log Enhancer.
-   * May be used to enhanced the {@link MonitoredResource} and/or the {@link LogEntry}
+   * May be used to enhance the {@link MonitoredResource} and/or the {@link LogEntry}
    */
   interface Enhancer {
     void enhanceMonitoredResource(MonitoredResource.Builder builder);

From 0676de0b514da5d7b0296dd88c20665b0a74bc47 Mon Sep 17 00:00:00 2001
From: Greg Wilkins 
Date: Tue, 17 Jan 2017 21:02:08 +1100
Subject: [PATCH 05/11] fixed codacy-bot recommendation

---
 .../java/com/google/cloud/logging/GaeFlexLoggingEnhancer.java   | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/google-cloud-logging/src/main/java/com/google/cloud/logging/GaeFlexLoggingEnhancer.java b/google-cloud-logging/src/main/java/com/google/cloud/logging/GaeFlexLoggingEnhancer.java
index 26e0b919249d..b4c80e288007 100644
--- a/google-cloud-logging/src/main/java/com/google/cloud/logging/GaeFlexLoggingEnhancer.java
+++ b/google-cloud-logging/src/main/java/com/google/cloud/logging/GaeFlexLoggingEnhancer.java
@@ -39,7 +39,7 @@ public class GaeFlexLoggingEnhancer implements LoggingHandler.Enhancer {
 
   private static final ThreadLocal traceId = new ThreadLocal<>();
 
-  private String gaeInstanceId;
+  private final String gaeInstanceId;
 
   /**
    * Set the Trace ID associated with any logging done by the current thread.

From 0aa59f06929d931bb687471cca394a699d5c8819 Mon Sep 17 00:00:00 2001
From: Greg Wilkins 
Date: Tue, 17 Jan 2017 21:25:59 +1100
Subject: [PATCH 06/11] Do not create default field value if non null valued
 passed

---
 .../main/java/com/google/cloud/logging/LoggingHandler.java    | 4 ++--
 1 file changed, 2 insertions(+), 2 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 45ac48701a15..23a3f30348ad 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
@@ -162,9 +162,9 @@ public LoggingHandler(String log, LoggingOptions options, MonitoredResource moni
     setFilter(helper.getFilterProperty(className + ".filter", null));
     setFormatter(helper.getFormatterProperty(className + ".formatter", new SimpleFormatter()));
     String logName = firstNonNull(log, helper.getProperty(className + ".log", "java.log"));
-    this.enhancers = firstNonNull(enhancers, helper.getEnhancerProperty(className + ".enhancers"));
+    this.enhancers = enhancers != null ? enhancers : helper.getEnhancerProperty(className + ".enhancers");
     String resourceType = helper.getProperty(className + ".resourceType", "global");
-    MonitoredResource resource = firstNonNull(monitoredResource, getDefaultResource(resourceType));
+    MonitoredResource resource = monitoredResource != null ? monitoredResource : getDefaultResource(resourceType);
     writeOptions = new WriteOption[]{WriteOption.logName(logName), WriteOption.resource(resource)};
   }
 

From e1740070f15ef89626394e7e5b1bb17784a74f2a Mon Sep 17 00:00:00 2001
From: Greg Wilkins 
Date: Tue, 17 Jan 2017 21:26:11 +1100
Subject: [PATCH 07/11] Added unit test

---
 .../cloud/logging/LoggingHandlerTest.java     | 36 +++++++++++++++++++
 1 file changed, 36 insertions(+)

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 e48b2051cd5d..a543b0580ec9 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
@@ -17,11 +17,14 @@
 package com.google.cloud.logging;
 
 import com.google.cloud.MonitoredResource;
+import com.google.cloud.logging.LogEntry.Builder;
 import com.google.cloud.logging.Logging.WriteOption;
 import com.google.cloud.logging.Payload.StringPayload;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.util.concurrent.Futures;
+
+import java.util.Collections;
 import java.util.logging.ErrorManager;
 import java.util.logging.Formatter;
 import java.util.logging.Handler;
@@ -45,6 +48,12 @@ public class LoggingHandlerTest {
       .addLabel("levelName", "FINEST")
       .addLabel("levelValue", String.valueOf(Level.FINEST.intValue()))
       .build();
+  private static final LogEntry FINEST_ENHANCED_ENTRY = LogEntry.newBuilder(StringPayload.of(MESSAGE))
+      .setSeverity(Severity.DEBUG)
+      .addLabel("levelName", "FINEST")
+      .addLabel("levelValue", String.valueOf(Level.FINEST.intValue()))
+      .addLabel("enhanced", "true")
+      .build();
   private static final LogEntry FINER_ENTRY = LogEntry.newBuilder(StringPayload.of(MESSAGE))
       .setSeverity(Severity.DEBUG)
       .addLabel("levelName", "FINER")
@@ -207,6 +216,33 @@ public void testPublishCustomResource() {
     handler.publish(new LogRecord(Level.FINEST, MESSAGE));
   }
 
+  @Test
+  public void testEnhancedLogEntry() {
+    EasyMock.expect(options.getProjectId()).andReturn(PROJECT).anyTimes();
+    EasyMock.expect(options.getService()).andReturn(logging);
+    MonitoredResource resource = MonitoredResource.of("custom", ImmutableMap.of());
+    logging.writeAsync(ImmutableList.of(FINEST_ENHANCED_ENTRY), WriteOption.logName(LOG_NAME),
+        WriteOption.resource(resource));
+    EasyMock.expectLastCall().andReturn(Futures.immediateFuture(null));
+    EasyMock.replay(options, logging);
+    LoggingHandler.Enhancer enhancer = new LoggingHandler.Enhancer() {
+      @Override
+      public void enhanceMonitoredResource(com.google.cloud.MonitoredResource.Builder builder) {
+        throw new IllegalStateException();
+      }
+
+      @Override
+      public void enhanceLogEntry(Builder builder, LogRecord record) {
+        builder.addLabel("enhanced", "true");
+      }
+    };
+    Handler handler =
+        new LoggingHandler(LOG_NAME, options, resource, Collections.singletonList(enhancer));
+    handler.setLevel(Level.ALL);
+    handler.setFormatter(new TestFormatter());
+    handler.publish(new LogRecord(Level.FINEST, MESSAGE));
+  }
+  
   @Test
   public void testReportFlushError() {
     EasyMock.expect(options.getProjectId()).andReturn(PROJECT).anyTimes();

From c64eb42cdd72c99273285a5bb1051ee3278097b4 Mon Sep 17 00:00:00 2001
From: Greg Wilkins 
Date: Tue, 17 Jan 2017 21:34:54 +1100
Subject: [PATCH 08/11] fixed codacy-bot recommendation

---
 .../test/java/com/google/cloud/logging/LoggingHandlerTest.java  | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

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 a543b0580ec9..6b593e665fa2 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
@@ -227,7 +227,7 @@ public void testEnhancedLogEntry() {
     EasyMock.replay(options, logging);
     LoggingHandler.Enhancer enhancer = new LoggingHandler.Enhancer() {
       @Override
-      public void enhanceMonitoredResource(com.google.cloud.MonitoredResource.Builder builder) {
+      public void enhanceMonitoredResource(MonitoredResource.Builder builder) {
         throw new IllegalStateException();
       }
 

From c93532ac5d799c60df333cd72fa7b8ef799de244 Mon Sep 17 00:00:00 2001
From: Greg Wilkins 
Date: Thu, 19 Jan 2017 09:48:17 +1100
Subject: [PATCH 09/11] improved javadoc

---
 .../google/cloud/logging/LoggingHandler.java  | 20 ++++++++++++++-----
 1 file changed, 15 insertions(+), 5 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 23a3f30348ad..1dae22e60dda 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
@@ -78,6 +78,11 @@
  * 
  • {@code com.google.cloud.logging.LoggingHandler.flushLevel} specifies the flush log level. * When a log with this level is published, logs are transmitted to the Stackdriver Logging * service (defaults to {@link LoggingLevel#ERROR}). + *
  • {@code com.google.cloud.logging.LoggingHandler.enhancers} specifies a comma separated list + * of {@link Enhancer} classes. This handler will call each enhancer list whenever it builds + * a {@link MonitoredResource} or {@link LogEntry} instance (defaults to empty list). + *
  • {@code com.google.cloud.logging.LoggingHandler.resourceType} the type name to use when + * creating the default {@link MonitoredResource} (defaults to "global"). * * *

    To add a {@code LoggingHandler} to an existing {@link Logger} and be sure to avoid infinite @@ -135,8 +140,10 @@ public LoggingHandler(String log, LoggingOptions options) { * @param log the name of the log to which log entries are written * @param options options for the Stackdriver Logging service * @param monitoredResource the monitored resource to which log entries refer. If it is null - * then a default resource is created based on the project ID. If a Google App Engine environment is detected - * then a more comprehensive default resource may be created. + * then a default resource is created based on the project ID. When creating a default resource, if + * any {@link Enhancer} instances are configured and then each + * {@link Enhancer#enhanceMonitoredResource(com.google.cloud.MonitoredResource.Builder)} method + * is called before building the default resource. */ public LoggingHandler(String log, LoggingOptions options, MonitoredResource monitoredResource) { this(log, options, monitoredResource,null); @@ -148,9 +155,12 @@ public LoggingHandler(String log, LoggingOptions options, MonitoredResource moni * @param log the name of the log to which log entries are written * @param options options for the Stackdriver Logging service * @param monitoredResource the monitored resource to which log entries refer. If it is null - * then a default resource is created based on the project ID. If a Google App Engine environment is detected - * then a more comprehensive default resource may be created. - * @param enhancers List of {@link Enhancer} instances. + * then a default resource is created based on the project ID. When creating a default resource, if + * any {@link Enhancer} instances are configured and then each + * {@link Enhancer#enhanceMonitoredResource(com.google.cloud.MonitoredResource.Builder)} method + * is called before building the default resource. + * @param enhancers List of {@link Enhancer} instances used to enhance any {@link MonitoredResource} + * or {@link LogEntry} instances built by this handler. */ public LoggingHandler(String log, LoggingOptions options, MonitoredResource monitoredResource, List enhancers) { LogConfigHelper helper = new LogConfigHelper(); From 3185bc2a97037857b5c617d1a0d47f3cd336ff90 Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Thu, 19 Jan 2017 09:48:57 +1100 Subject: [PATCH 10/11] deprecate enhanceLogEntry --- .../java/com/google/cloud/logging/LoggingHandler.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 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 1dae22e60dda..df80d361d9a8 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 @@ -364,15 +364,16 @@ private LogEntry entryFor(LogRecord record) { .addLabel("levelName", level.getName()) .addLabel("levelValue", String.valueOf(level.intValue())) .setSeverity(severityFor(level)); - + + for (Enhancer enhancer : enhancers) { + enhancer.enhanceLogEntry(builder, record); + } enhanceLogEntry(builder, record); return builder.build(); } + @Deprecated protected void enhanceLogEntry(LogEntry.Builder builder, LogRecord record) { - for (Enhancer enhancer : enhancers) { - enhancer.enhanceLogEntry(builder, record); - } } private static Severity severityFor(Level level) { From 823dda77aee8b246cedfabdf83cf497d6fd768a7 Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Thu, 19 Jan 2017 10:39:57 +1100 Subject: [PATCH 11/11] fixed test after merge --- .../test/java/com/google/cloud/logging/LoggingHandlerTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 1c19d4d40922..7b4f3298c1d4 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 @@ -54,6 +54,7 @@ public class LoggingHandlerTest { .addLabel("levelName", "FINEST") .addLabel("levelValue", String.valueOf(Level.FINEST.intValue())) .addLabel("enhanced", "true") + .setTimestamp(123456789L) .build(); private static final LogEntry FINER_ENTRY = LogEntry.newBuilder(StringPayload.of(MESSAGE)) .setSeverity(Severity.DEBUG) @@ -260,7 +261,7 @@ public void enhanceLogEntry(Builder builder, LogRecord record) { new LoggingHandler(LOG_NAME, options, resource, Collections.singletonList(enhancer)); handler.setLevel(Level.ALL); handler.setFormatter(new TestFormatter()); - handler.publish(new LogRecord(Level.FINEST, MESSAGE)); + handler.publish(newLogRecord(Level.FINEST, MESSAGE)); } @Test