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/05/06 05:20:03 UTC

[camel] branch main updated: CAMEL-18060: camel-jbang - Add option to turn off logging colors

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 df51ef8815b CAMEL-18060: camel-jbang - Add option to turn off logging colors
df51ef8815b is described below

commit df51ef8815baa07cfc41ab982c3f18f789bb3bd8
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Fri May 6 07:19:16 2022 +0200

    CAMEL-18060: camel-jbang - Add option to turn off logging colors
---
 .../dsl/jbang/core/commands/AbstractSearch.java    |  2 +-
 .../apache/camel/dsl/jbang/core/commands/Run.java  | 18 +++++++++------
 .../camel/dsl/jbang/core/common/RuntimeUtil.java   | 16 +++++++++----
 .../src/main/resources/log4j2-no-color.properties  | 27 ++++++++++++++++++++++
 4 files changed, 51 insertions(+), 12 deletions(-)

diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/AbstractSearch.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/AbstractSearch.java
index 2d26114d952..2ea0cb9272a 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/AbstractSearch.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/AbstractSearch.java
@@ -55,7 +55,7 @@ public abstract class AbstractSearch {
 
     protected void downloadResource(File indexFile) throws ResourceDoesNotExist, IOException {
         // turn off logging as we use camel to download
-        RuntimeUtil.configureLog("off");
+        RuntimeUtil.configureLog("off", true);
 
         KameletMain main = new KameletMain();
         main.start();
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 dc828bdb73f..1757c3145c8 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
@@ -94,12 +94,15 @@ class Run implements Callable<Integer> {
     @Option(names = { "--name" }, defaultValue = "CamelJBang", description = "The name of the Camel application")
     private String name;
 
-    @Option(names = { "--logging" }, description = "Can be used to turn off logging")
+    @Option(names = { "--logging" }, defaultValue = "true", description = "Can be used to turn off logging")
     private boolean logging = true;
 
     @Option(names = { "--logging-level" }, defaultValue = "info", description = "Logging level")
     private String loggingLevel;
 
+    @Option(names = { "--logging-color" }, defaultValue = "true", description = "Use colored loggging")
+    private boolean loggingColor = true;
+
     @Option(names = { "--stop" }, description = "Stop all running instances of Camel JBang")
     private boolean stopRequested;
 
@@ -151,7 +154,7 @@ class Run implements Callable<Integer> {
     @Option(names = { "--health" }, description = "Health check at /q/health on local HTTP server (port 8080 by default)")
     private boolean health;
 
-    @Option(names = { "--modeline" }, description = "Enables Camel-K style modeline")
+    @Option(names = { "--modeline" }, defaultValue = "true", description = "Enables Camel-K style modeline")
     private boolean modeline = true;
 
     @Option(names = { "--open-api" }, description = "Add an OpenAPI spec from the given file")
@@ -234,7 +237,6 @@ class Run implements Callable<Integer> {
     private static String prefixFile(String line, String key) {
         String value = StringHelper.after(line, key + "=");
         if (value != null) {
-
             value = value.replaceAll("file:", "classpath:");
             line = key + "=" + value;
         }
@@ -257,8 +259,9 @@ class Run implements Callable<Integer> {
             File source = new File("application.properties");
             if (source.exists()) {
                 applicationProperties = loadApplicationProperties(source);
-                // logging level may be configured in the properties file
+                // logging level/color may be configured in the properties file
                 loggingLevel = applicationProperties.getProperty("loggingLevel", loggingLevel);
+                loggingColor = "true".equals(applicationProperties.getProperty("loggingColor", loggingColor ? "true" : "false"));
             } else if (!silentRun && !source.exists()) {
                 System.out.println("Cannot run because application.properties file does not exist");
                 return 1;
@@ -270,12 +273,13 @@ class Run implements Callable<Integer> {
 
         // configure logging first
         if (silentRun) {
-            RuntimeUtil.configureLog("off");
+            RuntimeUtil.configureLog("off", false);
         } else if (logging) {
-            RuntimeUtil.configureLog(loggingLevel);
+            RuntimeUtil.configureLog(loggingLevel, loggingColor);
             writeSettings("loggingLevel", loggingLevel);
+            writeSettings("loggingColor", loggingColor ? "true" : "false");
         } else {
-            RuntimeUtil.configureLog("off");
+            RuntimeUtil.configureLog("off", false);
             writeSettings("loggingLevel", "off");
         }
 
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/RuntimeUtil.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/RuntimeUtil.java
index 27c04604d64..355d17ccec2 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/RuntimeUtil.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/RuntimeUtil.java
@@ -20,16 +20,24 @@ import org.apache.logging.log4j.Level;
 import org.apache.logging.log4j.core.config.Configurator;
 import org.slf4j.LoggerFactory;
 
+import java.util.concurrent.atomic.AtomicBoolean;
+
 public final class RuntimeUtil {
 
-    static {
-        Configurator.initialize("CamelJBang", "log4j2.properties");
-    }
+    private static final AtomicBoolean initDone = new AtomicBoolean();
 
     private RuntimeUtil() {
     }
 
-    public static void configureLog(String level) {
+    public static void configureLog(String level, boolean color) {
+        if (initDone.compareAndSet(false, true)) {
+            if (color) {
+                Configurator.initialize("CamelJBang", "log4j2.properties");
+            } else {
+                Configurator.initialize("CamelJBang", "log4j2-no-color.properties");
+            }
+        }
+
         level = level.toLowerCase();
 
         switch (level) {
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/resources/log4j2-no-color.properties b/dsl/camel-jbang/camel-jbang-core/src/main/resources/log4j2-no-color.properties
new file mode 100644
index 00000000000..2bb0c0ddc93
--- /dev/null
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/resources/log4j2-no-color.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 (no color)
+appender.stdout.layout.pattern = %d{yyyy-MM-dd HH:mm:ss.SSS} %5p %pid --- [%15.15t] %-40.40c : %m%n
+
+rootLogger.level = INFO
+rootLogger.appenderRef.out.ref = out
+