com.tersesystems.echopraxia.async.AsyncLogger Maven / Gradle / Ivy
Show all versions of async Show documentation
package com.tersesystems.echopraxia.async;
import com.tersesystems.echopraxia.api.*;
import com.tersesystems.echopraxia.spi.AbstractLoggerSupport;
import com.tersesystems.echopraxia.spi.CoreLogger;
import java.util.concurrent.Executor;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* An asynchronous echopraxia logger built around a LoggerHandle.
*
* This class is useful when evaluating a condition would unreasonably block or otherwise be too
* expensive to run inline with a business operation.
*
*
Instances of this class are usually created from {@code
* AsyncLoggerFactory.getLogger().withExecutor(executor))}.
*
* @param the field builder type
*/
public class AsyncLogger extends AbstractLoggerSupport, FB>
implements DefaultAsyncLoggerMethods {
protected AsyncLogger(@NotNull CoreLogger core, @NotNull FB fieldBuilder) {
super(core, fieldBuilder, AsyncLogger.class);
}
/**
* Creates a new logger with the given field builder.
*
* @param newBuilder the given field builder.
* @param the type of the field builder.
* @return a new logger using the given field builder.
*/
@NotNull
public AsyncLogger withFieldBuilder(@NotNull T newBuilder) {
return newLogger(newBuilder);
}
/**
* Provides a function to be run in the async logger to set up thread local storage variables in
* the logging executor's thread. Any existing function on the core logger is composed with the
* given function.
*
* The method to supply is in two parts, with the supply portion run to save off the TLS
* variables, and the runnable portion applying the TLS variables in the thread:
*
* @param supplier the function to apply to manage TLS state.
* @return the logger with the thread local supplier applied.
*/
public AsyncLogger withThreadLocal(Supplier supplier) {
// Supplier s = () -> {
// // Run before async thread execution
// final RequestAttributes requestAttributes =
// RequestContextHolder.currentRequestAttributes();
// // runnable.run() is called in the logging thread.
// return () -> {
// RequestContextHolder.setRequestAttributes(requestAttributes);
// };
// };
return newLogger(core.withThreadLocal(supplier));
}
/**
* Creates a new async logger with a new executor.
*
* @param executor the new executor.
* @return the new async logger.
*/
public AsyncLogger withExecutor(Executor executor) {
return newLogger(core().withExecutor(executor));
}
protected @NotNull AsyncLogger newLogger(CoreLogger coreLogger) {
return new AsyncLogger<>(coreLogger, fieldBuilder());
}
@NotNull
protected AsyncLogger newLogger(@NotNull T fieldBuilder) {
if (this.fieldBuilder == fieldBuilder) {
//noinspection unchecked
return (AsyncLogger) this;
}
return new AsyncLogger<>(core(), fieldBuilder);
}
@Override
@NotNull
protected AsyncLogger neverLogger() {
return new NeverAsyncLogger<>(core().withCondition(Condition.never()), fieldBuilder);
}
// This must extend AsyncLogger so the return type is the same
private static class NeverAsyncLogger extends AsyncLogger {
protected NeverAsyncLogger(@NotNull CoreLogger core, @NotNull FB fieldBuilder) {
super(core, fieldBuilder);
}
protected @NotNull AsyncLogger newLogger(CoreLogger coreLogger) {
return this;
}
@Override
public @NotNull AsyncLogger withThreadContext() {
return this;
}
@Override
public @NotNull AsyncLogger withFieldBuilder(@NotNull T newBuilder) {
return new NeverAsyncLogger(core, newBuilder);
}
@Override
public @NotNull AsyncLogger withCondition(@NotNull Condition condition) {
return this;
}
public void trace(@NotNull Consumer> consumer) {}
/**
* Logs using a condition and a logger handle at TRACE level.
*
* @param c the condition
* @param consumer the consumer of the logger handle.
*/
public void trace(@NotNull Condition c, @NotNull Consumer> consumer) {}
@Override
public void trace(@Nullable String message) {}
@Override
public void trace(@Nullable String message, @NotNull Function f) {}
@Override
public void trace(@Nullable String message, @NotNull Throwable e) {}
@Override
public void trace(@NotNull Condition condition, @Nullable String message) {}
@Override
public void trace(
@NotNull Condition condition,
@Nullable String message,
@NotNull Function f) {}
@Override
public void trace(
@NotNull Condition condition, @Nullable String message, @NotNull Throwable e) {}
/**
* Logs using a logger handle at DEBUG level.
*
* @param consumer the consumer of the logger handle.
*/
public void debug(@NotNull Consumer> consumer) {}
/**
* Logs using a condition and a logger handle at DEBUG level.
*
* @param c the condition
* @param consumer the consumer of the logger handle.
*/
public void debug(@NotNull Condition c, @NotNull Consumer> consumer) {}
@Override
public void debug(@Nullable String message) {}
@Override
public void debug(@Nullable String message, @NotNull Function f) {}
@Override
public void debug(@Nullable String message, @NotNull Throwable e) {}
@Override
public void debug(@NotNull Condition condition, @Nullable String message) {}
@Override
public void debug(
@NotNull Condition condition,
@Nullable String message,
@NotNull Function f) {}
@Override
public void debug(
@NotNull Condition condition, @Nullable String message, @NotNull Throwable e) {}
/**
* Logs using a logger handle at INFO level.
*
* @param consumer the consumer of the logger handle.
*/
public void info(@NotNull Consumer> consumer) {}
/**
* Logs using a condition and a logger handle at INFO level.
*
* @param c the condition
* @param consumer the consumer of the logger handle.
*/
public void info(@NotNull Condition c, @NotNull Consumer> consumer) {}
@Override
public void info(@Nullable String message) {}
@Override
public void info(@Nullable String message, @NotNull Function f) {}
@Override
public void info(@Nullable String message, @NotNull Throwable e) {}
@Override
public void info(@NotNull Condition condition, @Nullable String message) {}
@Override
public void info(
@NotNull Condition condition,
@Nullable String message,
@NotNull Function f) {}
@Override
public void info(
@NotNull Condition condition, @Nullable String message, @NotNull Throwable e) {}
/**
* Logs using a logger handle at WARN level.
*
* @param consumer the consumer of the logger handle.
*/
public void warn(@NotNull Consumer> consumer) {}
/**
* Logs using a condition and a logger handle at WARN level.
*
* @param c the condition
* @param consumer the consumer of the logger handle.
*/
public void warn(@NotNull Condition c, @NotNull Consumer> consumer) {}
@Override
public void warn(@Nullable String message, @NotNull Function f) {}
@Override
public void warn(@Nullable String message, @NotNull Throwable e) {}
@Override
public void warn(@NotNull Condition condition, @Nullable String message) {}
@Override
public void warn(
@NotNull Condition condition,
@Nullable String message,
@NotNull Function f) {}
@Override
public void warn(
@NotNull Condition condition, @Nullable String message, @NotNull Throwable e) {}
/**
* Logs using a logger handle at ERROR level.
*
* @param consumer the consumer of the logger handle.
*/
@Override
public void error(@NotNull Consumer> consumer) {}
/**
* Logs using a condition and a logger handle at ERROR level.
*
* @param c the condition
* @param consumer the consumer of the logger handle.
*/
@Override
public void error(@NotNull Condition c, @NotNull Consumer> consumer) {}
@Override
public void error(@Nullable String message) {}
@Override
public void error(@Nullable String message, @NotNull Function f) {}
@Override
public void error(@Nullable String message, @NotNull Throwable e) {}
@Override
public void error(@NotNull Condition condition, @Nullable String message) {}
@Override
public void error(
@NotNull Condition condition,
@Nullable String message,
@NotNull Function f) {}
@Override
public void error(
@NotNull Condition condition, @Nullable String message, @NotNull Throwable e) {}
}
}