<commit_before>package de.sormuras.bach.util;

import java.text.MessageFormat;
import java.util.Collection;
import java.util.List;
import java.util.ResourceBundle;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.function.Function;
import java.util.stream.Collectors;

/** An entry-collecting system logger implementation. */
public /*static*/ class Logbook implements System.Logger {

  private final Collection<Entry> entries = new ConcurrentLinkedQueue<>();

  @Override
  public String getName() {
    return "Logbook";
  }

  @Override
  public boolean isLoggable(Level level) {
    return true;
  }

  @Override
  public void log(Level level, ResourceBundle bundle, String message, Throwable thrown) {
    entries.add(new Entry(level, message, thrown));
  }

  @Override
  public void log(Level level, ResourceBundle bundle, String pattern, Object... arguments) {
    entries.add(new Entry(level, MessageFormat.format(pattern, arguments), null));
  }

  public List<String> messages() {
    return lines(Entry::message);
  }

  public List<String> lines(Function<Entry, String> mapper) {
    return entries.stream().map(mapper).collect(Collectors.toList());
  }

  public static final class Entry {
    private final Level level;
    private final String message;
    private final Throwable thrown;

    public Entry(Level level, String message, Throwable thrown) {
      this.level = level;
      this.message = message;
      this.thrown = thrown;
    }

    public Level level() {
      return level;
    }

    public String message() {
      return message;
    }

    public Throwable thrown() {
      return thrown;
    }
  }
}
<commit_msg>🐞 Extend logbook with debug and dry-run flags<commit_after>package de.sormuras.bach.util;

import java.text.MessageFormat;
import java.util.Collection;
import java.util.List;
import java.util.ResourceBundle;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;

/** An entry-collecting system logger implementation. */
public /*static*/ class Logbook implements System.Logger {

  public static Logbook ofSystem() {
    var debug = Boolean.getBoolean("ebug") || "".equals(System.getProperty("ebug"));
    var dryRun = Boolean.getBoolean("ry-run") || "".equals(System.getProperty("ry-run"));
    return new Logbook(System.out::println, debug, dryRun);
  }

  private final Consumer<String> consumer;
  private final boolean debug;
  private final boolean dryRun;
  private final Collection<Entry> entries;

  public Logbook(Consumer<String> consumer, boolean debug, boolean dryRun) {
    this.consumer = consumer;
    this.debug = debug;
    this.dryRun = dryRun;
    this.entries = new ConcurrentLinkedQueue<>();
  }

  public boolean isDebug() {
    return debug;
  }

  public boolean isDryRun() {
    return dryRun;
  }

  @Override
  public String getName() {
    return "Logbook";
  }

  @Override
  public boolean isLoggable(Level level) {
    if (level == Level.ALL) return isDebug();
    if (level == Level.OFF) return isDryRun();
    return true;
  }

  @Override
  public void log(Level level, ResourceBundle bundle, String message, Throwable thrown) {
    entries.add(new Entry(level, message, thrown));
  }

  @Override
  public void log(Level level, ResourceBundle bundle, String pattern, Object... arguments) {
    entries.add(new Entry(level, MessageFormat.format(pattern, arguments), null));
  }

  public List<String> messages() {
    return lines(entry -> entry.message);
  }

  public List<String> lines(Function<Entry, String> mapper) {
    return entries.stream().map(mapper).collect(Collectors.toList());
  }

  public final class Entry {
    private final Level level;
    private final String message;
    private final Throwable thrown;

    public Entry(Level level, String message, Throwable thrown) {
      this.level = level;
      this.message = message;
      this.thrown = thrown;
      if (debug) consumer.accept(message);
    }

    @Override
    public String toString() {
      if (thrown == null) return level + "|" + message;
      return level + "|" + message + " -> " + thrown;
    }
  }
}

{'fraction_non_alphanumeric': 0.11285340936089332, 'fraction_numerical': 0.0, 'mean_word_length': 3.6777777777777776, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1564756', 'n_tokens_mistral': 1217, 'n_tokens_neox': 1191, 'n_words': 342}