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/09/23 11:39:41 UTC
[camel] 03/03: camel-jbang - Logger command
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
commit c2d9862eb5596dcdcf90fdf2a320286b68a1617e
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Fri Sep 23 13:39:11 2022 +0200
camel-jbang - Logger command
---
.../modules/ROOT/pages/camel-jbang.adoc | 22 +++++++
.../camel/cli/connector/LocalCliConnector.java | 15 +++++
.../apache/camel/cli/connector/LoggerHelper.java | 73 ++++++++++++++++++++++
.../jbang/core/commands/action/LoggerAction.java | 43 ++++++++++++-
4 files changed, 152 insertions(+), 1 deletion(-)
diff --git a/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc b/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc
index 434832300c0..cd4dc1e8358 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc
@@ -713,6 +713,28 @@ camel cmd start-route --all
TIP: You can stop one or more route by their ids by separating using
comma such as: camel cmd start-route --id=route1,hello. Use `camel cmd start-route --help` for more details.
+==== Configuring logging levels
+
+You can see the current logging levels of the running Camel integrations by:
+
+[source,bash]
+----
+camel cmd logger
+ PID NAME AGE LOGGER LEVEL
+ 90857 bar 2m48s root INFO
+ 91103 foo 20s root INFO
+----
+
+The logging level can be change at runtime, for example to change foo to DEBUG you execute:
+
+[source,bash]
+----
+camel cmd logger --level=DEBUG foo
+----
+
+TIP: You can use `--all` to change logging levels for all running integrations.
+
+
=== Using Jolokia and Hawtio
The https://hawt.io/[Hawtio] web console allows inspecting running Camel integrations, such
diff --git a/dsl/camel-cli-connector/src/main/java/org/apache/camel/cli/connector/LocalCliConnector.java b/dsl/camel-cli-connector/src/main/java/org/apache/camel/cli/connector/LocalCliConnector.java
index e2a53c61202..c0649d9aa33 100644
--- a/dsl/camel-cli-connector/src/main/java/org/apache/camel/cli/connector/LocalCliConnector.java
+++ b/dsl/camel-cli-connector/src/main/java/org/apache/camel/cli/connector/LocalCliConnector.java
@@ -230,6 +230,17 @@ public class LocalCliConnector extends ServiceSupport implements CliConnector, C
// ignore
}
}
+ } else if ("logger".equals(action)) {
+ try {
+ String command = root.getString("command");
+ if ("set-logging-level".equals(command)) {
+ String logger = root.getString("logger-name");
+ String level = root.getString("logging-level");
+ LoggerHelper.changeLoggingLevel(logger, level);
+ }
+ } catch (Exception e) {
+ // ignore
+ }
} else if ("gc".equals(action)) {
System.gc();
} else if ("reload".equals(action)) {
@@ -274,6 +285,10 @@ public class LocalCliConnector extends ServiceSupport implements CliConnector, C
}
}
+ private void changeLoggingLevel(String level) {
+
+ }
+
JsonObject loadAction() {
try {
if (actionFile != null && actionFile.exists()) {
diff --git a/dsl/camel-cli-connector/src/main/java/org/apache/camel/cli/connector/LoggerHelper.java b/dsl/camel-cli-connector/src/main/java/org/apache/camel/cli/connector/LoggerHelper.java
new file mode 100644
index 00000000000..82e899238e3
--- /dev/null
+++ b/dsl/camel-cli-connector/src/main/java/org/apache/camel/cli/connector/LoggerHelper.java
@@ -0,0 +1,73 @@
+/*
+ * 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.cli.connector;
+
+import java.lang.management.ManagementFactory;
+import java.util.Set;
+
+import javax.management.Attribute;
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+
+/**
+ * Helper for logger action.
+ *
+ * Currently, only log4j is supported.
+ */
+public final class LoggerHelper {
+
+ // log4j support
+ private static final String LOG4J_MBEAN = "org.apache.logging.log4j2";
+
+ private LoggerHelper() {
+ }
+
+ /**
+ * Change logging level in the logging system.
+ *
+ * Currently, only log4j is supported.
+ *
+ * @param logger the logger name, null is assumed to be root
+ * @param level the new logging level
+ */
+ public static void changeLoggingLevel(String logger, String level) {
+ if (logger == null || logger.isEmpty()) {
+ logger = "root";
+ }
+
+ try {
+ MBeanServer ms = ManagementFactory.getPlatformMBeanServer();
+ if (ms != null) {
+ Set<ObjectName> set = ms.queryNames(new ObjectName(LOG4J_MBEAN + ":type=*,component=Loggers,name=*"), null);
+ for (ObjectName on : set) {
+ if (ms.isRegistered(on)) {
+ String name = (String) ms.getAttribute(on, "Name");
+ if (name == null || name.isEmpty()) {
+ name = "root";
+ }
+ if (logger.equals(name)) {
+ ms.setAttribute(on, new Attribute("Level", level));
+ }
+ }
+ }
+ }
+ } catch (Exception e) {
+ // ignore
+ }
+ }
+
+}
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/LoggerAction.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/LoggerAction.java
index ac43d0511d8..98c65826b2e 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/LoggerAction.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/LoggerAction.java
@@ -16,6 +16,7 @@
*/
package org.apache.camel.dsl.jbang.core.commands.action;
+import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@@ -26,6 +27,7 @@ import com.github.freva.asciitable.Column;
import com.github.freva.asciitable.HorizontalAlign;
import com.github.freva.asciitable.OverflowBehaviour;
import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain;
+import org.apache.camel.util.IOHelper;
import org.apache.camel.util.TimeUtils;
import org.apache.camel.util.json.JsonObject;
import picocli.CommandLine;
@@ -38,13 +40,52 @@ public class LoggerAction extends ActionBaseCommand {
description = "Sort by pid, name or age", defaultValue = "pid")
String sort;
+ @CommandLine.Parameters(description = "Name or pid of running Camel integration", arity = "0..1")
+ String name;
+
+ @CommandLine.Option(names = { "--all" },
+ description = "To select all running Camel integrations")
+ boolean all;
+
+ @CommandLine.Option(names = { "--level", "--logging-level" },
+ description = "To change logging level")
+ String loggingLevel;
+
+ @CommandLine.Option(names = { "--logger" },
+ description = "The logger name", defaultValue = "root")
+ String logger;
+
public LoggerAction(CamelJBangMain main) {
super(main);
}
@Override
public Integer call() throws Exception {
- return callList();
+ if (loggingLevel == null) {
+ return callList();
+ }
+ if (!all && name == null) {
+ return 0;
+ } else if (all) {
+ name = "*";
+ }
+ return callChangeLoggingLevel();
+ }
+
+ protected Integer callChangeLoggingLevel() throws Exception {
+ List<Long> pids = findPids(name);
+
+ for (long pid : pids) {
+ JsonObject root = new JsonObject();
+ root.put("action", "logger");
+ File f = getActionFile("" + pid);
+ root.put("command", "set-logging-level");
+ root.put("logger-name", logger);
+ root.put("logging-level", loggingLevel);
+ IOHelper.writeText(root.toJson(), f);
+ }
+
+ return 0;
}
protected Integer callList() throws Exception {