Last active
March 5, 2018 22:53
-
-
Save splatch/c94f0f961d7444fd5920bec9cb79c918 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// I don't know it makes any sense at all, it's just a random rant in code. | |
import org.slf4j.Logger; | |
import java.util.Arrays; | |
import java.util.concurrent.ExecutorService; | |
import java.util.concurrent.Executors; | |
import java.util.function.BiConsumer; | |
import java.util.function.Supplier; | |
class FunctionalLogger { | |
private final Logger delegate; | |
FunctionalLogger(Logger delegate) { | |
this.delegate = delegate; | |
} | |
public void info(String message, Supplier<String>... args) { | |
info(() -> message, args); | |
} | |
public void info(Supplier<String> message, Supplier<String>... args) { | |
log(delegate::isInfoEnabled, delegate::info, message, args); | |
} | |
public void debug(String message, Supplier<String>... args) { | |
debug(() -> message, args); | |
} | |
public void debug(Supplier<String> message, Supplier<String>... args) { | |
log(delegate::isDebugEnabled, delegate::debug, message, args); | |
} | |
void log(Supplier<Boolean> condition, BiConsumer<String, Object[]> loggerCall, Supplier<String> message, Supplier<String>[] args) { | |
if (condition.get()) { | |
loggerCall.accept(message.get(), Arrays.stream(args).map(SuppliedString::new).toArray(); | |
} | |
} | |
static class AsyncCall implements BiConsumer<String, Object[]> { | |
static ExecutorService executor = Executors.newSingleThreadExecutor(); | |
private final BiConsumer<String, Object[]> delegate; | |
AsyncCall(BiConsumer<String, Object[]> delegate) { | |
this.delegate = delegate; | |
} | |
@Override | |
public void accept(String s, Object[] objects) { | |
executor.submit(() -> delegate.accept(s, objects)); | |
} | |
} | |
static class SuppliedString { | |
private final Supplier<String> supplier; | |
SuppliedString(Supplier<String> supplier) { | |
this.supplier = supplier; | |
} | |
@Override | |
public String toString() { | |
return supplier.get(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment