You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by ro...@apache.org on 2022/06/28 12:51:25 UTC

[activemq-artemis] 02/02: Use the given category name and supply the appropriate logger to be used

This is an automated email from the ASF dual-hosted git repository.

robbie pushed a commit to branch new-logging
in repository https://gitbox.apache.org/repos/asf/activemq-artemis.git

commit 7e6c0d2a3e5767a21e9fcee14bd336eced22a50d
Author: Robbie Gemmell <ro...@apache.org>
AuthorDate: Tue Jun 28 13:50:11 2022 +0100

    Use the given category name and supply the appropriate logger to be used
---
 .../activemq/artemis/logprocessor/CodeFactory.java | 44 ++++++++++++++--------
 .../artemis/logprocessor/LogProcessor.java         | 10 -----
 2 files changed, 29 insertions(+), 25 deletions(-)

diff --git a/artemis-log-facade/src/main/java/org/apache/activemq/artemis/logprocessor/CodeFactory.java b/artemis-log-facade/src/main/java/org/apache/activemq/artemis/logprocessor/CodeFactory.java
index 16c368dee6..a9562634e5 100644
--- a/artemis-log-facade/src/main/java/org/apache/activemq/artemis/logprocessor/CodeFactory.java
+++ b/artemis-log-facade/src/main/java/org/apache/activemq/artemis/logprocessor/CodeFactory.java
@@ -17,13 +17,18 @@
 
 package org.apache.activemq.artemis.logprocessor;
 
-import java.lang.reflect.Field;
+import java.lang.reflect.Constructor;
 import java.security.PrivilegedAction;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
 import static java.security.AccessController.doPrivileged;
 
 public class CodeFactory {
 
+   private static final Logger logger = LoggerFactory.getLogger(CodeFactory.class);
+
    public static <T> T getCodeClass(final Class<T> type) {
       return getCodeClass(type, type.getName());
    }
@@ -32,21 +37,30 @@ public class CodeFactory {
       return doPrivileged(new PrivilegedAction<T>() {
          @Override
          public T run() {
+            final String implClassName = type.getName() + "_impl";
+
+            logger.trace("Loading [{}]", implClassName);
+
+            final Class<? extends T> implClass;
             try {
-               String className = type.getName() + "_impl";
-               System.out.println("Loading [" + className + "]"); //TODO: remove or make Logger
-               Class<?> messageClass = Class.forName(className, true, type.getClassLoader()).asSubclass(type);
-
-               Field field = messageClass.getField("INSTANCE");
-
-               return type.cast(field.get(null));
-            } catch (ClassNotFoundException e) {
-               return null;
-            } catch (NoSuchFieldException e) {
-               throw new IllegalStateException(e.getMessage(), e);
-            } catch (IllegalAccessException e) {
-               e.printStackTrace();
-               throw new IllegalStateException(e.getMessage(), e);
+               implClass = Class.forName(implClassName, true, type.getClassLoader()).asSubclass(type);
+            } catch (Exception e) {
+               throw new IllegalArgumentException("Unable to find class for log/message impl: " + implClassName, e);
+            }
+
+            final Constructor<? extends T> constructor;
+            try {
+               constructor = implClass.getConstructor(Logger.class);
+            } catch (Exception e) {
+               throw new IllegalArgumentException("Unable to find constructor for log/message impl: " + implClassName, e);
+            }
+
+            try {
+               Logger logger = LoggerFactory.getLogger(category);
+
+               return type.cast(constructor.newInstance(logger));
+            } catch (Exception e) {
+               throw new IllegalArgumentException("Unable to create instance for log/message impl: " + implClassName, e);
             }
          }
       });
diff --git a/artemis-log-processor/src/main/java/org/apache/activemq/artemis/logprocessor/LogProcessor.java b/artemis-log-processor/src/main/java/org/apache/activemq/artemis/logprocessor/LogProcessor.java
index b772add78b..a3f80f3c29 100644
--- a/artemis-log-processor/src/main/java/org/apache/activemq/artemis/logprocessor/LogProcessor.java
+++ b/artemis-log-processor/src/main/java/org/apache/activemq/artemis/logprocessor/LogProcessor.java
@@ -125,16 +125,6 @@ public class LogProcessor extends AbstractProcessor {
                writerOutput.println("   }");
                writerOutput.println();
 
-               writerOutput.println("   public " + simpleClassName + "() {");
-               writerOutput.println("      this(LoggerFactory.getLogger(" + fullClassName + ".class));");
-               writerOutput.println("   }");
-               writerOutput.println();
-
-
-               // Declaring the static field that's used by {@link I18NFactory}
-               writerOutput.println("   public static " + simpleClassName + " INSTANCE = new " + simpleClassName + "();");
-               writerOutput.println();
-
                for (Element el : annotatedType.getEnclosedElements()) {
                   if (el.getKind() == ElementKind.METHOD) {