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 2dded30d9d3 CAMEL-18067: camel-jbang - Add option to use json logging 
when running.
2dded30d9d3 is described below

commit 2dded30d9d3cd6414f5edba3c21b7cfee64cec87
Author: Claus Ibsen <[email protected]>
AuthorDate: Mon May 9 10:23:24 2022 +0200

    CAMEL-18067: camel-jbang - Add option to use json logging when running.
---
 dsl/camel-jbang/camel-jbang-core/pom.xml           |  5 +++++
 .../dsl/jbang/core/commands/AbstractSearch.java    |  2 +-
 .../apache/camel/dsl/jbang/core/commands/Run.java  | 12 ++++++++---
 .../camel/dsl/jbang/core/common/RuntimeUtil.java   |  6 ++++--
 .../src/main/resources/log4j2-json.properties      | 25 ++++++++++++++++++++++
 5 files changed, 44 insertions(+), 6 deletions(-)

diff --git a/dsl/camel-jbang/camel-jbang-core/pom.xml 
b/dsl/camel-jbang/camel-jbang-core/pom.xml
index be76c83cdf0..f86f0a18b62 100644
--- a/dsl/camel-jbang/camel-jbang-core/pom.xml
+++ b/dsl/camel-jbang/camel-jbang-core/pom.xml
@@ -81,6 +81,11 @@
             <groupId>org.apache.logging.log4j</groupId>
             <artifactId>log4j-core</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.apache.logging.log4j</groupId>
+            <artifactId>log4j-layout-template-json</artifactId>
+            <version>${log4j2-version}</version>
+        </dependency>
         <dependency>
             <groupId>org.apache.logging.log4j</groupId>
             <artifactId>log4j-slf4j-impl</artifactId>
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 2ea0cb9272a..642b3c0d5f2 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", true);
+        RuntimeUtil.configureLog("off", true, false);
 
         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 15de0f5f23e..9d50170d070 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
@@ -108,6 +108,9 @@ class Run implements Callable<Integer> {
     @Option(names = { "--logging-color" }, defaultValue = "true", description 
= "Use colored loggging")
     private boolean loggingColor = true;
 
+    @Option(names = { "--logging-json" }, description = "Use JSON logging (ECS 
Layout)")
+    private boolean loggingJson;
+
     @Option(names = { "--stop" }, description = "Stop all running instances of 
Camel JBang")
     private boolean stopRequested;
 
@@ -269,6 +272,8 @@ class Run implements Callable<Integer> {
                 loggingLevel = 
applicationProperties.getProperty("loggingLevel", loggingLevel);
                 loggingColor
                         = 
"true".equals(applicationProperties.getProperty("loggingColor", loggingColor ? 
"true" : "false"));
+                loggingJson
+                        = 
"true".equals(applicationProperties.getProperty("loggingJson", loggingJson ? 
"true" : "false"));
             } else if (!silentRun && !source.exists()) {
                 System.out.println("Cannot run because application.properties 
file does not exist");
                 return 1;
@@ -280,13 +285,14 @@ class Run implements Callable<Integer> {
 
         // configure logging first
         if (silentRun) {
-            RuntimeUtil.configureLog("off", false);
+            RuntimeUtil.configureLog("off", false, false);
         } else if (logging) {
-            RuntimeUtil.configureLog(loggingLevel, loggingColor);
+            RuntimeUtil.configureLog(loggingLevel, loggingColor, loggingJson);
             writeSettings("loggingLevel", loggingLevel);
             writeSettings("loggingColor", loggingColor ? "true" : "false");
+            writeSettings("loggingJson", loggingJson ? "true" : "false");
         } else {
-            RuntimeUtil.configureLog("off", false);
+            RuntimeUtil.configureLog("off", false, 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 40347204345..5b6a956ae67 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
@@ -29,9 +29,11 @@ public final class RuntimeUtil {
     private RuntimeUtil() {
     }
 
-    public static void configureLog(String level, boolean color) {
+    public static void configureLog(String level, boolean color, boolean json) 
{
         if (INIT_DONE.compareAndSet(false, true)) {
-            if (color) {
+            if (json) {
+                Configurator.initialize("CamelJBang", 
"log4j2-json.properties");
+            } else if (color) {
                 Configurator.initialize("CamelJBang", "log4j2.properties");
             } else {
                 Configurator.initialize("CamelJBang", 
"log4j2-no-color.properties");
diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/main/resources/log4j2-json.properties 
b/dsl/camel-jbang/camel-jbang-core/src/main/resources/log4j2-json.properties
new file mode 100644
index 00000000000..2b7ffbb7064
--- /dev/null
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/resources/log4j2-json.properties
@@ -0,0 +1,25 @@
+## ---------------------------------------------------------------------------
+## 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.json.type = JsonTemplateLayout
+appender.stdout.json.eventTemplateUri = classpath:EcsLayout.json
+
+rootLogger.level = INFO
+rootLogger.appenderRef.out.ref = out

Reply via email to