chia7712 commented on code in PR #19687:
URL: https://github.com/apache/kafka/pull/19687#discussion_r2093853688


##########
server/src/main/java/org/apache/kafka/server/logging/Log4jCoreController.java:
##########
@@ -0,0 +1,96 @@
+/*
+ * 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.logging;
+
+import org.apache.kafka.common.utils.Utils;
+
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.config.Configurator;
+import org.apache.logging.log4j.core.config.LoggerConfig;
+
+import java.util.HashMap;
+import java.util.Locale;
+import java.util.Map;
+
+public class Log4jCoreController extends LoggingControllerDelegate {
+    private final LoggerContext logContext;
+
+    public Log4jCoreController() {
+        this.logContext = (LoggerContext) LogManager.getContext(false);
+    }
+
+    @Override
+    public Map<String, String> loggers() {
+        String rootLoggerLevel = 
logContext.getRootLogger().getLevel().toString();
+
+        Map<String, String> result = new HashMap<>();
+        // Loggers defined in the configuration
+        for (LoggerConfig logger : 
logContext.getConfiguration().getLoggers().values()) {
+            if (!logger.getName().equals(LogManager.ROOT_LOGGER_NAME)) {
+                result.put(logger.getName(), logger.getLevel().toString());
+            }
+        }
+        // Loggers actually running
+        for (Logger logger : logContext.getLoggers()) {
+            if (!logger.getName().equals(LogManager.ROOT_LOGGER_NAME)) {
+                result.put(logger.getName(), logger.getLevel().toString());
+            }
+        }
+        // Add root logger
+        result.put(LoggingController.ROOT_LOGGER, rootLoggerLevel);
+        return result;
+    }
+
+    @Override
+    public boolean logLevel(String loggerName, String logLevel) {
+        if (Utils.isBlank(loggerName) || Utils.isBlank(logLevel))
+            return false;
+
+        Level level = Level.toLevel(logLevel.toUpperCase(Locale.ROOT));
+
+        if (loggerName.equals(LoggingController.ROOT_LOGGER)) {
+            Configurator.setLevel(LogManager.ROOT_LOGGER_NAME, level);
+            return true;
+        } else {
+            if (loggerExists(loggerName) && level != null) {
+                Configurator.setLevel(loggerName, level);
+                return true;
+            } else {
+                return false;
+            }
+        }
+    }
+
+    @Override
+    public boolean unsetLogLevel(String loggerName) {
+        Level nullLevel = null;

Review Comment:
   ```java
           Level nullLevel = null;
           if (loggerName.equals(LoggingController.ROOT_LOGGER)) {
               Configurator.setLevel(LogManager.ROOT_LOGGER_NAME, nullLevel);
               return true;
           }
           if (loggerExists(loggerName)) {
               Configurator.setLevel(loggerName, nullLevel);
               return true;
           }
           return false;
   ```



##########
server/src/main/java/org/apache/kafka/server/logging/Log4jCoreController.java:
##########
@@ -0,0 +1,96 @@
+/*
+ * 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.logging;
+
+import org.apache.kafka.common.utils.Utils;
+
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.config.Configurator;
+import org.apache.logging.log4j.core.config.LoggerConfig;
+
+import java.util.HashMap;
+import java.util.Locale;
+import java.util.Map;
+
+public class Log4jCoreController extends LoggingControllerDelegate {
+    private final LoggerContext logContext;
+
+    public Log4jCoreController() {
+        this.logContext = (LoggerContext) LogManager.getContext(false);
+    }
+
+    @Override
+    public Map<String, String> loggers() {
+        String rootLoggerLevel = 
logContext.getRootLogger().getLevel().toString();
+
+        Map<String, String> result = new HashMap<>();
+        // Loggers defined in the configuration
+        for (LoggerConfig logger : 
logContext.getConfiguration().getLoggers().values()) {
+            if (!logger.getName().equals(LogManager.ROOT_LOGGER_NAME)) {
+                result.put(logger.getName(), logger.getLevel().toString());
+            }
+        }
+        // Loggers actually running
+        for (Logger logger : logContext.getLoggers()) {
+            if (!logger.getName().equals(LogManager.ROOT_LOGGER_NAME)) {
+                result.put(logger.getName(), logger.getLevel().toString());
+            }
+        }
+        // Add root logger
+        result.put(LoggingController.ROOT_LOGGER, rootLoggerLevel);
+        return result;
+    }
+
+    @Override
+    public boolean logLevel(String loggerName, String logLevel) {
+        if (Utils.isBlank(loggerName) || Utils.isBlank(logLevel))
+            return false;
+
+        Level level = Level.toLevel(logLevel.toUpperCase(Locale.ROOT));
+
+        if (loggerName.equals(LoggingController.ROOT_LOGGER)) {

Review Comment:
   ```java
           if (loggerName.equals(LoggingController.ROOT_LOGGER)) {
               Configurator.setLevel(LogManager.ROOT_LOGGER_NAME, level);
               return true;
           }
           if (loggerExists(loggerName) && level != null) {
               Configurator.setLevel(loggerName, level);
               return true;
           }
           return false;
   ```



##########
server/src/main/java/org/apache/kafka/server/logging/Log4jCoreController.java:
##########
@@ -0,0 +1,96 @@
+/*
+ * 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.logging;
+
+import org.apache.kafka.common.utils.Utils;
+
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.config.Configurator;
+import org.apache.logging.log4j.core.config.LoggerConfig;
+
+import java.util.HashMap;
+import java.util.Locale;
+import java.util.Map;
+
+public class Log4jCoreController extends LoggingControllerDelegate {

Review Comment:
   Could you please make it package-private?



##########
server/src/main/java/org/apache/kafka/server/logging/NoOpController.java:
##########
@@ -0,0 +1,38 @@
+/*
+ * 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.logging;
+
+import java.util.Collections;
+import java.util.Map;
+
+public class NoOpController extends LoggingControllerDelegate {
+
+    @Override
+    public Map<String, String> loggers() {
+        return Collections.emptyMap();

Review Comment:
   `Map.of`



##########
server/src/main/java/org/apache/kafka/server/logging/NoOpController.java:
##########
@@ -0,0 +1,38 @@
+/*
+ * 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.logging;
+
+import java.util.Collections;
+import java.util.Map;
+
+public class NoOpController extends LoggingControllerDelegate {

Review Comment:
   Could you please make it package-private?



##########
server/src/main/java/org/apache/kafka/server/logging/NoOpController.java:
##########
@@ -0,0 +1,38 @@
+/*
+ * 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.logging;

Review Comment:
   sorry for my previous comment. Given that we have `kafka.server.logger` 
already, could you please use `org.apache.kafka.server.logger` instead?



##########
server/src/main/java/org/apache/kafka/server/logging/LoggingControllerDelegate.java:
##########
@@ -0,0 +1,28 @@
+/*
+ * 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.logging;
+
+import java.util.Map;
+
+public abstract class LoggingControllerDelegate {

Review Comment:
   this can be interface, right?



-- 
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-unsubscr...@kafka.apache.org

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

Reply via email to