streamlined logging
- log via logger, if stdout, just wrap in Handler - logger instance property since handlers are being removed/ added - java bundled logger uses MessageFormat, so add indexes in curly braces
This commit is contained in:
		@@ -11,8 +11,10 @@ import java.time.Duration;
 | 
			
		||||
import java.time.ZonedDateTime;
 | 
			
		||||
import java.time.temporal.ChronoUnit;
 | 
			
		||||
import java.util.concurrent.TimeUnit;
 | 
			
		||||
import java.util.function.Supplier;
 | 
			
		||||
import java.util.logging.Handler;
 | 
			
		||||
import java.util.logging.Level;
 | 
			
		||||
import java.util.logging.SimpleFormatter;
 | 
			
		||||
import java.util.logging.StreamHandler;
 | 
			
		||||
 | 
			
		||||
@SuppressWarnings("unused")
 | 
			
		||||
public class FabianUtil {
 | 
			
		||||
@@ -116,17 +118,16 @@ public class FabianUtil {
 | 
			
		||||
 | 
			
		||||
   public static class ProgressHandler {
 | 
			
		||||
 | 
			
		||||
      private static final Logger LOG = new Logger(ProgressHandler.class);
 | 
			
		||||
      private static final DecimalFormat TIME_DECIMAL_FORMAT = new DecimalFormat("00");
 | 
			
		||||
      protected static final int DEFAULT_LOG_DISTANCE = 50;
 | 
			
		||||
      protected static final int DEFAULT_TIME_LOG_DISTANCE = 15;
 | 
			
		||||
 | 
			
		||||
      protected final Logger log;
 | 
			
		||||
      private final ZonedDateTime startTime;
 | 
			
		||||
      private final BigDecimal logDistance;
 | 
			
		||||
      private final int timeLogDistance;
 | 
			
		||||
      private BigDecimal counter = BigDecimal.ZERO;
 | 
			
		||||
      private ZonedDateTime lastLoggedAt;
 | 
			
		||||
      protected final PrintStream printer;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
      public ProgressHandler() {
 | 
			
		||||
@@ -144,11 +145,17 @@ public class FabianUtil {
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
      public ProgressHandler(int logDistance, int timeLogDistance, PrintStream printer) {
 | 
			
		||||
      public ProgressHandler(int logDistance, int timeLogDistance, OutputStream printer) {
 | 
			
		||||
         startTime = ZonedDateTime.now();
 | 
			
		||||
         this.logDistance = validateBD(logDistance, "Log-Distanz");
 | 
			
		||||
         this.timeLogDistance = validateInt(timeLogDistance, "Zeitbasierte Log-Distanz");
 | 
			
		||||
         this.printer = printer;
 | 
			
		||||
         log = new Logger(getClass());
 | 
			
		||||
         if (printer != null) {
 | 
			
		||||
            for (Handler handler : log.getHandlers()) {
 | 
			
		||||
               log.removeHandler(handler);
 | 
			
		||||
            }
 | 
			
		||||
            log.addHandler(new StreamHandler(printer, new SimpleFormatter()));
 | 
			
		||||
         }
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -165,11 +172,6 @@ public class FabianUtil {
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
      protected Logger logger() {
 | 
			
		||||
         return LOG;
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
      protected final synchronized BigDecimal counter() {
 | 
			
		||||
         return counter;
 | 
			
		||||
      }
 | 
			
		||||
@@ -205,7 +207,7 @@ public class FabianUtil {
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
      protected synchronized boolean shouldLog() {
 | 
			
		||||
         return (printer != null || logger().isLoggable(Level.INFO)) && (counter.remainder(logDistance).signum() == 0 || isBored());
 | 
			
		||||
         return log.isLoggable(Level.INFO) && (counter.remainder(logDistance).signum() == 0 || isBored());
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -222,11 +224,7 @@ public class FabianUtil {
 | 
			
		||||
 | 
			
		||||
      public synchronized void log() {
 | 
			
		||||
         Duration timePassed = getTimePassed();
 | 
			
		||||
         if (printer != null) {
 | 
			
		||||
            printer.println(counter + " Stück erledigt. " + formatDuration(timePassed) + " vergangen.");
 | 
			
		||||
         } else {
 | 
			
		||||
            logger().info("{} Stück erledigt. {} vergangen.", counter, formatDuration(timePassed));
 | 
			
		||||
         }
 | 
			
		||||
         log.info("{0} Stück erledigt. {1} vergangen.", counter, formatDuration(timePassed));
 | 
			
		||||
         updateLastLoggedAt();
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
@@ -253,18 +251,13 @@ public class FabianUtil {
 | 
			
		||||
 | 
			
		||||
      public synchronized void logFinal() {
 | 
			
		||||
         Duration timePassed = getTimePassed();
 | 
			
		||||
         if (printer != null) {
 | 
			
		||||
            printer.println(counter + " Stück (100 %) erledigt. " + formatDuration(timePassed) + " vergangen.");
 | 
			
		||||
         } else {
 | 
			
		||||
            logger().info("{} Stück (100 %) erledigt. {} vergangen.", counter, formatDuration(timePassed));
 | 
			
		||||
         }
 | 
			
		||||
         log.info("{0} Stück (100 %) erledigt. {1} vergangen.", counter, formatDuration(timePassed));
 | 
			
		||||
      }
 | 
			
		||||
   }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
   public static class ProgressHandlerWithTotal extends ProgressHandler {
 | 
			
		||||
 | 
			
		||||
      private static final Logger LOG = new Logger(ProgressHandlerWithTotal.class);
 | 
			
		||||
      private static final DecimalFormat PERCENTAGE_FORMAT = new DecimalFormat("00.00");
 | 
			
		||||
      private static final BigDecimal ONE_HUNDRED_PERCENT = BigDecimal.valueOf(100);
 | 
			
		||||
 | 
			
		||||
@@ -286,18 +279,12 @@ public class FabianUtil {
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
      public ProgressHandlerWithTotal(int logDistance, int timeLogDistance, int total, PrintStream printer) {
 | 
			
		||||
      public ProgressHandlerWithTotal(int logDistance, int timeLogDistance, int total, OutputStream printer) {
 | 
			
		||||
         super(logDistance, timeLogDistance, printer);
 | 
			
		||||
         this.total = validateBD(total, "Gesamt-Anzahl");
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
      @Override
 | 
			
		||||
      protected Logger logger() {
 | 
			
		||||
         return LOG;
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
      @Override
 | 
			
		||||
      protected synchronized boolean shouldLog() {
 | 
			
		||||
         return super.shouldLog() && counter().compareTo(total) <= 0;
 | 
			
		||||
@@ -320,13 +307,8 @@ public class FabianUtil {
 | 
			
		||||
         }
 | 
			
		||||
 | 
			
		||||
         String counterPrint = total.compareTo(BigDecimal.TEN) >= 0 ? formatNumber(counter) : counter.toString();
 | 
			
		||||
         if (printer != null) {
 | 
			
		||||
            printer.println(counterPrint + " Stück von " + total + " (" + PERCENTAGE_FORMAT.format(percentage) + " %) erledigt. "
 | 
			
		||||
                + formatDuration(timePassed) + " vergangen" + remainingTimePart + ".");
 | 
			
		||||
         } else {
 | 
			
		||||
            logger().info("{} Stück von {} ({} %) erledigt. {} vergangen{}.",
 | 
			
		||||
                counterPrint, total, PERCENTAGE_FORMAT.format(percentage), formatDuration(timePassed), remainingTimePart);
 | 
			
		||||
         }
 | 
			
		||||
         log.info("{0} Stück von {1} ({2} %) erledigt. {3} vergangen{4}.",
 | 
			
		||||
             counterPrint, total, PERCENTAGE_FORMAT.format(percentage), formatDuration(timePassed), remainingTimePart);
 | 
			
		||||
         updateLastLoggedAt();
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user