kinow commented on code in PR #492:
URL: https://github.com/apache/opennlp/pull/492#discussion_r1116622099


##########
opennlp-tools/src/main/java/opennlp/tools/parser/Parse.java:
##########
@@ -319,32 +323,25 @@ public void insert(final Parse constituent) {
       int pn = parts.size();
       for (; pi < pn; pi++) {
         Parse subPart = parts.get(pi);
-        //System.err.println("Parse.insert:con="+constituent+" sp["+pi+"] 
"+subPart+" "+subPart.getType());

Review Comment:
   +1 too. Especially to `trace`. Before since the output of tests & tools was 
all in `out`/`err` sometimes I would just ignore it as it was too much to parse 
it (which could possibly hide some errors). Having some like this in a separate 
level is extremely helpful :pray: :bow: Thanks!



##########
opennlp-tools/src/main/java/opennlp/tools/log/LogPrintStream.java:
##########
@@ -0,0 +1,87 @@
+/*
+ * 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 opennlp.tools.log;
+
+import java.io.PrintStream;
+import java.util.Objects;
+
+import org.slf4j.Logger;
+import org.slf4j.event.Level;
+
+import opennlp.tools.commons.Internal;
+
+/**
+ * This class serves as an adapter for a {@link Logger} used withing a {@link 
PrintStream}.
+ */
+@Internal
+public class LogPrintStream extends PrintStream {
+
+  private final Logger logger;
+  private final Level level;
+
+  /**
+   * Creates a {@link LogPrintStream} for the given {@link Logger}
+   *
+   * @param logger must not be {@code null}
+   */
+  public LogPrintStream(Logger logger) {
+    this(logger, Level.INFO);
+  }
+
+  /**
+   * Creates a {@link LogPrintStream} for the given {@link Logger}, which logs 
at the specified
+   * {@link Level level}.
+   *
+   * @param logger must not be {@code null}
+   * @param level  must not be {@code null}
+   */
+  public LogPrintStream(Logger logger, Level level) {
+    super(nullOutputStream());
+    Objects.requireNonNull(logger, "logger must not be NULL.");
+    Objects.requireNonNull(level, "log level must not be NULL.");
+    this.logger = logger;
+    this.level = level;
+  }
+
+  @Override
+  public PrintStream printf(String format, Object... args) {
+    log(String.format(format, args));
+    return this;
+  }
+
+  @Override
+  public void println(String msg) {
+    log(msg);
+  }
+
+  private void log(String msg) {
+    switch (level) {
+      case TRACE:
+        logger.trace(msg);
+      case DEBUG:
+        logger.debug(msg);
+      case INFO:
+        logger.info(msg);
+      case WARN:
+        logger.warn(msg);
+      case ERROR:
+        logger.error(msg);
+    }

Review Comment:
   #TodayILearned 
   
   @rzo1 I think this switch is still missing the `break` ? :car: 
   
   ```java
   public static void main(String[] args) {
           int level = 0;
           switch (level) {
               case 0:
                   System.out.print("TRACE ");
               case 1:
                   System.out.print("WARN ");
               case 2:
                   System.out.print("INFO ");
               case 3:
                   System.out.print("DEBUG");
           }
       }
   ```
   
   Prints `TRACE WARN INFO DEBUG`, so I think in the end this would call all 
the `logger.*` methods?



##########
opennlp-tools/src/main/java/opennlp/tools/ml/maxent/GISTrainer.java:
##########
@@ -407,9 +399,9 @@ public GISModel trainModel(int iterations, DataIndexer di, 
Prior modelPrior, int
     prior.setLabels(outcomeLabels, predLabels);
     numPreds = predLabels.length;
 
-    display("\tNumber of Event Tokens: " + numUniqueEvents + "\n");
-    display("\t    Number of Outcomes: " + numOutcomes + "\n");
-    display("\t  Number of Predicates: " + numPreds + "\n");
+    logger.info("Number of Event Tokens: {}", numUniqueEvents);

Review Comment:
   +1 for either one-liner/group too, as long as the information is there in 
the logs as appears as a single block without risks of other threads logging 
anything :+1: 



-- 
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: [email protected]

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

Reply via email to