You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by "majialoong (via GitHub)" <gi...@apache.org> on 2026/07/07 16:35:44 UTC

[PR] MINOR: Move Log4jControllerRegistration to server module [kafka]

majialoong opened a new pull request, #22779:
URL: https://github.com/apache/kafka/pull/22779

   Move `Log4jControllerRegistration` from core to the server module.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MINOR: Move Log4jControllerRegistration to server module [kafka]

Posted by "mimaison (via GitHub)" <gi...@apache.org>.
mimaison commented on code in PR #22779:
URL: https://github.com/apache/kafka/pull/22779#discussion_r3545453126


##########
server/src/main/java/org/apache/kafka/server/logger/Log4jControllerRegistration.java:
##########
@@ -0,0 +1,51 @@
+/*
+ * 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.kafka.server.logger;
+
+import org.apache.kafka.common.utils.Utils;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+
+/**
+ * Registers the {@link LoggingController} MBean, which lets users dynamically alter log4j levels
+ * at runtime. The MBean is registered only once, on the first call to {@link #register()}.
+ */
+public final class Log4jControllerRegistration {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(Log4jControllerRegistration.class);
+
+    private static final AtomicBoolean REGISTERED = new AtomicBoolean(false);
+
+    private static final String MBEAN_TYPE = "kafka.Log4jController";
+    private static final String MBEAN_NAME = "kafka:type=" + MBEAN_TYPE;
+
+    private Log4jControllerRegistration() {
+    }
+
+    public static void register() {
+        if (REGISTERED.compareAndSet(false, true)) {
+            if (Utils.registerMBean(new LoggingController(), MBEAN_NAME)) {
+                LOGGER.info("Registered `{}` MBean", MBEAN_NAME);
+            } else {
+                LOGGER.warn("Couldn't register `{}` MBean", MBEAN_NAME);

Review Comment:
   I wonder if we actually need this branch. When `registerMBean()` returns `false` it already prints an error message with a similar message. It seems the previous catch clause in the Scala code was unreachable as `registerMBean()` can't throw.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MINOR: Move Log4jControllerRegistration to server module [kafka]

Posted by "mimaison (via GitHub)" <gi...@apache.org>.
mimaison merged PR #22779:
URL: https://github.com/apache/kafka/pull/22779


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MINOR: Move Log4jControllerRegistration to server module [kafka]

Posted by "majialoong (via GitHub)" <gi...@apache.org>.
majialoong commented on code in PR #22779:
URL: https://github.com/apache/kafka/pull/22779#discussion_r3586757051


##########
core/src/main/scala/kafka/utils/Logging.scala:
##########
@@ -48,7 +31,7 @@ trait Logging {
 
   protected var logIdent: String = _
 
-  Log4jControllerRegistration
+  Log4jControllerRegistration.register()

Review Comment:
   Agreed. `KafkaRaftServer.startup()` is just a thought for a follow-up. 
   
   I wanted to keep this PR focused and didn't plan to move it now, so keeping it here works perfectly.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MINOR: Move Log4jControllerRegistration to server module [kafka]

Posted by "majialoong (via GitHub)" <gi...@apache.org>.
majialoong commented on code in PR #22779:
URL: https://github.com/apache/kafka/pull/22779#discussion_r3545690563


##########
server/src/main/java/org/apache/kafka/server/logger/Log4jControllerRegistration.java:
##########
@@ -0,0 +1,51 @@
+/*
+ * 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.kafka.server.logger;
+
+import org.apache.kafka.common.utils.Utils;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+
+/**
+ * Registers the {@link LoggingController} MBean, which lets users dynamically alter log4j levels
+ * at runtime. The MBean is registered only once, on the first call to {@link #register()}.
+ */
+public final class Log4jControllerRegistration {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(Log4jControllerRegistration.class);
+
+    private static final AtomicBoolean REGISTERED = new AtomicBoolean(false);
+
+    private static final String MBEAN_TYPE = "kafka.Log4jController";
+    private static final String MBEAN_NAME = "kafka:type=" + MBEAN_TYPE;
+
+    private Log4jControllerRegistration() {
+    }
+
+    public static void register() {
+        if (REGISTERED.compareAndSet(false, true)) {
+            if (Utils.registerMBean(new LoggingController(), MBEAN_NAME)) {
+                LOGGER.info("Registered `{}` MBean", MBEAN_NAME);
+            } else {
+                LOGGER.warn("Couldn't register `{}` MBean", MBEAN_NAME);

Review Comment:
   Thanks for the review. 
   
   Good point. `Utils.registerMBean` already catches registration failures and logs them internally, so the previous Scala catch block was effectively unreachable. I agree that the extra warning is redundant, so I'll remove this branch.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MINOR: Move Log4jControllerRegistration to server module [kafka]

Posted by "majialoong (via GitHub)" <gi...@apache.org>.
majialoong commented on code in PR #22779:
URL: https://github.com/apache/kafka/pull/22779#discussion_r3560049705


##########
core/src/main/scala/kafka/utils/Logging.scala:
##########
@@ -48,7 +31,7 @@ trait Logging {
 
   protected var logIdent: String = _
 
-  Log4jControllerRegistration
+  Log4jControllerRegistration.register()

Review Comment:
   Perhaps `KafkaRaftServer.startup()` would be a better place, since the registration is server wide rather than tied to `Logging`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MINOR: Move Log4jControllerRegistration to server module [kafka]

Posted by "chia7712 (via GitHub)" <gi...@apache.org>.
chia7712 commented on code in PR #22779:
URL: https://github.com/apache/kafka/pull/22779#discussion_r3554054956


##########
core/src/main/scala/kafka/utils/Logging.scala:
##########
@@ -48,7 +31,7 @@ trait Logging {
 
   protected var logIdent: String = _
 
-  Log4jControllerRegistration
+  Log4jControllerRegistration.register()

Review Comment:
   I'm wondering whether this is the best class to call `Log4jControllerRegistration.register()`?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MINOR: Move Log4jControllerRegistration to server module [kafka]

Posted by "mimaison (via GitHub)" <gi...@apache.org>.
mimaison commented on code in PR #22779:
URL: https://github.com/apache/kafka/pull/22779#discussion_r3586382774


##########
core/src/main/scala/kafka/utils/Logging.scala:
##########
@@ -48,7 +31,7 @@ trait Logging {
 
   protected var logIdent: String = _
 
-  Log4jControllerRegistration
+  Log4jControllerRegistration.register()

Review Comment:
   I wonder if it's better to keep here, at least for now. We still have a few Scala classes using `Logging`, like `StorageTool`, that need this call.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org