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/02/16 11:13:00 UTC

[camel] branch main updated: CAMEL-17656: camel-main - Add start and stop logging with app name and version

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 cdbb31b  CAMEL-17656: camel-main - Add start and stop logging with app name and version
cdbb31b is described below

commit cdbb31b75f7209618014eafb0bcfc25100279528
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed Feb 16 12:10:09 2022 +0100

    CAMEL-17656: camel-main - Add start and stop logging with app name and version
---
 .../camel/impl/engine/AbstractCamelContext.java    |  5 +-
 .../org/apache/camel/main/BaseMainSupport.java     |  2 +-
 .../java/org/apache/camel/main/MainHelper.java     | 58 ++++++++++++++++++++++
 .../java/org/apache/camel/main/MainSupport.java    | 20 ++++++++
 .../apache/camel/dsl/jbang/core/commands/Run.java  |  2 +-
 5 files changed, 82 insertions(+), 5 deletions(-)

diff --git a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
index 1e52da4..6524e76 100644
--- a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
+++ b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
@@ -2980,9 +2980,8 @@ public abstract class AbstractCamelContext extends BaseService
     }
 
     protected void doStartContext() throws Exception {
-        if (LOG.isDebugEnabled()) {
-            LOG.debug("Apache Camel {} ({}) is starting", getVersion(), getName());
-        }
+        LOG.info("Apache Camel {} ({}) is starting", getVersion(), getName());
+
         vetoed = null;
         startDate = System.currentTimeMillis();
         stopWatch.restart();
diff --git a/core/camel-main/src/main/java/org/apache/camel/main/BaseMainSupport.java b/core/camel-main/src/main/java/org/apache/camel/main/BaseMainSupport.java
index 7755478..48822f1 100644
--- a/core/camel-main/src/main/java/org/apache/camel/main/BaseMainSupport.java
+++ b/core/camel-main/src/main/java/org/apache/camel/main/BaseMainSupport.java
@@ -96,7 +96,7 @@ public abstract class BaseMainSupport extends BaseService {
     protected Properties initialProperties;
     protected Properties overrideProperties;
     protected boolean standalone = true;
-    private final MainHelper helper;
+    protected final MainHelper helper;
 
     protected BaseMainSupport() {
         helper = new MainHelper();
diff --git a/core/camel-main/src/main/java/org/apache/camel/main/MainHelper.java b/core/camel-main/src/main/java/org/apache/camel/main/MainHelper.java
index 0e115de..05b100d 100644
--- a/core/camel-main/src/main/java/org/apache/camel/main/MainHelper.java
+++ b/core/camel-main/src/main/java/org/apache/camel/main/MainHelper.java
@@ -43,17 +43,21 @@ import org.apache.camel.util.IOHelper;
 import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.OrderedProperties;
 import org.apache.camel.util.StringHelper;
+import org.apache.camel.util.TimeUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public final class MainHelper {
     private static final Logger LOG = LoggerFactory.getLogger(MainHelper.class);
 
+    private final String version;
+    private final long startDate;
     private final Set<String> componentEnvNames = new HashSet<>();
     private final Set<String> dataformatEnvNames = new HashSet<>();
     private final Set<String> languageEnvNames = new HashSet<>();
 
     public MainHelper() {
+        startDate = System.currentTimeMillis();
         try {
             InputStream is = MainHelper.class.getResourceAsStream("/org/apache/camel/main/components.properties");
             loadLines(is, componentEnvNames, s -> "CAMEL_COMPONENT_" + s.toUpperCase(Locale.US).replace('-', '_'));
@@ -69,6 +73,20 @@ public final class MainHelper {
         } catch (Exception e) {
             throw new RuntimeException("Error loading catalog information from classpath", e);
         }
+
+        version = doGetVersion();
+    }
+
+    public String getVersion() {
+        return version;
+    }
+
+    public String getUptime() {
+        long delta = System.currentTimeMillis() - startDate;
+        if (delta == 0) {
+            return "";
+        }
+        return TimeUtils.printDuration(delta);
     }
 
     public void bootstrapDone() {
@@ -429,4 +447,44 @@ public final class MainHelper {
         }
     }
 
+    private String doGetVersion() {
+        String version = null;
+
+        InputStream is = null;
+        // try to load from maven properties first
+        try {
+            Properties p = new Properties();
+            is = MainHelper.class
+                    .getResourceAsStream("/META-INF/maven/org.apache.camel/camel-main/pom.properties");
+            if (is != null) {
+                p.load(is);
+                version = p.getProperty("version", "");
+            }
+        } catch (Exception e) {
+            // ignore
+        } finally {
+            if (is != null) {
+                IOHelper.close(is);
+            }
+        }
+
+        // fallback to using Java API
+        if (version == null) {
+            Package aPackage = getClass().getPackage();
+            if (aPackage != null) {
+                version = aPackage.getImplementationVersion();
+                if (version == null) {
+                    version = aPackage.getSpecificationVersion();
+                }
+            }
+        }
+
+        if (version == null) {
+            // we could not compute the version so use a blank
+            version = "";
+        }
+
+        return version;
+    }
+
 }
diff --git a/core/camel-main/src/main/java/org/apache/camel/main/MainSupport.java b/core/camel-main/src/main/java/org/apache/camel/main/MainSupport.java
index 85e24b8..51fa028 100644
--- a/core/camel-main/src/main/java/org/apache/camel/main/MainSupport.java
+++ b/core/camel-main/src/main/java/org/apache/camel/main/MainSupport.java
@@ -43,6 +43,7 @@ public abstract class MainSupport extends BaseMainSupport {
 
     protected volatile ProducerTemplate camelTemplate;
 
+    private String appName = "Apache Camel (Main)";
     private int durationMaxIdleSeconds;
     private int durationMaxMessages;
     private long durationMaxSeconds;
@@ -61,6 +62,13 @@ public abstract class MainSupport extends BaseMainSupport {
         this.shutdownStrategy = new DefaultMainShutdownStrategy(this);
     }
 
+    @Override
+    protected void doInit() throws Exception {
+        // we want this logging to be as early as possible
+        LOG.info("{} {} is starting", appName, helper.getVersion());
+        super.doInit();
+    }
+
     /**
      * Runs this process with the given arguments, and will wait until completed, or the JVM terminates.
      */
@@ -82,6 +90,7 @@ public abstract class MainSupport extends BaseMainSupport {
                 // while running then just log errors
                 LOG.error("Failed: {}", e, e);
             }
+            LOG.info("{} {} shutdown (uptime:{})", appName, helper.getVersion(), helper.getUptime());
         }
     }
 
@@ -274,6 +283,17 @@ public abstract class MainSupport extends BaseMainSupport {
         this.shutdownStrategy = shutdownStrategy;
     }
 
+    public String getAppName() {
+        return appName;
+    }
+
+    /**
+     * Application name (used for logging start and stop)
+     */
+    public void setAppName(String appName) {
+        this.appName = appName;
+    }
+
     @Override
     protected void doStop() throws Exception {
         // call completed to properly stop as we count down the waiting latch
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
index 6653527..6bb4837 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
@@ -150,6 +150,7 @@ class Run implements Callable<Integer> {
         } else {
             main = new KameletMain("file://" + localKameletDir);
         }
+        main.setAppName("Apache Camel (JBang)");
 
         main.addInitialProperty("camel.main.name", name);
         // shutdown quickly
@@ -308,7 +309,6 @@ class Run implements Callable<Integer> {
             main.addInitialProperty("camel.component.properties.location", loc);
         }
 
-        System.out.println("Starting CamelJBang");
         main.start();
 
         context = main.getCamelContext();