exceptionfactory commented on code in PR #8407:
URL: https://github.com/apache/nifi/pull/8407#discussion_r1512922173


##########
nifi-nar-bundles/nifi-py4j-bundle/nifi-py4j-bridge/src/main/java/org/apache/nifi/py4j/PythonProcessLogReader.java:
##########
@@ -0,0 +1,163 @@
+/*
+ * 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.nifi.py4j;
+
+import org.apache.nifi.py4j.logging.PythonLogLevel;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.LinkedList;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Queue;
+import java.util.stream.Collectors;
+
+/**
+ * Runnable Command for reading a line from Process Output Stream and writing 
to a Logger
+ */
+class PythonProcessLogReader implements Runnable {
+    private static final int LOG_LEVEL_BEGIN_INDEX = 0;
+
+    private static final int LOG_LEVEL_END_INDEX = 2;
+
+    private static final int MESSAGE_BEGIN_INDEX = 3;
+
+    private static final char NAME_MESSAGE_SEPARATOR = ':';
+
+    private static final int MINIMUM_LOGGER_NAME_INDEX = 3;
+
+    private static final String LOG_PREFIX = "PY4JLOG";
+
+    private static final int PREFIXED_LOG_LEVEL_BEGIN_INDEX = 8;
+
+    private static final String LINE_SEPARATOR = System.lineSeparator();
+
+    private static final Map<String, PythonLogLevel> PYTHON_LOG_LEVELS = 
Arrays.stream(PythonLogLevel.values()).collect(
+            Collectors.toUnmodifiableMap(
+                    pythonLogLevel -> 
Integer.toString(pythonLogLevel.getLevel()),
+                    pythonLogLevel -> pythonLogLevel
+            )
+    );
+
+    private final Logger processLogger = 
LoggerFactory.getLogger("org.apache.nifi.py4j.ProcessLog");
+
+    private final BufferedReader processReader;
+
+    /**
+     * Standard constructor with Buffered Reader connected to Python Process 
Output Stream
+     *
+     * @param processReader Reader from Process Output Stream
+     */
+    PythonProcessLogReader(final BufferedReader processReader) {
+        this.processReader = Objects.requireNonNull(processReader, "Reader 
required");
+    }
+
+    /**
+     * Read lines from Process Reader and write log messages based on parsed 
level and named logger
+     */
+    @Override
+    public void run() {
+        final Queue<ParsedRecord> parsedRecords = new LinkedList<>();
+
+        try {
+            String line = processReader.readLine();
+            while (line != null) {
+                processLine(line, parsedRecords);
+
+                if (parsedRecords.size() == 2 || !processReader.ready()) {
+                    final ParsedRecord parsedRecord = parsedRecords.remove();
+                    log(parsedRecord);
+                }

Review Comment:
   Thanks for the thorough feedback and testing. I made a change to check the 
readiness of the Process Reader separately, and then the example `LogContents` 
Processor writes the messages as expected.



-- 
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: issues-unsubscr...@nifi.apache.org

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

Reply via email to