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 f26e66b  CAMEL-17276: mvn camel:run - Can now log in color with its 
own built-in logging if enabled with logLevel setting.
f26e66b is described below

commit f26e66b4289beb50c177aa180d8bd461a81f0d4b
Author: Claus Ibsen <[email protected]>
AuthorDate: Mon Dec 6 18:53:10 2021 +0100

    CAMEL-17276: mvn camel:run - Can now log in color with its own built-in 
logging if enabled with logLevel setting.
---
 tooling/maven/camel-maven-plugin/pom.xml           | 17 +++++-
 .../src/main/docs/camel-maven-plugin.adoc          | 22 ++++++++
 .../java/org/apache/camel/maven/LoggingUtil.java   | 61 ++++++++++++++++++++++
 .../main/java/org/apache/camel/maven/RunMojo.java  | 54 ++++++++++++++++---
 .../src/main/resources/log4j2.properties           | 27 ++++++++++
 5 files changed, 173 insertions(+), 8 deletions(-)

diff --git a/tooling/maven/camel-maven-plugin/pom.xml 
b/tooling/maven/camel-maven-plugin/pom.xml
index fef5344..6e7ba86 100644
--- a/tooling/maven/camel-maven-plugin/pom.xml
+++ b/tooling/maven/camel-maven-plugin/pom.xml
@@ -91,13 +91,26 @@
             <groupId>org.apache.camel</groupId>
             <artifactId>camel-cdi</artifactId>
         </dependency>
-
+        <!-- for logging in color -->
+        <dependency>
+            <groupId>org.fusesource.jansi</groupId>
+            <artifactId>jansi</artifactId>
+            <version>${jansi-version}</version>
+        </dependency>
         <!-- logging -->
         <dependency>
             <groupId>org.apache.logging.log4j</groupId>
+            <artifactId>log4j-api</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.logging.log4j</groupId>
             <artifactId>log4j-slf4j-impl</artifactId>
-            <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.logging.log4j</groupId>
+            <artifactId>log4j-core</artifactId>
+        </dependency>
+
     </dependencies>
 
     <build>
diff --git 
a/tooling/maven/camel-maven-plugin/src/main/docs/camel-maven-plugin.adoc 
b/tooling/maven/camel-maven-plugin/src/main/docs/camel-maven-plugin.adoc
index d7e134d..4392c8f 100644
--- a/tooling/maven/camel-maven-plugin/src/main/docs/camel-maven-plugin.adoc
+++ b/tooling/maven/camel-maven-plugin/src/main/docs/camel-maven-plugin.adoc
@@ -33,6 +33,7 @@ The maven plugin *run* goal supports the following options 
which can be configur
 | durationIdle | -1 | Sets the idle time duration (seconds) duration that the 
application can be idle before terminating. A value <= 0 will run forever.
 | durationMaxMessages | -1 | Sets the duration of maximum number of messages 
that the application will process before terminating.
 | logClasspath | false | Whether to log the classpath when starting
+| logLevel | OFF | Whether to use built-in console logging (uses log4j), which 
does not require to add any logging dependency to your project. However, the 
logging is fixed to log to the console, with a color style that is similar to 
Spring Boot. You can change the root logging level to: FATAL, ERROR, WARN, 
INFO, DEBUG, TRACE, OFF
 |===
 
 
@@ -70,6 +71,27 @@ You can enable this in the configuration using:
 </plugin>
 ----
 
+=== Using built-in logging
+
+If you want quickly to have logging to console, you can use the built-in 
logging by setting the logging level as shown:
+
+[source,xml]
+----
+<plugin>
+  <groupId>org.apache.camel</groupId>
+  <artifactId>camel-maven-plugin</artifactId>
+  <configuration>
+    <logLevel>INFO</logLevel>
+  </configuration>
+</plugin>
+----
+
+This runs the application with console logging, in color that is similar to 
Spring Boot logging.
+This is default turned off, to use the configured logging system in the 
project.
+
+The idea with the built-in logging is that you sometimes want to avoid messing 
with setting
+up logging, and just want a quick and easy log to console that looks good.
+
 === Using live reload of XML files
 
 You can configure the plugin to scan for XML file changes and trigger a reload 
of the Camel routes which are contained in those XML files.
diff --git 
a/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/LoggingUtil.java
 
b/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/LoggingUtil.java
new file mode 100644
index 0000000..c8b31cc
--- /dev/null
+++ 
b/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/LoggingUtil.java
@@ -0,0 +1,61 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.camel.maven;
+
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.core.config.Configurator;
+import org.slf4j.LoggerFactory;
+
+public final class LoggingUtil {
+
+    static {
+        Configurator.initialize(null, "log4j2.properties");
+    }
+
+    private LoggingUtil() {
+    }
+
+    public static void configureLog(String level) {
+        level = level.toLowerCase();
+
+        switch (level) {
+            case "trace":
+                Configurator.setRootLevel(Level.TRACE);
+                break;
+            case "debug":
+                Configurator.setRootLevel(Level.DEBUG);
+                break;
+            case "info":
+                Configurator.setRootLevel(Level.INFO);
+                break;
+            case "warn":
+                Configurator.setRootLevel(Level.WARN);
+                break;
+            case "error":
+                Configurator.setRootLevel(Level.ERROR);
+                break;
+            case "fatal":
+                Configurator.setRootLevel(Level.FATAL);
+                break;
+            default: {
+                Configurator.setRootLevel(Level.INFO);
+                LoggerFactory.getLogger(LoggingUtil.class).warn("Invalid 
logging level: {}", level);
+            }
+        }
+    }
+
+}
diff --git 
a/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/RunMojo.java
 
b/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/RunMojo.java
index 8dd895b..f4ef4ea 100644
--- 
a/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/RunMojo.java
+++ 
b/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/RunMojo.java
@@ -27,7 +27,6 @@ import java.util.Collection;
 import java.util.Collections;
 import java.util.HashSet;
 import java.util.Iterator;
-import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Properties;
 import java.util.Set;
@@ -113,6 +112,17 @@ public class RunMojo extends AbstractExecMojo {
     protected boolean logClasspath;
 
     /**
+     * Whether to use built-in console logging (uses log4j), which does not 
require to add any logging dependency to
+     * your project.
+     *
+     * However, the logging is fixed to log to the console, with a color style 
that is similar to Spring Boot.
+     *
+     * You can change the root logging level to: FATAL, ERROR, WARN, INFO, 
DEBUG, TRACE, OFF
+     */
+    @Parameter(property = "camel.logLevel", defaultValue = "OFF")
+    protected String logLevel;
+
+    /**
      * Whether to use CDI when running, instead of Spring
      */
     @Parameter(property = "camel.useCDI")
@@ -684,7 +694,7 @@ public class RunMojo extends AbstractExecMojo {
      * @throws MojoExecutionException
      */
     private ClassLoader getClassLoader() throws MojoExecutionException {
-        Set<URL> classpathURLs = new LinkedHashSet<>();
+        List<URL> classpathURLs = new ArrayList<>();
         // project classpath must be first
         this.addRelevantProjectDependenciesToClasspath(classpathURLs);
         // and extra plugin classpath
@@ -692,10 +702,18 @@ public class RunMojo extends AbstractExecMojo {
         // and plugin classpath last
         this.addRelevantPluginDependenciesToClasspath(classpathURLs);
 
+        if (!logLevel.equals("OFF")) {
+            getLog().info("Using built-in logging level: " + logLevel);
+            // and extra plugin classpath
+            this.addConsoleLogDependenciesToClasspath(classpathURLs);
+            // setup logging
+            LoggingUtil.configureLog(logLevel);
+        }
+
         if (logClasspath) {
             getLog().info("Classpath:");
             for (URL url : classpathURLs) {
-                getLog().info("  " + url.getFile().toString());
+                getLog().info("  " + url.getFile());
             }
         }
         return new URLClassLoader(classpathURLs.toArray(new 
URL[classpathURLs.size()]));
@@ -708,7 +726,7 @@ public class RunMojo extends AbstractExecMojo {
      * @param  path                   classpath of {@link java.net.URL} objects
      * @throws MojoExecutionException
      */
-    private void addRelevantPluginDependenciesToClasspath(Set<URL> path) 
throws MojoExecutionException {
+    private void addRelevantPluginDependenciesToClasspath(List<URL> path) 
throws MojoExecutionException {
         if (hasCommandlineArgs()) {
             arguments = parseCommandlineArgs();
         }
@@ -745,7 +763,7 @@ public class RunMojo extends AbstractExecMojo {
      * @param  path                   classpath of {@link java.net.URL} objects
      * @throws MojoExecutionException
      */
-    private void addExtraPluginDependenciesToClasspath(Set<URL> path) throws 
MojoExecutionException {
+    private void addExtraPluginDependenciesToClasspath(List<URL> path) throws 
MojoExecutionException {
         if (extraPluginDependencyArtifactId == null && 
extendedPluginDependencyArtifactId == null) {
             return;
         }
@@ -776,12 +794,36 @@ public class RunMojo extends AbstractExecMojo {
     }
 
     /**
+     * Adds the JARs needed for using the built-in logging to console
+     */
+    private void addConsoleLogDependenciesToClasspath(List<URL> path) throws 
MojoExecutionException {
+        try {
+            Set<Artifact> artifacts = new HashSet<>(this.pluginDependencies);
+            for (Artifact artifact : artifacts) {
+                // add these loggers in the beginning so they are first
+                if (artifact.getArtifactId().equals("jansi")) {
+                    // jansi for logging in color
+                    path.add(0, artifact.getFile().toURI().toURL());
+                } else if 
(artifact.getGroupId().equals("org.apache.logging.log4j")) {
+                    // add log4j as this is needed
+                    path.add(0, artifact.getFile().toURI().toURL());
+                } else if 
(artifact.getArtifactId().equals("camel-maven-plugin")) {
+                    // add ourselves
+                    path.add(0, artifact.getFile().toURI().toURL());
+                }
+            }
+        } catch (MalformedURLException e) {
+            throw new MojoExecutionException("Error during setting up 
classpath", e);
+        }
+    }
+
+    /**
      * Add any relevant project dependencies to the classpath. Takes 
includeProjectDependencies into consideration.
      *
      * @param  path                   classpath of {@link java.net.URL} objects
      * @throws MojoExecutionException
      */
-    private void addRelevantProjectDependenciesToClasspath(Set<URL> path) 
throws MojoExecutionException {
+    private void addRelevantProjectDependenciesToClasspath(List<URL> path) 
throws MojoExecutionException {
         if (this.includeProjectDependencies) {
             try {
                 getLog().debug("Project Dependencies will be included.");
diff --git 
a/tooling/maven/camel-maven-plugin/src/main/resources/log4j2.properties 
b/tooling/maven/camel-maven-plugin/src/main/resources/log4j2.properties
new file mode 100644
index 0000000..cca5fb5
--- /dev/null
+++ b/tooling/maven/camel-maven-plugin/src/main/resources/log4j2.properties
@@ -0,0 +1,27 @@
+## ---------------------------------------------------------------------------
+## Licensed to the Apache Software Foundation (ASF) under one or more
+## contributor license agreements.  See the NOTICE file distributed with
+## this work for additional information regarding copyright ownership.
+## The ASF licenses this file to You 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.
+## ---------------------------------------------------------------------------
+
+appender.stdout.type = Console
+appender.stdout.name = out
+appender.stdout.layout.type = PatternLayout
+
+# logging style that is similar to spring boot
+appender.stdout.layout.pattern = %style{%d{yyyy-MM-dd HH:mm:ss.SSS}}{Dim} 
%highlight{%5p} %style{%pid}{Magenta} %style{---}{Dim} %style{[%15.15t]}{Dim} 
%style{%-40.40c{1.}}{Cyan} : %m%n
+
+rootLogger.level = INFO
+rootLogger.appenderRef.out.ref = out
+

Reply via email to