You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by sb...@apache.org on 2015/07/20 16:55:15 UTC

[12/24] incubator-ignite git commit: # ignite-788: nodeId and fileName

# ignite-788: nodeId and fileName


Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/a04605a6
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/a04605a6
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/a04605a6

Branch: refs/heads/ignite-788-last-review
Commit: a04605a602b361f203ec5504f92b6734f551db2c
Parents: f524d3f
Author: ashutak <as...@gridgain.com>
Authored: Thu Jul 16 13:58:10 2015 +0300
Committer: ashutak <as...@gridgain.com>
Committed: Thu Jul 16 13:58:10 2015 +0300

----------------------------------------------------------------------
 config/ignite-log4j.xml                         |  4 +-
 config/ignite-log4j2.xml                        | 12 ++--
 .../logger/log4j/Log4jRollingFileAppender.java  |  4 --
 .../ignite/logger/log4j2/Log4J2Logger.java      | 63 +++++++++++++-------
 .../log4j2/GridLog4j2CorrectFileNameTest.java   | 10 +++-
 .../log4j2/GridLog4j2InitiallizationTest.java   | 12 ++++
 6 files changed, 70 insertions(+), 35 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/a04605a6/config/ignite-log4j.xml
----------------------------------------------------------------------
diff --git a/config/ignite-log4j.xml b/config/ignite-log4j.xml
index 8dd114c..192e945 100644
--- a/config/ignite-log4j.xml
+++ b/config/ignite-log4j.xml
@@ -123,9 +123,9 @@
         <level value="INFO"/>
 
         <!-- Uncomment to enable logging to console. -->
-        <!--&lt;!&ndash;-->
+        <!--
         <appender-ref ref="CONSOLE"/>
-        <!--&ndash;&gt;-->
+        -->
 
         <appender-ref ref="CONSOLE_ERR"/>
         <appender-ref ref="FILE"/>

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/a04605a6/config/ignite-log4j2.xml
----------------------------------------------------------------------
diff --git a/config/ignite-log4j2.xml b/config/ignite-log4j2.xml
index c1393aa..3f7b3d5 100644
--- a/config/ignite-log4j2.xml
+++ b/config/ignite-log4j2.xml
@@ -27,11 +27,11 @@
             <PatternLayout pattern="[%d{ABSOLUTE}][%-5p][%t][%c{1}] %m%n"/>
         </Console>
 
-        <Routing name="Routing">
-            <Routes pattern="$${ctx:nodeId}">
+        <Routing name="FILE">
+            <Routes pattern="$${sys:nodeId}">
                 <Route>
-                    <RollingFile name="Rolling-${ctx:nodeId}" fileName="work/log/ignite-${ctx:nodeId}.log"
-                                 filePattern="work/log/ignite-${ctx:nodeId}-%i-%d{yyyy-MM-dd}.log.gz">
+                    <RollingFile name="Rolling-${sys:nodeId}" fileName="work/log/ignite-${sys:nodeId}.log"
+                                 filePattern="work/log/ignite-${sys:nodeId}-%i-%d{yyyy-MM-dd}.log.gz">
                         <PatternLayout pattern="[%d{ABSOLUTE}][%-5p][%t][%c{1}] %m%n"/>
                         <Policies>
                             <TimeBasedTriggeringPolicy interval="6" modulate="true" />
@@ -41,7 +41,7 @@
                 </Route>
 
                 <!--
-                <Route key="$${ctx:nodeId}">
+                <Route key="$${sys:nodeId}">
                     <RollingFile name="Rolling-default" fileName="work/log/ignite-default.log"
                                  filePattern="work/log/ignite-default-%i-%d{yyyy-MM-dd}.log.gz">
                         <PatternLayout pattern="[%d{ABSOLUTE}][%-5p][%t][%c{1}] %m%n"/>
@@ -87,7 +87,7 @@
             -->
 
             <AppenderRef ref="CONSOLE_ERR" level="ERROR"/>
-            <AppenderRef ref="Routing" level="DEBUG"/>
+            <AppenderRef ref="FILE" level="DEBUG"/>
         </Root>
     </Loggers>
 </Configuration>

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/a04605a6/modules/log4j/src/main/java/org/apache/ignite/logger/log4j/Log4jRollingFileAppender.java
----------------------------------------------------------------------
diff --git a/modules/log4j/src/main/java/org/apache/ignite/logger/log4j/Log4jRollingFileAppender.java b/modules/log4j/src/main/java/org/apache/ignite/logger/log4j/Log4jRollingFileAppender.java
index fbe3aab..dcf03a4 100644
--- a/modules/log4j/src/main/java/org/apache/ignite/logger/log4j/Log4jRollingFileAppender.java
+++ b/modules/log4j/src/main/java/org/apache/ignite/logger/log4j/Log4jRollingFileAppender.java
@@ -73,16 +73,12 @@ public class Log4jRollingFileAppender extends RollingFileAppender implements Log
 
     /** {@inheritDoc} */
     @Override public synchronized void updateFilePath(IgniteClosure<String, String> filePathClos) {
-        U.debug("**************** updateFilePath ************");
-
         A.notNull(filePathClos, "filePathClos");
 
         if (baseFileName == null)
             baseFileName = fileName;
 
         fileName = filePathClos.apply(baseFileName);
-
-        U.debug("**************** fileName=" + fileName + " appender=" + System.identityHashCode(this) + " ************");
     }
 
     /** {@inheritDoc} */

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/a04605a6/modules/log4j2/src/main/java/org/apache/ignite/logger/log4j2/Log4J2Logger.java
----------------------------------------------------------------------
diff --git a/modules/log4j2/src/main/java/org/apache/ignite/logger/log4j2/Log4J2Logger.java b/modules/log4j2/src/main/java/org/apache/ignite/logger/log4j2/Log4J2Logger.java
index 0cfe485..5ccbc04 100644
--- a/modules/log4j2/src/main/java/org/apache/ignite/logger/log4j2/Log4J2Logger.java
+++ b/modules/log4j2/src/main/java/org/apache/ignite/logger/log4j2/Log4J2Logger.java
@@ -27,11 +27,13 @@ import org.apache.logging.log4j.*;
 import org.apache.logging.log4j.Logger;
 import org.apache.logging.log4j.core.*;
 import org.apache.logging.log4j.core.appender.*;
+import org.apache.logging.log4j.core.appender.routing.*;
 import org.apache.logging.log4j.core.config.*;
 import org.apache.logging.log4j.core.layout.*;
 import org.jetbrains.annotations.*;
 
 import java.io.*;
+import java.lang.reflect.*;
 import java.net.*;
 import java.util.*;
 
@@ -67,7 +69,11 @@ import static org.apache.ignite.IgniteSystemProperties.*;
  * injection.
  */
 public class Log4J2Logger implements IgniteLogger, LoggerNodeIdAware {
-    public static final String LOGGER_NAME = "Log4J2Logger";
+    /** */
+    // TODO review.
+    public static final String LOGGER_NAME = LogManager.ROOT_LOGGER_NAME;
+
+    /** */
     public static final String NODE_ID = "nodeId";
 
     /** */
@@ -172,7 +178,6 @@ public class Log4J2Logger implements IgniteLogger, LoggerNodeIdAware {
         addConsoleAppenderIfNeeded(null, new C1<Boolean, Logger>() {
             @Override public Logger apply(Boolean init) {
                 if (init)
-                    // TODO review
                     Configurator.initialize(LOGGER_NAME, cfgUrl.toString());
 
                 return LogManager.getRootLogger();
@@ -188,7 +193,6 @@ public class Log4J2Logger implements IgniteLogger, LoggerNodeIdAware {
      * @param cfgFile Log4j configuration XML file.
      * @throws IgniteCheckedException Thrown in case logger can't be created.
      */
-    // TODO create test
     public Log4J2Logger(File cfgFile) throws IgniteCheckedException {
         if (cfgFile == null)
             throw new IgniteCheckedException("Configuration XML file for Log4j must be specified.");
@@ -216,7 +220,6 @@ public class Log4J2Logger implements IgniteLogger, LoggerNodeIdAware {
      * @param cfgUrl URL for Log4j configuration XML file.
      * @throws IgniteCheckedException Thrown in case logger can't be created.
      */
-    // TODO test it.
     public Log4J2Logger(final URL cfgUrl) throws IgniteCheckedException {
         if (cfgUrl == null)
             throw new IgniteCheckedException("Configuration XML file for Log4j must be specified.");
@@ -262,24 +265,40 @@ public class Log4J2Logger implements IgniteLogger, LoggerNodeIdAware {
 
     /** {@inheritDoc} */
     @Nullable @Override public String fileName() {
-        // TODO
-        // It was.
-//        FileAppender fapp = F.first(fileAppenders);
-//
-//        return fapp != null ? fapp.getFile() : null;
+        for (org.apache.logging.log4j.core.Logger log = (org.apache.logging.log4j.core.Logger)impl;
+            log != null; log = log.getParent()) {
+            for (Appender a : log.getAppenders().values()) {
+                if (a instanceof FileAppender)
+                    return ((FileAppender)a).getFileName();
+
+                if (a instanceof RollingFileAppender)
+                    return ((RollingFileAppender)a).getFileName();
+
+                if (a instanceof RoutingAppender) {
+                    try {
+                        RoutingAppender routing = (RoutingAppender)a;
+
+                        Field appsFiled = routing.getClass().getDeclaredField("appenders");
 
-        // New logic.
-        // TODO cast, RollingFileAppender and etc.
-        org.apache.logging.log4j.core.Logger logImpl = (org.apache.logging.log4j.core.Logger)impl;
+                        appsFiled.setAccessible(true);
 
-        Collection<Appender> appenders = logImpl.getAppenders().values();
+                        Map<String, AppenderControl> appenders = (Map<String, AppenderControl>)appsFiled.get(routing);
 
-        for (Appender a : appenders) {
-            if (a instanceof FileAppender)
-                return ((FileAppender)a).getFileName();
+                        for (AppenderControl control : appenders.values()) {
+                            Appender innerApp = control.getAppender();
 
-            if (a instanceof RollingFileAppender)
-                return ((RollingFileAppender)a).getFileName();
+                            if (innerApp instanceof FileAppender)
+                                return ((FileAppender)innerApp).getFileName();
+
+                            if (innerApp instanceof RollingFileAppender)
+                                return ((RollingFileAppender)innerApp).getFileName();
+                        }
+                    }
+                    catch (IllegalAccessException | NoSuchFieldException e) {
+                        error("Faild to get file name. Looks like the implementation of log4j 2 was changed.", e);
+                    }
+                }
+            }
         }
 
         return null;
@@ -421,10 +440,14 @@ public class Log4J2Logger implements IgniteLogger, LoggerNodeIdAware {
 
         this.nodeId = nodeId;
 
-        // Set nodeId at context to be used at configuration.
-        ThreadContext.put(NODE_ID, U.id8(nodeId));
+        // Set nodeId as system variable to be used at configuration.
+        System.setProperty("nodeId", U.id8(nodeId));
 
         ((LoggerContext) LogManager.getContext(false)).reconfigure();
+
+        // Hack. To touch the logger to create all log resources (files). Then #fileName() will work properly.
+        // TODO review it
+        impl.log(Level.OFF, "");
     }
 
     /** {@inheritDoc} */

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/a04605a6/modules/log4j2/src/test/java/org/apache/ignite/logger/log4j2/GridLog4j2CorrectFileNameTest.java
----------------------------------------------------------------------
diff --git a/modules/log4j2/src/test/java/org/apache/ignite/logger/log4j2/GridLog4j2CorrectFileNameTest.java b/modules/log4j2/src/test/java/org/apache/ignite/logger/log4j2/GridLog4j2CorrectFileNameTest.java
index 7a968d4..2d8ee82 100644
--- a/modules/log4j2/src/test/java/org/apache/ignite/logger/log4j2/GridLog4j2CorrectFileNameTest.java
+++ b/modules/log4j2/src/test/java/org/apache/ignite/logger/log4j2/GridLog4j2CorrectFileNameTest.java
@@ -48,13 +48,17 @@ public class GridLog4j2CorrectFileNameTest extends TestCase {
      * @throws Exception If error occurred.
      */
     private void checkOneNode(int id) throws Exception {
-        String id8 = null;
-        
+        String id8;
+        String logPath;
+
         try (Ignite ignite = G.start(getConfiguration("grid" + id))) {
             id8 = U.id8(ignite.cluster().localNode().id());
+
+            logPath = "work/log/ignite-" + id8 + ".log";
+
+            assertEquals(logPath, ignite.log().fileName());
         }
 
-        String logPath = "work/log/ignite-" + id8 + ".log";
         File logFile = U.resolveIgnitePath(logPath);
 
         assertNotNull("Failed to resolve path: " + logPath, logFile);

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/a04605a6/modules/log4j2/src/test/java/org/apache/ignite/logger/log4j2/GridLog4j2InitiallizationTest.java
----------------------------------------------------------------------
diff --git a/modules/log4j2/src/test/java/org/apache/ignite/logger/log4j2/GridLog4j2InitiallizationTest.java b/modules/log4j2/src/test/java/org/apache/ignite/logger/log4j2/GridLog4j2InitiallizationTest.java
index c2fb7ca..734ca6d 100644
--- a/modules/log4j2/src/test/java/org/apache/ignite/logger/log4j2/GridLog4j2InitiallizationTest.java
+++ b/modules/log4j2/src/test/java/org/apache/ignite/logger/log4j2/GridLog4j2InitiallizationTest.java
@@ -19,6 +19,7 @@ package org.apache.ignite.logger.log4j2;
 
 import junit.framework.*;
 import org.apache.ignite.*;
+import org.apache.ignite.internal.util.typedef.internal.*;
 import org.apache.ignite.logger.*;
 import org.apache.ignite.testframework.*;
 import org.apache.ignite.testframework.junits.common.*;
@@ -91,4 +92,15 @@ public class GridLog4j2InitiallizationTest extends TestCase {
         log.error("This is 'error' message.");
         log.error("This is 'error' message.", new Exception("It's a test error exception"));
     }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testSystemNodeId() throws Exception {
+        UUID id = UUID.randomUUID();
+
+        new Log4J2Logger().setNodeId(id);
+
+        assertEquals(U.id8(id), System.getProperty("nodeId"));
+    }
 }