All Downloads are FREE. Search and download functionalities are using the official Maven repository.

echopraxia.noop.NoopCoreLogger Maven / Gradle / Ivy

The newest version!
package echopraxia.noop;

import static echopraxia.logging.spi.Utilities.joinFields;

import echopraxia.api.*;
import echopraxia.logging.api.Condition;
import echopraxia.logging.api.Level;
import echopraxia.logging.api.LoggerHandle;
import echopraxia.logging.spi.CoreLogger;
import echopraxia.logging.spi.LoggerContext;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class NoopCoreLogger implements CoreLogger {

  private final NoopLoggerContext context;
  private final Condition condition;
  private final String fqcn;

  private final Supplier tlsSupplier;

  public NoopCoreLogger(String fqcn) {
    this.fqcn = fqcn;
    this.context = NoopLoggerContext.empty();
    this.condition = Condition.always();
    this.tlsSupplier = () -> (Runnable) () -> {};
  }

  public NoopCoreLogger(
      String fqcn, NoopLoggerContext context, Condition condition, Supplier tlsSupplier) {
    this.fqcn = fqcn;
    this.context = context;
    this.condition = condition;
    this.tlsSupplier = tlsSupplier;
  }

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

  @Override
  public boolean isEnabled(@NotNull Level level) {
    return this.condition.test(
        level, new NoopLoggingContext(this, context::getLoggerFields, Collections::emptyList));
  }

  @Override
  public boolean isEnabled(@NotNull Level level, @NotNull Condition condition) {
    return this.condition
        .and(condition)
        .test(
            level, new NoopLoggingContext(this, context::getLoggerFields, Collections::emptyList));
  }

  @Override
  public boolean isEnabled(@NotNull Level level, @NotNull Supplier> extraFields) {
    return this.condition.test(
        level,
        new NoopLoggingContext(
            this, () -> context.withFields(extraFields).getLoggerFields(), Collections::emptyList));
  }

  @Override
  public boolean isEnabled(
      @NotNull Level level,
      @NotNull Condition condition,
      @NotNull Supplier> extraFields) {
    return this.condition
        .and(condition)
        .test(
            level,
            new NoopLoggingContext(
                this,
                () -> context.withFields(extraFields).getLoggerFields(),
                Collections::emptyList));
  }

  @Override
  public @NotNull Condition condition() {
    return condition;
  }

  @Override
  public @NotNull String fqcn() {
    return fqcn;
  }

  @NotNull
  public LoggerContext getLoggerContext() {
    return context;
  }

  @Override
  public @NotNull  CoreLogger withFields(
      @NotNull Function f, @NotNull FB builder) {
    NoopLoggerContext ctx =
        new NoopLoggerContext(
            joinFields(() -> context.getLoggerFields(), () -> convert(f.apply(builder))));
    return new NoopCoreLogger(fqcn, ctx, this.condition.and(condition), tlsSupplier);
  }

  private List convert(FieldBuilderResult input) {
    return input.fields();
  }

  @Override
  public @NotNull CoreLogger withThreadContext(
      @NotNull Function>, Supplier>> mapTransform) {
    return this;
  }

  @Override
  public @NotNull CoreLogger withThreadLocal(Supplier newSupplier) {
    return new NoopCoreLogger(fqcn, context, this.condition.and(condition), newSupplier);
  }

  @Override
  public @NotNull CoreLogger withCondition(@NotNull Condition condition) {
    return new NoopCoreLogger(fqcn, context, this.condition.and(condition), tlsSupplier);
  }

  @Override
  public @NotNull CoreLogger withFQCN(@NotNull String fqcn) {
    return new NoopCoreLogger(fqcn, context, condition, tlsSupplier);
  }

  // -----------------------------------------------------------------------

  @Override
  public void log(@NotNull Level level, @Nullable String message) {
    NoopLoggingContext memo =
        new NoopLoggingContext(this, context::getLoggerFields, Collections::emptyList);
    if (isEnabledFor(level) && this.condition.test(level, memo)) {
      List fields = memo.getFields();
      System.out.printf("" + message + " level %s fields %s\n", level, fields);
    }
  }

  @Override
  public void log(
      @NotNull Level level, @NotNull Supplier> extraFields, @Nullable String message) {
    NoopLoggingContext ctx =
        new NoopLoggingContext(this, context::getLoggerFields, Collections::emptyList);
    if (isEnabledFor(level) && this.condition.test(level, ctx)) {
      List fields = ctx.withFields(extraFields).getFields();
      System.out.printf("" + message + " level %s fields %s\n", level, fields);
    }
  }

  @Override
  public  void log(
      @NotNull Level level,
      @Nullable String message,
      @NotNull Function f,
      @NotNull FB builder) {
    List args = convert(f.apply(builder));
    if (isEnabledFor(level)) {
      NoopLoggingContext memo =
          new NoopLoggingContext(this, context::getLoggerFields, () -> f.apply(builder).fields());
      if (this.condition.test(level, memo)) {
        List fields = memo.getFields();
        System.out.printf("" + message + " level %s fields %s args %s\n", level, fields, args);
      }
    }
  }

  @Override
  public  void log(
      @NotNull Level level,
      @NotNull Supplier> extraFields,
      @Nullable String message,
      @NotNull Function f,
      @NotNull FB builder) {
    List args = convert(f.apply(builder));
    if (isEnabledFor(level)) {
      NoopLoggingContext memo =
          new NoopLoggingContext(this, context::getLoggerFields, () -> f.apply(builder).fields());
      if (this.condition.test(level, memo)) {
        List fields = memo.getFields();
        System.out.printf("" + message + " level %s fields %s args %s\n", level, fields, args);
      }
    }
  }

  @Override
  public void log(@NotNull Level level, @NotNull Condition condition, @Nullable String message) {
    NoopLoggingContext memo =
        new NoopLoggingContext(this, context::getLoggerFields, Collections::emptyList);
    if (isEnabledFor(level) && this.condition.and(condition).test(level, memo)) {
      List fields = memo.getLoggerFields();
      System.out.printf("" + message + " level %s fields %s\n", level, fields);
    }
  }

  @Override
  public void log(
      @NotNull Level level,
      @NotNull Supplier> extraFields,
      @NotNull Condition condition,
      @Nullable String message) {
    NoopLoggingContext memo =
        new NoopLoggingContext(this, context::getLoggerFields, Collections::emptyList);
    if (isEnabledFor(level) && this.condition.and(condition).test(level, memo)) {
      List fields = context.withFields(extraFields).getLoggerFields();
      System.out.printf("" + message + " level %s fields %s\n", level, fields);
    }
  }

  @Override
  public  void log(
      @NotNull Level level,
      @NotNull Condition condition,
      @Nullable String message,
      @NotNull Function f,
      @NotNull FB builder) {
    // When passing a condition through with explicit arguments, we pull the args and make
    // them available through context.
    NoopLoggingContext argContext =
        new NoopLoggingContext(
            NoopCoreLogger.this, context::getLoggerFields, () -> convert(f.apply(builder)));
    if (isEnabledFor(level) && this.condition.and(condition).test(level, argContext)) {
      System.out.printf(
          "" + message + " level %s fields %s args %s\n",
          level,
          argContext.getLoggerFields(),
          argContext.getArgumentFields());
    }
  }

  @Override
  public  void log(
      @NotNull Level level,
      @NotNull Supplier> extraFields,
      @NotNull Condition condition,
      @Nullable String message,
      @NotNull Function f,
      @NotNull FB builder) {
    if (isEnabledFor(level)) {
      NoopLoggingContext argContext =
          new NoopLoggingContext(
              NoopCoreLogger.this,
              () -> context.withFields(extraFields).getLoggerFields(),
              () -> convert(f.apply(builder)));
      if (this.condition.and(condition).test(level, argContext)) {
        System.out.printf(
            "" + message + " level %s fields %s args %s\n",
            level,
            argContext.getLoggerFields(),
            argContext.getArgumentFields());
      }
    }
  }

  @Override
  public @NotNull  LoggerHandle logHandle(@NotNull Level level, @NotNull FB builder) {
    return new LoggerHandle() {
      @Override
      public void log(@Nullable String message) {
        System.out.printf("" + message + " level %s fields %s\n", level, context.getLoggerFields());
      }

      @Override
      public void log(@Nullable String message, @NotNull Function f) {
        NoopLoggingContext ctx =
            new NoopLoggingContext(
                NoopCoreLogger.this, context::getLoggerFields, () -> convert(f.apply(builder)));
        System.out.printf(
            "" + message + " level %s fields %s args %s\n",
            level,
            ctx.getLoggerFields(),
            ctx.getArgumentFields());
      }
    };
  }

  private boolean isEnabledFor(Level level) {
    return true;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy