zentol commented on a change in pull request #11839:
URL: https://github.com/apache/flink/pull/11839#discussion_r414418912



##########
File path: flink-dist/src/main/flink-bin/bin/flink-console.sh
##########
@@ -58,7 +58,18 @@ esac
 
 FLINK_TM_CLASSPATH=`constructFlinkClassPath`
 
-log_setting=("-Dlog4j.configuration=file:${FLINK_CONF_DIR}/log4j-console.properties"
 "-Dlog4j.configurationFile=file:${FLINK_CONF_DIR}/log4j-console.properties" 
"-Dlogback.configurationFile=file:${FLINK_CONF_DIR}/logback-console.xml")
+if [ "$FLINK_IDENT_STRING" = "" ]; then
+    FLINK_IDENT_STRING="$USER"
+fi
+
+RANDOM_ID=$(echo $RANDOM | tr '[0-9]' '[a-z]')
+
+FLINK_LOG_PREFIX="${FLINK_LOG_DIR}/flink-${FLINK_IDENT_STRING}-${SERVICE}-${RANDOM_ID}-${HOSTNAME}"

Review comment:
       this should use the same logic as `flink-daemon.sh`

##########
File path: 
flink-runtime/src/main/java/org/apache/flink/runtime/util/StdOutErrRedirector.java
##########
@@ -0,0 +1,199 @@
+/*
+ * 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.
+ */
+
+package org.apache.flink.runtime.util;
+
+import org.apache.flink.annotation.VisibleForTesting;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.PrintStream;
+
+/**
+ * Redirect std out and std err to logger.
+ */
+public class StdOutErrRedirector {
+
+       private static final String STD_OUT_FILE_PROPERTY_KEY = "stdout.file";
+       private static final String STD_ERR_FILE_PROPERTY_KEY = "stderr.file";
+
+       private static final String STD_OUT_LOGGER_NAME = 
"StdOutErrRedirector.StdOut";
+       private static final String STD_ERR_LOGGER_NAME = 
"StdOutErrRedirector.StdErr";
+
+       private static final Logger STD_OUT_LOGGER = 
LoggerFactory.getLogger(STD_OUT_LOGGER_NAME);
+       private static final Logger STD_ERR_LOGGER = 
LoggerFactory.getLogger(STD_ERR_LOGGER_NAME);
+
+       private static final ThreadLocal<Boolean> isRedirecting = 
ThreadLocal.withInitial(() -> false);
+
+       /**
+        * Try to redirect stdout and stderr.
+        */
+       public static void redirectStdOutErr() {
+               if (System.getProperty(STD_OUT_FILE_PROPERTY_KEY) != null) {
+                       System.setOut(createLoggerProxy(STD_OUT_LOGGER, 
System.out));
+               }
+
+               if (System.getProperty(STD_ERR_FILE_PROPERTY_KEY) != null) {
+                       System.setErr(createLoggerProxy(STD_ERR_LOGGER, 
System.err));
+               }
+       }
+
+       /**
+        * Create logger proxy print stream.
+        * Do not check null, keep the same behavior with System.out/err.
+        *
+        * @param originalPrintStream the original print stream
+        * @return the proxy print stream
+        */
+       @VisibleForTesting
+       static PrintStream createLoggerProxy(final Logger logger, final 
PrintStream originalPrintStream) {

Review comment:
       change the Logger argument to a `Consumer<String>`, pass 
`STD_(OUT|ERR)_LOGGER::info` as method reference, remove TestingLogger.

##########
File path: 
flink-runtime/src/test/java/org/apache/flink/runtime/util/StdOutErrRedirectorTest.java
##########
@@ -0,0 +1,456 @@
+/*
+ * 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.
+ */
+
+package org.apache.flink.runtime.util;
+
+import org.apache.flink.util.TestLogger;
+
+import org.hamcrest.Matchers;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.Marker;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.PrintStream;
+import java.io.UnsupportedEncodingException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.function.Consumer;
+
+import static org.apache.flink.core.testutils.CommonTestUtils.assertThrows;
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThat;
+
+/**
+ * Unit test for StdOutErrRedirector.
+ */
+public class StdOutErrRedirectorTest extends TestLogger {
+
+       private ByteArrayOutputStream byteArrayOutputStream;
+       private PrintStream originalStream;
+
+       @Before
+       public void setUp() throws UnsupportedEncodingException {
+               byteArrayOutputStream = new ByteArrayOutputStream();
+               originalStream = new PrintStream(byteArrayOutputStream, true, 
"UTF-8");
+       }
+
+       @After
+       public void tearDown() throws IOException {
+               byteArrayOutputStream.close();
+               originalStream.close();
+       }
+
+       @Test
+       public void testNormalPrint() {
+               final int intMsg = 1234;
+               final String strMsg = "some message";
+               final List<String> logContents = new ArrayList<>();
+
+               final Logger logger = new TestingLogger(
+                       msg -> {
+                               assertThat(StdOutErrRedirector.isRedirecting(), 
is(true));
+                               logContents.add(msg);
+                       }
+               );
+
+               final PrintStream proxyStream = 
StdOutErrRedirector.createLoggerProxy(logger, originalStream);
+
+               assertFalse(StdOutErrRedirector.isRedirecting());
+
+               proxyStream.print(intMsg);
+               assertThat(StdOutErrRedirector.isRedirecting(), is(false));
+               proxyStream.println(strMsg);
+               assertThat(StdOutErrRedirector.isRedirecting(), is(false));
+
+               assertThat(logContents.size(), is(3));
+               assertThat(logContents, 
Matchers.containsInAnyOrder(String.valueOf(intMsg), strMsg, 
System.lineSeparator()));
+       }
+
+       @Test
+       public void testRecursivePrint() throws Exception {
+               final String recursive = "recursive";
+               final Logger logger = new TestingLogger();
+
+               final PrintStream proxyStream = 
StdOutErrRedirector.createLoggerProxy(logger, originalStream);
+               ((TestingLogger) logger).setMsgConsumer(
+                       msg -> {
+                               assertThat(StdOutErrRedirector.isRedirecting(), 
is(true));
+                               proxyStream.print(recursive);

Review comment:
       ```suggestion
                                proxyStream.print(msg);
   ```

##########
File path: 
flink-runtime/src/test/java/org/apache/flink/runtime/util/StdOutErrRedirectorTest.java
##########
@@ -0,0 +1,456 @@
+/*
+ * 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.
+ */
+
+package org.apache.flink.runtime.util;
+
+import org.apache.flink.util.TestLogger;
+
+import org.hamcrest.Matchers;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.Marker;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.PrintStream;
+import java.io.UnsupportedEncodingException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.function.Consumer;
+
+import static org.apache.flink.core.testutils.CommonTestUtils.assertThrows;
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThat;
+
+/**
+ * Unit test for StdOutErrRedirector.
+ */
+public class StdOutErrRedirectorTest extends TestLogger {
+
+       private ByteArrayOutputStream byteArrayOutputStream;
+       private PrintStream originalStream;
+
+       @Before
+       public void setUp() throws UnsupportedEncodingException {
+               byteArrayOutputStream = new ByteArrayOutputStream();
+               originalStream = new PrintStream(byteArrayOutputStream, true, 
"UTF-8");
+       }
+
+       @After
+       public void tearDown() throws IOException {
+               byteArrayOutputStream.close();
+               originalStream.close();
+       }
+
+       @Test
+       public void testNormalPrint() {
+               final int intMsg = 1234;
+               final String strMsg = "some message";
+               final List<String> logContents = new ArrayList<>();
+
+               final Logger logger = new TestingLogger(
+                       msg -> {
+                               assertThat(StdOutErrRedirector.isRedirecting(), 
is(true));
+                               logContents.add(msg);
+                       }
+               );
+
+               final PrintStream proxyStream = 
StdOutErrRedirector.createLoggerProxy(logger, originalStream);
+
+               assertFalse(StdOutErrRedirector.isRedirecting());
+
+               proxyStream.print(intMsg);
+               assertThat(StdOutErrRedirector.isRedirecting(), is(false));
+               proxyStream.println(strMsg);
+               assertThat(StdOutErrRedirector.isRedirecting(), is(false));
+
+               assertThat(logContents.size(), is(3));
+               assertThat(logContents, 
Matchers.containsInAnyOrder(String.valueOf(intMsg), strMsg, 
System.lineSeparator()));

Review comment:
       the order should be important

##########
File path: flink-dist/src/main/flink-bin/bin/flink-console.sh
##########
@@ -58,7 +58,18 @@ esac
 
 FLINK_TM_CLASSPATH=`constructFlinkClassPath`
 
-log_setting=("-Dlog4j.configuration=file:${FLINK_CONF_DIR}/log4j-console.properties"
 "-Dlog4j.configurationFile=file:${FLINK_CONF_DIR}/log4j-console.properties" 
"-Dlogback.configurationFile=file:${FLINK_CONF_DIR}/logback-console.xml")
+if [ "$FLINK_IDENT_STRING" = "" ]; then
+    FLINK_IDENT_STRING="$USER"
+fi
+
+RANDOM_ID=$(echo $RANDOM | tr '[0-9]' '[a-z]')
+
+FLINK_LOG_PREFIX="${FLINK_LOG_DIR}/flink-${FLINK_IDENT_STRING}-${SERVICE}-${RANDOM_ID}-${HOSTNAME}"
+log="${FLINK_LOG_PREFIX}.log"
+out="${FLINK_LOG_PREFIX}.out"
+err="${FLINK_LOG_PREFIX}.err"

Review comment:
       why are we introducing a dedicated `.err` file? AFAIK no other script 
has it, why should this one?

##########
File path: 
flink-runtime/src/test/java/org/apache/flink/runtime/util/StdOutErrRedirectorTest.java
##########
@@ -0,0 +1,456 @@
+/*
+ * 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.
+ */
+
+package org.apache.flink.runtime.util;
+
+import org.apache.flink.util.TestLogger;
+
+import org.hamcrest.Matchers;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.Marker;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.PrintStream;
+import java.io.UnsupportedEncodingException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.function.Consumer;
+
+import static org.apache.flink.core.testutils.CommonTestUtils.assertThrows;
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThat;
+
+/**
+ * Unit test for StdOutErrRedirector.
+ */
+public class StdOutErrRedirectorTest extends TestLogger {
+
+       private ByteArrayOutputStream byteArrayOutputStream;
+       private PrintStream originalStream;
+
+       @Before
+       public void setUp() throws UnsupportedEncodingException {
+               byteArrayOutputStream = new ByteArrayOutputStream();
+               originalStream = new PrintStream(byteArrayOutputStream, true, 
"UTF-8");
+       }
+
+       @After
+       public void tearDown() throws IOException {
+               byteArrayOutputStream.close();
+               originalStream.close();
+       }
+
+       @Test
+       public void testNormalPrint() {
+               final int intMsg = 1234;
+               final String strMsg = "some message";
+               final List<String> logContents = new ArrayList<>();
+
+               final Logger logger = new TestingLogger(
+                       msg -> {
+                               assertThat(StdOutErrRedirector.isRedirecting(), 
is(true));
+                               logContents.add(msg);
+                       }
+               );
+
+               final PrintStream proxyStream = 
StdOutErrRedirector.createLoggerProxy(logger, originalStream);
+
+               assertFalse(StdOutErrRedirector.isRedirecting());

Review comment:
       use assertThat for consistency with the rest of the file




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to