You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2022/01/25 13:58:22 UTC

[camel] branch main updated: CAMEL-17549: log component - Add option to have it use source location:line as logger name

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 3318bdb  CAMEL-17549: log component - Add option to have it use source location:line as logger name
3318bdb is described below

commit 3318bdbaa3f954594d8044c303e245a18b1443e7
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue Jan 25 14:51:55 2022 +0100

    CAMEL-17549: log component - Add option to have it use source location:line as logger name
---
 .../component/log/LogComponentConfigurer.java      |  6 +++
 .../camel/component/log/LogEndpointConfigurer.java |  6 +++
 .../camel/component/log/LogEndpointUriFactory.java |  3 +-
 .../org/apache/camel/component/log/log.json        |  2 +
 .../apache/camel/component/log/LogComponent.java   | 27 ++++++++++++-
 .../apache/camel/component/log/LogEndpoint.java    | 47 +++++++++++++++++++++-
 .../java/org/apache/camel/LineNumberAware.java     | 11 +++++
 .../java/org/apache/camel/reifier/SendReifier.java | 10 +++--
 .../org/apache/camel/reifier/WireTapReifier.java   |  2 +
 9 files changed, 106 insertions(+), 8 deletions(-)

diff --git a/components/camel-log/src/generated/java/org/apache/camel/component/log/LogComponentConfigurer.java b/components/camel-log/src/generated/java/org/apache/camel/component/log/LogComponentConfigurer.java
index 8b2c6a2..6bfc1a0 100644
--- a/components/camel-log/src/generated/java/org/apache/camel/component/log/LogComponentConfigurer.java
+++ b/components/camel-log/src/generated/java/org/apache/camel/component/log/LogComponentConfigurer.java
@@ -27,6 +27,8 @@ public class LogComponentConfigurer extends PropertyConfigurerSupport implements
         case "exchangeFormatter": target.setExchangeFormatter(property(camelContext, org.apache.camel.spi.ExchangeFormatter.class, value)); return true;
         case "lazystartproducer":
         case "lazyStartProducer": target.setLazyStartProducer(property(camelContext, boolean.class, value)); return true;
+        case "sourcelocationloggername":
+        case "sourceLocationLoggerName": target.setSourceLocationLoggerName(property(camelContext, boolean.class, value)); return true;
         default: return false;
         }
     }
@@ -45,6 +47,8 @@ public class LogComponentConfigurer extends PropertyConfigurerSupport implements
         case "exchangeFormatter": return org.apache.camel.spi.ExchangeFormatter.class;
         case "lazystartproducer":
         case "lazyStartProducer": return boolean.class;
+        case "sourcelocationloggername":
+        case "sourceLocationLoggerName": return boolean.class;
         default: return null;
         }
     }
@@ -59,6 +63,8 @@ public class LogComponentConfigurer extends PropertyConfigurerSupport implements
         case "exchangeFormatter": return target.getExchangeFormatter();
         case "lazystartproducer":
         case "lazyStartProducer": return target.isLazyStartProducer();
+        case "sourcelocationloggername":
+        case "sourceLocationLoggerName": return target.isSourceLocationLoggerName();
         default: return null;
         }
     }
diff --git a/components/camel-log/src/generated/java/org/apache/camel/component/log/LogEndpointConfigurer.java b/components/camel-log/src/generated/java/org/apache/camel/component/log/LogEndpointConfigurer.java
index e2a5837..993c787 100644
--- a/components/camel-log/src/generated/java/org/apache/camel/component/log/LogEndpointConfigurer.java
+++ b/components/camel-log/src/generated/java/org/apache/camel/component/log/LogEndpointConfigurer.java
@@ -71,6 +71,8 @@ public class LogEndpointConfigurer extends PropertyConfigurerSupport implements
         case "showStreams": target.setShowStreams(property(camelContext, boolean.class, value)); return true;
         case "skipbodylineseparator":
         case "skipBodyLineSeparator": target.setSkipBodyLineSeparator(property(camelContext, boolean.class, value)); return true;
+        case "sourcelocationloggername":
+        case "sourceLocationLoggerName": target.setSourceLocationLoggerName(property(camelContext, boolean.class, value)); return true;
         case "style": target.setStyle(property(camelContext, org.apache.camel.support.processor.DefaultExchangeFormatter.OutputStyle.class, value)); return true;
         default: return false;
         }
@@ -129,6 +131,8 @@ public class LogEndpointConfigurer extends PropertyConfigurerSupport implements
         case "showStreams": return boolean.class;
         case "skipbodylineseparator":
         case "skipBodyLineSeparator": return boolean.class;
+        case "sourcelocationloggername":
+        case "sourceLocationLoggerName": return boolean.class;
         case "style": return org.apache.camel.support.processor.DefaultExchangeFormatter.OutputStyle.class;
         default: return null;
         }
@@ -188,6 +192,8 @@ public class LogEndpointConfigurer extends PropertyConfigurerSupport implements
         case "showStreams": return target.isShowStreams();
         case "skipbodylineseparator":
         case "skipBodyLineSeparator": return target.isSkipBodyLineSeparator();
+        case "sourcelocationloggername":
+        case "sourceLocationLoggerName": return target.isSourceLocationLoggerName();
         case "style": return target.getStyle();
         default: return null;
         }
diff --git a/components/camel-log/src/generated/java/org/apache/camel/component/log/LogEndpointUriFactory.java b/components/camel-log/src/generated/java/org/apache/camel/component/log/LogEndpointUriFactory.java
index 756b855..9afe548 100644
--- a/components/camel-log/src/generated/java/org/apache/camel/component/log/LogEndpointUriFactory.java
+++ b/components/camel-log/src/generated/java/org/apache/camel/component/log/LogEndpointUriFactory.java
@@ -21,11 +21,12 @@ public class LogEndpointUriFactory extends org.apache.camel.support.component.En
     private static final Set<String> SECRET_PROPERTY_NAMES;
     private static final Set<String> MULTI_VALUE_PREFIXES;
     static {
-        Set<String> props = new HashSet<>(29);
+        Set<String> props = new HashSet<>(30);
         props.add("showProperties");
         props.add("showStreams");
         props.add("showHeaders");
         props.add("showException");
+        props.add("sourceLocationLoggerName");
         props.add("showExchangeId");
         props.add("logMask");
         props.add("groupActiveOnly");
diff --git a/components/camel-log/src/generated/resources/org/apache/camel/component/log/log.json b/components/camel-log/src/generated/resources/org/apache/camel/component/log/log.json
index 5e40356..7f84903 100644
--- a/components/camel-log/src/generated/resources/org/apache/camel/component/log/log.json
+++ b/components/camel-log/src/generated/resources/org/apache/camel/component/log/log.json
@@ -23,6 +23,7 @@
   },
   "componentProperties": {
     "lazyStartProducer": { "kind": "property", "displayName": "Lazy Start Producer", "group": "producer", "label": "producer", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": false, "description": "Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during star [...]
+    "sourceLocationLoggerName": { "kind": "property", "displayName": "Source Location Logger Name", "group": "producer", "label": "", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": false, "description": "If enabled then the source location of where the log endpoint is used in Camel routes, would be used as logger name, instead of the given name. However, if the source location is disabled or not possi [...]
     "autowiredEnabled": { "kind": "property", "displayName": "Autowired Enabled", "group": "advanced", "label": "advanced", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "description": "Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which t [...]
     "exchangeFormatter": { "kind": "property", "displayName": "Exchange Formatter", "group": "advanced", "label": "advanced", "required": false, "type": "object", "javaType": "org.apache.camel.spi.ExchangeFormatter", "deprecated": false, "autowired": true, "secret": false, "description": "Sets a custom ExchangeFormatter to convert the Exchange to a String suitable for logging. If not specified, we default to DefaultExchangeFormatter." }
   },
@@ -37,6 +38,7 @@
     "logMask": { "kind": "parameter", "displayName": "Log Mask", "group": "producer", "label": "", "required": false, "type": "boolean", "javaType": "java.lang.Boolean", "deprecated": false, "autowired": false, "secret": false, "description": "If true, mask sensitive information like password or passphrase in the log." },
     "marker": { "kind": "parameter", "displayName": "Marker", "group": "producer", "label": "", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "description": "An optional Marker name to use." },
     "plain": { "kind": "parameter", "displayName": "Plain", "group": "producer", "label": "", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": false, "description": "If enabled only the body will be printed out" },
+    "sourceLocationLoggerName": { "kind": "parameter", "displayName": "Source Location Logger Name", "group": "producer", "label": "", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": false, "description": "If enabled then the source location of where the log endpoint is used in Camel routes, would be used as logger name, instead of the given name. However, if the source location is disabled or not poss [...]
     "exchangeFormatter": { "kind": "parameter", "displayName": "Exchange Formatter", "group": "advanced", "label": "advanced", "required": false, "type": "object", "javaType": "org.apache.camel.spi.ExchangeFormatter", "deprecated": false, "autowired": false, "secret": false, "description": "To use a custom exchange formatter" },
     "maxChars": { "kind": "parameter", "displayName": "Max Chars", "group": "formatting", "label": "formatting", "required": false, "type": "integer", "javaType": "int", "deprecated": false, "autowired": false, "secret": false, "defaultValue": 10000, "description": "Limits the number of characters logged per line." },
     "multiline": { "kind": "parameter", "displayName": "Multiline", "group": "formatting", "label": "formatting", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": false, "description": "If enabled then each information is outputted on a newline." },
diff --git a/components/camel-log/src/main/java/org/apache/camel/component/log/LogComponent.java b/components/camel-log/src/main/java/org/apache/camel/component/log/LogComponent.java
index 7c06d49..4f0166e 100644
--- a/components/camel-log/src/main/java/org/apache/camel/component/log/LogComponent.java
+++ b/components/camel-log/src/main/java/org/apache/camel/component/log/LogComponent.java
@@ -29,6 +29,8 @@ import org.apache.camel.support.processor.DefaultExchangeFormatter;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import static org.apache.camel.support.LoggerHelper.getLineNumberLoggerName;
+
 /**
  * The <a href="http://camel.apache.org/log.html">Log Component</a> is for logging message exchanges via the underlying
  * logging mechanism.
@@ -42,6 +44,8 @@ public class LogComponent extends DefaultComponent {
 
     @Metadata(label = "advanced", autowired = true)
     private ExchangeFormatter exchangeFormatter;
+    @Metadata
+    private boolean sourceLocationLoggerName;
 
     public LogComponent() {
     }
@@ -76,13 +80,19 @@ public class LogComponent extends DefaultComponent {
 
         LogEndpoint endpoint = new LogEndpoint(uri, this);
         endpoint.setLevel(level.name());
+        endpoint.setSourceLocationLoggerName(sourceLocationLoggerName);
         endpoint.setExchangeFormatter(logFormatter);
+        setProperties(endpoint, parameters);
+
         if (providedLogger == null) {
-            endpoint.setLoggerName(remaining);
+            String loggerName = endpoint.isSourceLocationLoggerName() ? getLineNumberLoggerName(endpoint) : null;
+            if (loggerName == null) {
+                loggerName = remaining;
+            }
+            endpoint.setLoggerName(loggerName);
         } else {
             endpoint.setProvidedLogger(providedLogger);
         }
-        setProperties(endpoint, parameters);
 
         return endpoint;
     }
@@ -122,6 +132,19 @@ public class LogComponent extends DefaultComponent {
         this.exchangeFormatter = exchangeFormatter;
     }
 
+    public boolean isSourceLocationLoggerName() {
+        return sourceLocationLoggerName;
+    }
+
+    /**
+     * If enabled then the source location of where the log endpoint is used in Camel routes, would be used as logger
+     * name, instead of the given name. However, if the source location is disabled or not possible to resolve then the
+     * existing logger name will be used.
+     */
+    public void setSourceLocationLoggerName(boolean sourceLocationLoggerName) {
+        this.sourceLocationLoggerName = sourceLocationLoggerName;
+    }
+
     /**
      * Gets the default shared exchange formatter.
      */
diff --git a/components/camel-log/src/main/java/org/apache/camel/component/log/LogEndpoint.java b/components/camel-log/src/main/java/org/apache/camel/component/log/LogEndpoint.java
index 6df0b1d..16d8977 100644
--- a/components/camel-log/src/main/java/org/apache/camel/component/log/LogEndpoint.java
+++ b/components/camel-log/src/main/java/org/apache/camel/component/log/LogEndpoint.java
@@ -19,6 +19,7 @@ package org.apache.camel.component.log;
 import org.apache.camel.Category;
 import org.apache.camel.Component;
 import org.apache.camel.ExtendedCamelContext;
+import org.apache.camel.LineNumberAware;
 import org.apache.camel.LoggingLevel;
 import org.apache.camel.Processor;
 import org.apache.camel.Producer;
@@ -37,6 +38,8 @@ import org.apache.camel.support.processor.ThroughputLogger;
 import org.apache.camel.support.service.ServiceHelper;
 import org.slf4j.Logger;
 
+import static org.apache.camel.support.LoggerHelper.getLineNumberLoggerName;
+
 /**
  * Log messages to the underlying logging mechanism.
  *
@@ -44,11 +47,13 @@ import org.slf4j.Logger;
  */
 @UriEndpoint(firstVersion = "1.1.0", scheme = "log", title = "Log",
              syntax = "log:loggerName", producerOnly = true, category = { Category.CORE, Category.MONITORING })
-public class LogEndpoint extends ProcessorEndpoint {
+public class LogEndpoint extends ProcessorEndpoint implements LineNumberAware {
 
     private volatile Processor logger;
     private Logger providedLogger;
     private ExchangeFormatter localFormatter;
+    private int lineNumber;
+    private String location;
 
     @UriPath(description = "Name of the logging category to use")
     @Metadata(required = true)
@@ -120,6 +125,9 @@ public class LogEndpoint extends ProcessorEndpoint {
     private DefaultExchangeFormatter.OutputStyle style = DefaultExchangeFormatter.OutputStyle.Default;
     @UriParam(defaultValue = "false", description = "If enabled only the body will be printed out")
     private boolean plain;
+    @UriParam(description = "If enabled then the source location of where the log endpoint is used in Camel routes, would be used as logger name, instead"
+                            + " of the given name. However, if the source location is disabled or not possible to resolve then the existing logger name will be used.")
+    private boolean sourceLocationLoggerName;
 
     public LogEndpoint() {
     }
@@ -194,6 +202,26 @@ public class LogEndpoint extends ProcessorEndpoint {
         ServiceHelper.stopService(logger);
     }
 
+    @Override
+    public int getLineNumber() {
+        return lineNumber;
+    }
+
+    @Override
+    public void setLineNumber(int lineNumber) {
+        this.lineNumber = lineNumber;
+    }
+
+    @Override
+    public String getLocation() {
+        return location;
+    }
+
+    @Override
+    public void setLocation(String location) {
+        this.location = location;
+    }
+
     public void setLogger(Processor logger) {
         this.logger = logger;
         // the logger is the processor
@@ -236,7 +264,14 @@ public class LogEndpoint extends ProcessorEndpoint {
             loggingLevel = LoggingLevel.valueOf(level);
         }
         if (providedLogger == null) {
-            camelLogger = new CamelLogger(loggerName, loggingLevel, getMarker());
+            String name = loggerName;
+            if (sourceLocationLoggerName) {
+                name = getLineNumberLoggerName(this);
+                if (name == null) {
+                    loggerName = name;
+                }
+            }
+            camelLogger = new CamelLogger(name, loggingLevel, getMarker());
         } else {
             camelLogger = new CamelLogger(providedLogger, loggingLevel, getMarker());
         }
@@ -553,4 +588,12 @@ public class LogEndpoint extends ProcessorEndpoint {
     public void setPlain(boolean plain) {
         this.plain = plain;
     }
+
+    public boolean isSourceLocationLoggerName() {
+        return sourceLocationLoggerName;
+    }
+
+    public void setSourceLocationLoggerName(boolean sourceLocationLoggerName) {
+        this.sourceLocationLoggerName = sourceLocationLoggerName;
+    }
 }
diff --git a/core/camel-api/src/main/java/org/apache/camel/LineNumberAware.java b/core/camel-api/src/main/java/org/apache/camel/LineNumberAware.java
index 941b3e5..100f687 100644
--- a/core/camel-api/src/main/java/org/apache/camel/LineNumberAware.java
+++ b/core/camel-api/src/main/java/org/apache/camel/LineNumberAware.java
@@ -46,4 +46,15 @@ public interface LineNumberAware {
      */
     void setLocation(String location);
 
+    /**
+     * Set the {@link LineNumberAware} if the object is an instance of {@link LineNumberAware}.
+     */
+    static <T> T trySetLineNumberAware(T object, LineNumberAware source) {
+        if (source != null && object instanceof LineNumberAware) {
+            ((LineNumberAware) object).setLineNumber(source.getLineNumber());
+            ((LineNumberAware) object).setLocation(source.getLocation());
+        }
+        return object;
+    }
+
 }
diff --git a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/SendReifier.java b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/SendReifier.java
index f5a3051..3f6f4b1 100644
--- a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/SendReifier.java
+++ b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/SendReifier.java
@@ -18,6 +18,7 @@ package org.apache.camel.reifier;
 
 import org.apache.camel.Endpoint;
 import org.apache.camel.ExchangePattern;
+import org.apache.camel.LineNumberAware;
 import org.apache.camel.Processor;
 import org.apache.camel.Route;
 import org.apache.camel.model.ProcessorDefinition;
@@ -38,15 +39,18 @@ public class SendReifier extends ProcessorReifier<SendDefinition<?>> {
     }
 
     public Endpoint resolveEndpoint() {
+        Endpoint answer;
         if (definition.getEndpoint() == null) {
             if (definition.getEndpointProducerBuilder() == null) {
-                return CamelContextHelper.resolveEndpoint(camelContext, definition.getEndpointUri(), null);
+                answer = CamelContextHelper.resolveEndpoint(camelContext, definition.getEndpointUri(), null);
             } else {
-                return definition.getEndpointProducerBuilder().resolve(camelContext);
+                answer = definition.getEndpointProducerBuilder().resolve(camelContext);
             }
         } else {
-            return definition.getEndpoint();
+            answer = definition.getEndpoint();
         }
+        LineNumberAware.trySetLineNumberAware(answer, definition);
+        return answer;
     }
 
 }
diff --git a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/WireTapReifier.java b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/WireTapReifier.java
index c43c592..4c8bc70 100644
--- a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/WireTapReifier.java
+++ b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/WireTapReifier.java
@@ -23,6 +23,7 @@ import org.apache.camel.Endpoint;
 import org.apache.camel.ExchangePattern;
 import org.apache.camel.Expression;
 import org.apache.camel.ExtendedCamelContext;
+import org.apache.camel.LineNumberAware;
 import org.apache.camel.Processor;
 import org.apache.camel.Route;
 import org.apache.camel.model.ProcessorDefinition;
@@ -77,6 +78,7 @@ public class WireTapReifier extends ToDynamicReifier<WireTapDefinition<?>> {
         } else {
             // static so we can use a plain send processor
             Endpoint endpoint = CamelContextHelper.resolveEndpoint(camelContext, uri, null);
+            LineNumberAware.trySetLineNumberAware(endpoint, definition);
             sendProcessor = new SendProcessor(endpoint);
         }