You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@samza.apache.org by GitBox <gi...@apache.org> on 2019/05/10 17:35:43 UTC

[GitHub] [samza] Sanil15 commented on a change in pull request #1029: Config logger

Sanil15 commented on a change in pull request #1029: Config logger
URL: https://github.com/apache/samza/pull/1029#discussion_r282975774
 
 

 ##########
 File path: samza-core/src/main/java/org/apache/samza/config/ConfigLogger.java
 ##########
 @@ -0,0 +1,71 @@
+package org.apache.samza.config;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.function.Consumer;
+
+/**
+ * This logger logs the config line by line, while shortening the lines which are too long (over maxLen)
+ * Logging preserves the original logger name and may be redirected to a different file
+ * by specifying a logger name that starts with ConfigLogger. in log4j configuration.
+ */
+public class ConfigLogger {
+  private final Logger log;
+  private final int maxLen;
+
+  public enum Level { INFO, DEBUG, WARN, ERROR}
+
+  public static ConfigLogger of(String loggerName, int maxLen) { return new ConfigLogger(loggerName, maxLen);}
+
+  private ConfigLogger(String loggerName, int maxLen) {
+    this.log = LoggerFactory.getLogger("ConfigLogger." + loggerName);
+    this.maxLen = maxLen;
+  }
+
+  /**
+   * log the config line by line
+   * @param header prepend the config with this header
+   * @param config config to log
+   * @param level at what level {@ConfigLogger.Leevel}
+   */
+  public void logConfig(String header, Config config, Level level) {
+    String msg = buildConfigString(header, maxLen, config);
+    log(log, msg, level);
+  }
+
+
+  // static api
+  public static void logConfig(String loggerName, String header, Config config, int maxLen, Level level) {
+    Logger logger = LoggerFactory.getLogger("ConfigLogger." + loggerName);
+    String msg = buildConfigString(header, maxLen, config);
+    log(logger, msg, level);
+  }
+
+  private static String buildConfigString(String header, int maxLen, Config config) {
+    StringBuilder bldr = new StringBuilder(header + "\n");
+    for (MapConfig.Entry e: config.entrySet()) {
+      String val;
+      if (e.getValue().toString().length() > maxLen) {
+        val = e.getValue().toString().substring(0, maxLen) + "<...>";
 
 Review comment:
   I am trying to understand the use case, does log4j does not automatically split lines that are too long?
   
   If not maxLen can't default?

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services