You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by sk...@apache.org on 2021/10/08 15:38:33 UTC

[ignite-3] branch main updated: IGNITE-15675 Added jul formatting to CLI module. Fixes #390

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

sk0x50 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/ignite-3.git


The following commit(s) were added to refs/heads/main by this push:
     new e56b90c  IGNITE-15675 Added jul formatting to CLI module. Fixes #390
e56b90c is described below

commit e56b90ce652446662c0253436b4f0e28f1bf111b
Author: Mirza Aliev <al...@gmail.com>
AuthorDate: Fri Oct 8 18:35:39 2021 +0300

    IGNITE-15675 Added jul formatting to CLI module. Fixes #390
    
    Signed-off-by: Slava Koptilin <sl...@gmail.com>
---
 modules/cli/pom.xml                                |  5 +++
 .../java/org/apache/ignite/cli/IgniteCliApp.java   | 29 +++++++++++++
 .../java/org/apache/ignite/cli/IgnitePaths.java    |  7 ++++
 .../cli/builtins/init/InitIgniteCommand.java       | 19 +++++++++
 .../ignite/cli/builtins/node/NodeManager.java      |  7 +++-
 .../apache/ignite/cli/spec/NodeCommandSpec.java    |  1 +
 .../resources/cli.java.util.logging.properties     | 47 ++++++++++++++++++++++
 .../resources/ignite.java.util.logging.properties  | 45 +++++++++++++++++++++
 modules/cli/src/main/resources/logback.xml         | 31 --------------
 .../apache/ignite/cli/IgniteCliInterfaceTest.java  |  3 +-
 modules/cli/src/test/resources/logback-test.xml    | 31 --------------
 11 files changed, 161 insertions(+), 64 deletions(-)

diff --git a/modules/cli/pom.xml b/modules/cli/pom.xml
index 551b239..f07ea00 100644
--- a/modules/cli/pom.xml
+++ b/modules/cli/pom.xml
@@ -84,6 +84,11 @@
             <artifactId>annotations</artifactId>
         </dependency>
 
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-jdk14</artifactId>
+        </dependency>
+
         <!-- Test dependencies -->
         <dependency>
             <groupId>org.junit.jupiter</groupId>
diff --git a/modules/cli/src/main/java/org/apache/ignite/cli/IgniteCliApp.java b/modules/cli/src/main/java/org/apache/ignite/cli/IgniteCliApp.java
index 1f3c3f0..458e4cc 100644
--- a/modules/cli/src/main/java/org/apache/ignite/cli/IgniteCliApp.java
+++ b/modules/cli/src/main/java/org/apache/ignite/cli/IgniteCliApp.java
@@ -17,6 +17,11 @@
 
 package org.apache.ignite.cli;
 
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.StandardCopyOption;
 import io.micronaut.context.ApplicationContext;
 import org.apache.ignite.cli.spec.IgniteCliSpec;
 import org.fusesource.jansi.AnsiConsole;
@@ -26,6 +31,8 @@ import org.fusesource.jansi.AnsiConsole;
  */
 public class IgniteCliApp {
     public static void main(String... args) {
+        initJavaLoggerProps();
+
         ApplicationContext applicationCtx = ApplicationContext.run();
 
         int exitCode;
@@ -42,4 +49,26 @@ public class IgniteCliApp {
         System.exit(exitCode);
     }
 
+    /**
+     * This is a temporary solution to hide unnecessary java util logs that are produced by ivy.
+     * ConsoleHandler.level should be set to SEVERE.
+     * TODO: https://issues.apache.org/jira/browse/IGNITE-15713
+     */
+    private static void initJavaLoggerProps() {
+        InputStream propsFile = IgniteCliApp.class.getResourceAsStream("/cli.java.util.logging.properties");
+
+        Path props = null;
+
+        try {
+            props = Files.createTempFile("cli.java.util.logging.properties", "");
+
+            if (propsFile != null)
+                Files.copy(propsFile, props.toAbsolutePath(), StandardCopyOption.REPLACE_EXISTING);
+        }
+        catch (IOException ignored) {
+        }
+
+        if (props != null)
+            System.setProperty("java.util.logging.config.file", props.toString());
+    }
 }
diff --git a/modules/cli/src/main/java/org/apache/ignite/cli/IgnitePaths.java b/modules/cli/src/main/java/org/apache/ignite/cli/IgnitePaths.java
index b31c83a..1346b14 100644
--- a/modules/cli/src/main/java/org/apache/ignite/cli/IgnitePaths.java
+++ b/modules/cli/src/main/java/org/apache/ignite/cli/IgnitePaths.java
@@ -133,6 +133,13 @@ public class IgnitePaths {
     }
 
     /**
+     * @return Path to default Ignite node config.
+     */
+    public Path serverJavaUtilLoggingPros() {
+        return cfgDir.resolve("ignite.java.util.logging.properties");
+    }
+
+    /**
      * Init or recovers Ignite distributive directories structure.
      */
     public void initOrRecover() {
diff --git a/modules/cli/src/main/java/org/apache/ignite/cli/builtins/init/InitIgniteCommand.java b/modules/cli/src/main/java/org/apache/ignite/cli/builtins/init/InitIgniteCommand.java
index c751dce..2d69d01 100644
--- a/modules/cli/src/main/java/org/apache/ignite/cli/builtins/init/InitIgniteCommand.java
+++ b/modules/cli/src/main/java/org/apache/ignite/cli/builtins/init/InitIgniteCommand.java
@@ -114,6 +114,8 @@ public class InitIgniteCommand {
 
         initDefaultServerConfigs(cfg.serverDefaultConfigFile());
 
+        initJavaUtilLoggingPros(cfg.serverJavaUtilLoggingPros());
+
         out.println();
         out.println("Apache Ignite is successfully initialized. Use the " +
             cs.commandText("ignite node start") + " command to start a new local node.");
@@ -137,6 +139,23 @@ public class InitIgniteCommand {
     }
 
     /**
+     * Init java util logging properties file.
+     *
+     * @param javaUtilLogProps Path to java util logging properties file.
+     */
+    private void initJavaUtilLoggingPros(Path javaUtilLogProps) {
+        try {
+            if (!javaUtilLogProps.toFile().exists())
+                Files.copy(
+                    InitIgniteCommand.class
+                        .getResourceAsStream("/ignite.java.util.logging.properties"), javaUtilLogProps);
+        }
+        catch (IOException e) {
+            throw new IgniteCLIException("Can't create default config file for server", e);
+        }
+    }
+
+    /**
      * Downloads ignite node distro with all needed dependencies.
      *
      * @param ignitePaths Ignite distributive paths (bin, config, etc.).
diff --git a/modules/cli/src/main/java/org/apache/ignite/cli/builtins/node/NodeManager.java b/modules/cli/src/main/java/org/apache/ignite/cli/builtins/node/NodeManager.java
index 2ea71af..3e69c91 100644
--- a/modules/cli/src/main/java/org/apache/ignite/cli/builtins/node/NodeManager.java
+++ b/modules/cli/src/main/java/org/apache/ignite/cli/builtins/node/NodeManager.java
@@ -77,12 +77,13 @@ public class NodeManager {
      * @param out PrintWriter for user messages.
      * @return Information about successfully started node
      */
-    public RunningNode start(String nodeName, Path logDir, Path pidsDir, Path srvCfg, PrintWriter out) {
+    public RunningNode start(String nodeName, Path logDir, Path pidsDir, Path srvCfg, Path javaLogProps, PrintWriter out) {
         if (getRunningNodes(logDir, pidsDir).stream().anyMatch(n -> n.name.equals(nodeName)))
             throw new IgniteCLIException("Node with nodeName " + nodeName + " is already exist");
 
         try {
             Path logFile = logFile(logDir, nodeName);
+
             if (Files.exists(logFile))
                 Files.delete(logFile);
 
@@ -91,6 +92,10 @@ public class NodeManager {
             var cmdArgs = new ArrayList<String>();
 
             cmdArgs.add("java");
+
+            if (javaLogProps != null)
+                cmdArgs.add("-Djava.util.logging.config.file=" + javaLogProps.toAbsolutePath());
+
             cmdArgs.add("-cp");
             cmdArgs.add(classpath());
             cmdArgs.add(MAIN_CLASS);
diff --git a/modules/cli/src/main/java/org/apache/ignite/cli/spec/NodeCommandSpec.java b/modules/cli/src/main/java/org/apache/ignite/cli/spec/NodeCommandSpec.java
index e0b5c65..571ccca 100644
--- a/modules/cli/src/main/java/org/apache/ignite/cli/spec/NodeCommandSpec.java
+++ b/modules/cli/src/main/java/org/apache/ignite/cli/spec/NodeCommandSpec.java
@@ -77,6 +77,7 @@ public class NodeCommandSpec extends CategorySpec {
             NodeManager.RunningNode node = nodeMgr.start(nodeName, ignitePaths.logDir,
                 ignitePaths.cliPidsDir(),
                 configPath,
+                ignitePaths.serverJavaUtilLoggingPros(),
                 out);
 
             out.println();
diff --git a/modules/cli/src/main/resources/cli.java.util.logging.properties b/modules/cli/src/main/resources/cli.java.util.logging.properties
new file mode 100644
index 0000000..6809b3a
--- /dev/null
+++ b/modules/cli/src/main/resources/cli.java.util.logging.properties
@@ -0,0 +1,47 @@
+#
+# 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.
+#
+
+#########################################################################
+#       Default java.util.logging configuration for Ignite CLI module.
+#
+# To use another config file use `java.util.logging.config.file` system
+# property. For example `java -Djava.util.logging.config.file=myfile`
+#########################################################################
+
+#
+# Comma-separated list of logging "handlers". Note that some of them may be
+# reconfigured (or even removed) at runtime according to system properties.
+handlers=java.util.logging.ConsoleHandler
+
+#
+# Default global logging level.
+# This specifies which kinds of events are logged across all loggers.
+# For any given category this global level can be overridden by a category
+# specific level.
+# Note that handlers also have a separate level setting to limit messages
+# printed through it.
+#
+.level=SEVERE
+
+# Console handler logs all messages with importance level `SEVERE` and above
+# into standard error stream (`System.err`).
+#
+java.util.logging.ConsoleHandler.formatter = org.apache.ignite.lang.JavaLoggerFormatter
+# ConsoleHandler level should be set to SEVERE, otherwise CLI commands logging they'll be fill with unnecessary logs
+# TODO This will be changed in https://issues.apache.org/jira/browse/IGNITE-15713
+java.util.logging.ConsoleHandler.level = SEVERE
+java.util.logging.ConsoleHandler.encoding = UTF-8
diff --git a/modules/cli/src/main/resources/ignite.java.util.logging.properties b/modules/cli/src/main/resources/ignite.java.util.logging.properties
new file mode 100644
index 0000000..4910a3f
--- /dev/null
+++ b/modules/cli/src/main/resources/ignite.java.util.logging.properties
@@ -0,0 +1,45 @@
+#
+# 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.
+#
+
+#########################################################################
+#       Default java.util.logging configuration for Ignite instance.
+#
+# To use another config file use `java.util.logging.config.file` system
+# property. For example `java -Djava.util.logging.config.file=myfile`
+#########################################################################
+
+#
+# Comma-separated list of logging "handlers". Note that some of them may be
+# reconfigured (or even removed) at runtime according to system properties.
+handlers=java.util.logging.ConsoleHandler
+
+#
+# Default global logging level.
+# This specifies which kinds of events are logged across all loggers.
+# For any given category this global level can be overridden by a category
+# specific level.
+# Note that handlers also have a separate level setting to limit messages
+# printed through it.
+#
+.level=INFO
+
+# Console handler logs all messages with importance level `INFO` and above
+# into standard error stream (`System.err`).
+#
+java.util.logging.ConsoleHandler.formatter = org.apache.ignite.lang.JavaLoggerFormatter
+java.util.logging.ConsoleHandler.level = INFO
+java.util.logging.ConsoleHandler.encoding = UTF-8
diff --git a/modules/cli/src/main/resources/logback.xml b/modules/cli/src/main/resources/logback.xml
deleted file mode 100644
index d5fc3e8..0000000
--- a/modules/cli/src/main/resources/logback.xml
+++ /dev/null
@@ -1,31 +0,0 @@
-<!--
-  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.
--->
-
-<configuration>
-
-    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
-        <!-- encoders are assigned the type
-             ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
-        <encoder>
-            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
-        </encoder>
-    </appender>
-
-    <root level="error">
-        <appender-ref ref="STDOUT" />
-    </root>
-</configuration>
diff --git a/modules/cli/src/test/java/org/apache/ignite/cli/IgniteCliInterfaceTest.java b/modules/cli/src/test/java/org/apache/ignite/cli/IgniteCliInterfaceTest.java
index 98a9365..c7812eb 100644
--- a/modules/cli/src/test/java/org/apache/ignite/cli/IgniteCliInterfaceTest.java
+++ b/modules/cli/src/test/java/org/apache/ignite/cli/IgniteCliInterfaceTest.java
@@ -316,7 +316,7 @@ public class IgniteCliInterfaceTest extends AbstractCliTest {
             var node =
                 new NodeManager.RunningNode(1, nodeName, Path.of("logfile"));
 
-            when(nodeMgr.start(any(), any(), any(), any(), any()))
+            when(nodeMgr.start(any(), any(), any(), any(), any(), any()))
                 .thenReturn(node);
 
             when(cliPathsCfgLdr.loadIgnitePathsOrThrowError())
@@ -333,6 +333,7 @@ public class IgniteCliInterfaceTest extends AbstractCliTest {
                 ignitePaths.logDir,
                 ignitePaths.cliPidsDir(),
                 Path.of("conf.json"),
+                ignitePaths.serverJavaUtilLoggingPros(),
                 cli.getOut());
 
             assertEquals("\nNode is successfully started. To stop, type ignite node stop " + nodeName + "\n\n" +
diff --git a/modules/cli/src/test/resources/logback-test.xml b/modules/cli/src/test/resources/logback-test.xml
deleted file mode 100644
index d5fc3e8..0000000
--- a/modules/cli/src/test/resources/logback-test.xml
+++ /dev/null
@@ -1,31 +0,0 @@
-<!--
-  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.
--->
-
-<configuration>
-
-    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
-        <!-- encoders are assigned the type
-             ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
-        <encoder>
-            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
-        </encoder>
-    </appender>
-
-    <root level="error">
-        <appender-ref ref="STDOUT" />
-    </root>
-</configuration>