You are viewing a plain text version of this content. The canonical link for it is here.
Posted to gitbox@activemq.apache.org by GitBox <gi...@apache.org> on 2022/09/29 11:47:34 UTC

[GitHub] [activemq-artemis] gemmellr commented on a diff in pull request #4237: ARTEMIS-4020 Using a little trick to create the Loggers

gemmellr commented on code in PR #4237:
URL: https://github.com/apache/activemq-artemis/pull/4237#discussion_r983387224


##########
artemis-junit/src/main/java/org/apache/activemq/artemis/junit/EmbeddedActiveMQResource.java:
##########
@@ -66,11 +67,10 @@
  * </code></pre>
  */
 public class EmbeddedActiveMQResource extends ExternalResource {
+   private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

Review Comment:
   Ditto



##########
artemis-core-client/src/main/java/org/apache/activemq/artemis/core/remoting/impl/ssl/DefaultSSLContextFactory.java:
##########
@@ -15,18 +15,23 @@
  */
 package org.apache.activemq.artemis.core.remoting.impl.ssl;
 
+import java.lang.invoke.MethodHandles;
 import java.util.Map;
 import javax.net.ssl.SSLContext;
 import org.apache.activemq.artemis.core.remoting.impl.netty.TransportConstants;
 import org.apache.activemq.artemis.spi.core.remoting.ssl.SSLContextConfig;
 import org.apache.activemq.artemis.spi.core.remoting.ssl.SSLContextFactory;
 import org.apache.activemq.artemis.utils.ConfigurationHelper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * Simple SSLContextFactory for use in NettyConnector and NettyAcceptor.
  */
 public class DefaultSSLContextFactory implements SSLContextFactory {
 
+   private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

Review Comment:
   Ditto



##########
artemis-junit/src/main/java/org/apache/activemq/artemis/junit/EmbeddedJMSResource.java:
##########
@@ -78,6 +79,7 @@
  */
 @Deprecated
 public class EmbeddedJMSResource extends ExternalResource {
+   private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

Review Comment:
   This adds 'logger' but the class also has and is still using "log" below.



##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/sasl/scram/SCRAMServerSASLFactory.java:
##########
@@ -44,14 +44,16 @@
 import org.apache.activemq.artemis.spi.core.security.scram.UserData;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import java.lang.invoke.MethodHandles;
 
 /**
  * abstract class that implements the SASL-SCRAM authentication scheme, concrete implementations
  * must supply the {@link SCRAM} type to use and be register via SPI
  */
 public abstract class SCRAMServerSASLFactory implements ServerSASLFactory {
 
-   private final Logger logger = LoggerFactory.getLogger(getClass());
+   private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

Review Comment:
   This changes the name of the Logger, which is used for different mechanisms and passed around to related objects. The child classes should be changed to have their own static Logger and pass it via the constructor rather than creating here, to retain existing behaviour and allow distinguishing the mechanism the logging is actually for.



##########
tests/unit-tests/src/test/java/org/apache/activemq/artemis/tests/unit/logging/AssertionLoggerTest.java:
##########
@@ -64,7 +65,7 @@ public void testInfoAMQP() throws Exception {
 
    private void validateLogging(Class<?> clazz) {
       String randomLogging = RandomUtil.randomString();
-      Logger logging = LoggerFactory.getLogger(clazz);
+      Logger logging = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

Review Comment:
   This breaks the intent of the method, it should be restored.



##########
artemis-junit/src/main/java/org/apache/activemq/artemis/junit/AbstractActiveMQClientResource.java:
##########
@@ -27,10 +27,11 @@
 import org.junit.rules.ExternalResource;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import java.lang.invoke.MethodHandles;
 
 public abstract class AbstractActiveMQClientResource extends ExternalResource {
 
-   Logger log = LoggerFactory.getLogger(this.getClass());
+   private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

Review Comment:
   This will change the logger name from being the specific non-abstract childs, to the abstract parent class.
   
   May not be an issue, since the 'start' logging using it actually prints the class simple name, but just noting the difference.
   
   Could pass in the logger to use to retain the existing naming behaviour.



##########
artemis-server/src/main/java/org/apache/activemq/artemis/core/server/impl/ActiveMQServerImpl.java:
##########
@@ -814,7 +815,7 @@ public void run() {
    }
 
    private static void checkCriticalAnalyzerLogging() {
-      Logger criticalLogger = LoggerFactory.getLogger("org.apache.activemq.artemis.utils.critical");
+      Logger criticalLogger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

Review Comment:
   This changes the logger name used for the check to the wrong value, the one for ActiveMQServerImpl (and so also duplicates the class-level logger), rather than "org.apache.activemq.artemis.utils.critical" used before, which is what is also referenced in the log4j2.properties config.
   
   Should be changed to a value derived from the declaration of the original logger elsewhere (maybe declared as a constant).



##########
artemis-server/src/main/java/org/apache/activemq/artemis/spi/core/security/jaas/AbstractPrincipalLoginModule.java:
##########
@@ -31,12 +31,13 @@
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import java.lang.invoke.MethodHandles;
 
 /**
  * Abstract login module that uses an external authenticated principal
  */
 public abstract class AbstractPrincipalLoginModule implements AuditLoginModule {
-   private final Logger logger = LoggerFactory.getLogger(getClass());
+   private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

Review Comment:
   Similar to earlier comments, this changes the logger used by the subclasses to all be the same name, making it no longer possible to tell which one the logging related to. Should instead make them pass in their own static logger to retain existing logging behaviour.



##########
artemis-server/src/main/java/org/apache/activemq/artemis/core/server/impl/FileBasedNodeManager.java:
##########
@@ -32,14 +32,15 @@
 import org.apache.activemq.artemis.utils.UUIDGenerator;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import java.lang.invoke.MethodHandles;
 
 import static java.nio.file.StandardOpenOption.CREATE;
 import static java.nio.file.StandardOpenOption.READ;
 import static java.nio.file.StandardOpenOption.WRITE;
 
 public abstract class FileBasedNodeManager extends NodeManager {
 
-   private static final Logger LOGGER = LoggerFactory.getLogger(FileBasedNodeManager.class);
+   private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

Review Comment:
   Should be deleted instead; name changed without any other uses changing, so its unused.



##########
artemis-core-client/src/main/java/org/apache/activemq/artemis/core/remoting/impl/ssl/DefaultOpenSSLContextFactory.java:
##########
@@ -16,27 +16,32 @@
  */
 package org.apache.activemq.artemis.core.remoting.impl.ssl;
 
+import java.lang.invoke.MethodHandles;
 import java.util.Map;
 
 import io.netty.handler.ssl.SslContext;
 import org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnector;
 import org.apache.activemq.artemis.core.remoting.impl.netty.TransportConstants;
 import org.apache.activemq.artemis.spi.core.remoting.ssl.OpenSSLContextFactory;
 import org.apache.activemq.artemis.spi.core.remoting.ssl.SSLContextConfig;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * Default {@link OpenSSLContextFactory} for use in {@link NettyConnector} and NettyAcceptor.
  */
 public class DefaultOpenSSLContextFactory implements OpenSSLContextFactory {
 
+   private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

Review Comment:
   Since it is debug and really specific to this class, I think this is fine, especially as having a logger in an interface seems ugly, but just noting that this is changing the logger name used.



##########
artemis-server/src/test/java/org/apache/activemq/artemis/tests/util/ActiveMQTestBase.java:
##########
@@ -172,17 +173,17 @@
  */
 public abstract class ActiveMQTestBase extends Assert {
 
-   private static final Logger log = LoggerFactory.getLogger(ActiveMQTestBase.class);
+   private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

Review Comment:
   Might be worth looking at why there are 3 identical Logger declarations in this class (more below this one).



##########
tests/artemis-test-support/src/main/java/org/apache/activemq/transport/amqp/client/AmqpProtocolTracer.java:
##########
@@ -37,7 +39,7 @@
  */
 public class AmqpProtocolTracer implements ProtocolTracer, FrameBodyHandler<AmqpFrameValidator> {
 
-   private static final Logger TRACE_FRAMES = LoggerFactory.getLogger(AmqpProtocolTracer.class.getPackage().getName() + ".FRAMES");
+   private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

Review Comment:
   Noting it changes the logger name. Maybe not an issue as its only the test client.



-- 
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: gitbox-unsubscribe@activemq.apache.org

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