You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@kyuubi.apache.org by GitBox <gi...@apache.org> on 2022/08/16 10:35:32 UTC

[GitHub] [incubator-kyuubi] pan3793 commented on a diff in pull request #3243: Moved trait Logging#initializeLogging to object Logging

pan3793 commented on code in PR #3243:
URL: https://github.com/apache/incubator-kyuubi/pull/3243#discussion_r946608841


##########
kyuubi-common/src/main/scala/org/apache/kyuubi/Logging.scala:
##########
@@ -88,13 +88,55 @@ trait Logging {
     if (!Logging.initialized) {
       Logging.initLock.synchronized {
         if (!Logging.initialized) {
-          initializeLogging(isInterpreter)
+          Logging.initializeLogging(isInterpreter, loggerName, () => logger)
         }
       }
     }
   }
+}
+
+object Logging {
+  @volatile private var useDefault = false
+  @volatile private var defaultRootLevel: String = _
+  @volatile private var initialized = false
+  val initLock = new Object()
+
+  private[kyuubi] def isLog4j12: Boolean = {
+    // This distinguishes the log4j 1.2 binding, currently
+    // org.slf4j.impl.Log4jLoggerFactory, from the log4j 2.0 binding, currently
+    // org.apache.logging.slf4j.Log4jLoggerFactory
+    val binderClass = StaticLoggerBinder.getSingleton.getLoggerFactoryClassStr
+    "org.slf4j.impl.Log4jLoggerFactory".equals(binderClass)
+  }
+
+  private[kyuubi] def isLog4j2: Boolean = {
+    // This distinguishes the log4j 1.2 binding, currently
+    // org.slf4j.impl.Log4jLoggerFactory, from the log4j 2.0 binding, currently
+    // org.apache.logging.slf4j.Log4jLoggerFactory
+    val binderClass = StaticLoggerBinder.getSingleton.getLoggerFactoryClassStr
+    "org.apache.logging.slf4j.Log4jLoggerFactory".equals(binderClass)
+  }
+
+  /**
+   * Return true if log4j2 is initialized by default configuration which has one
+   * appender with error level. See `org.apache.logging.log4j.core.config.DefaultConfiguration`.
+   */
+  private def isLog4j2DefaultConfigured(): Boolean = {
+    val rootLogger = LogManager.getRootLogger.asInstanceOf[Log4jLogger]
+    // If Log4j 2 is used but is initialized by default configuration,
+    // load a default properties file
+    // (see org.apache.logging.log4j.core.config.DefaultConfiguration)
+    rootLogger.getAppenders.isEmpty ||
+    (rootLogger.getAppenders.size() == 1 &&
+      rootLogger.getLevel == Level.ERROR &&
+      LogManager.getContext.asInstanceOf[LoggerContext]
+        .getConfiguration.isInstanceOf[DefaultConfiguration])
+  }
 
-  private def initializeLogging(isInterpreter: Boolean): Unit = {
+  private def initializeLogging(
+      isInterpreter: Boolean,
+      loggerName: String,
+      logger: () => Logger): Unit = {

Review Comment:
   thuck could be used here to simplify code
   ```suggestion
         logger: => Logger): Unit = {
   ```



-- 
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: notifications-unsubscribe@kyuubi.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@kyuubi.apache.org
For additional commands, e-mail: notifications-help@kyuubi.apache.org