public final class Printers
extends java.lang.Object
System.out.println()" and "System.err.println()", with much more flexibility.
This concept borrows from the various "logging" frameworks, but differs in several ways:
Five levels of output exist:
Use the error(), warn(), info(), verbose() and debug() methods to print to
the "current context".
The "initial context" (which is in effect iff withPrinter(Printer, Runnable) has not yet been called)
prints errors and warnings to System.err and infos, verbose and debug to System.out.
In many cases, you want to use a LevelFilteredPrinter in a very early stage to configure how output is
printed:
public static void
main(final String... args) {
final Main main = new Main();
Printers.withPrinter(main.levelFilteredPrinter, new Runnable() {
@Override public void run() { main.main2(args); }
});
}
private final LevelFilteredPrinter levelFilteredPrinter = new LevelFilteredPrinter(Printers.get());
private void
main2(String[] args) {
// As an example, we enable verbose messages here. You would typically do that as a reaction to a
// "--verbose" command line option.
levelFilteredPrinter.setVerbose();
Printers.debug("foo"); // Not printed, because debug level was not enabled on the levelFilteredPrinter
Printers.verbose("foo"); // Prints to STDOUT, because verbose messages were enabled above.
Printers.info("foo"); // Prints to STDOUT
Printers.warn("foo"); // prints to STDERR
Printers.err("foo"); // prints to STDERR
}
| Modifier and Type | Method and Description |
|---|---|
static void |
debug(java.lang.String message)
Prints a debug message on the context printer.
|
static void |
debug(java.lang.String pattern,
java.lang.Object... arguments)
Prints a debug message on the context printer.
|
static void |
error(java.lang.String message)
Prints an error condition on the context printer.
|
static void |
error(java.lang.String pattern,
java.lang.Object... arguments)
Prints an error condition on the context printer.
|
static void |
error(java.lang.String message,
java.lang.Throwable t)
Prints an error condition on the context printer.
|
static void |
error(java.lang.String pattern,
java.lang.Throwable t,
java.lang.Object... arguments)
Prints an error condition on the context printer.
|
static Printer |
get() |
static void |
info(java.lang.String message)
Prints an informative ("normal") message on the context printer.
|
static void |
info(java.lang.String pattern,
java.lang.Object... arguments)
Prints an informative ("normal") message on the context printer.
|
static <EX extends java.lang.Throwable> |
redirectInfo(ConsumerWhichThrows<? super java.lang.String,? extends java.lang.RuntimeException> infoConsumer,
RunnableWhichThrows<? extends EX> runnable)
Runs the runnable with its INFO output redirected to the infoConsumer.
|
static <EX extends java.lang.Throwable> |
redirectInfo(java.io.Writer writer,
RunnableWhichThrows<EX> runnable)
Runs the runnable with its INFO output redirected to the writer.
|
static <EX extends java.lang.Throwable> |
redirectInfoToFile(java.io.File file,
RunnableWhichThrows<EX> runnable)
Runs the runnable with its INFO output redirected into the file.
|
static void |
verbose(java.lang.String message)
Prints a verbose message on the context printer.
|
static void |
verbose(java.lang.String pattern,
java.lang.Object... arguments)
Prints a verbose message on the context printer.
|
static void |
warn(java.lang.String message)
Prints a warning condition on the context printer.
|
static void |
warn(java.lang.String pattern,
java.lang.Object... arguments)
Prints a warning condition on the context printer.
|
static void |
withPrinter(Printer printer,
java.lang.Runnable runnable)
Replaces the context printer with the given
printer while the runnable is running. |
static <EX extends java.lang.Throwable> |
withPrinter(Printer printer,
RunnableWhichThrows<EX> runnable)
Replaces the context printer with the given
printer while the runnable is running. |
public static Printer get()
public static void error(@Nullable java.lang.String message)
public static void error(java.lang.String pattern,
java.lang.Object... arguments)
MessageFormatpublic static void error(@Nullable java.lang.String message, java.lang.Throwable t)
public static void error(java.lang.String pattern,
java.lang.Throwable t,
java.lang.Object... arguments)
MessageFormatpublic static void warn(@Nullable java.lang.String message)
public static void warn(java.lang.String pattern,
java.lang.Object... arguments)
MessageFormatpublic static void info(@Nullable java.lang.String message)
public static void info(java.lang.String pattern,
java.lang.Object... arguments)
MessageFormatpublic static void verbose(@Nullable java.lang.String message)
public static void verbose(java.lang.String pattern,
java.lang.Object... arguments)
MessageFormatpublic static void debug(@Nullable java.lang.String message)
public static void debug(java.lang.String pattern,
java.lang.Object... arguments)
MessageFormatpublic static void withPrinter(Printer printer, java.lang.Runnable runnable)
printer while the runnable is running.public static <EX extends java.lang.Throwable> void withPrinter(Printer printer, RunnableWhichThrows<EX> runnable) throws EX extends java.lang.Throwable
printer while the runnable is running.EX extends java.lang.Throwablepublic static <EX extends java.lang.Throwable> void redirectInfoToFile(@Nullable java.io.File file, RunnableWhichThrows<EX> runnable) throws java.io.IOException, EX extends java.lang.Throwable
Iff the file is null, then the INFO output is not redirected.
java.io.IOExceptionEX extends java.lang.Throwablepublic static <EX extends java.lang.Throwable> void redirectInfo(@Nullable java.io.Writer writer, RunnableWhichThrows<EX> runnable) throws EX extends java.lang.Throwable
null, then the INFO output is not redirected.EX extends java.lang.Throwablepublic static <EX extends java.lang.Throwable> void redirectInfo(@Nullable ConsumerWhichThrows<? super java.lang.String,? extends java.lang.RuntimeException> infoConsumer, RunnableWhichThrows<? extends EX> runnable) throws EX extends java.lang.Throwable
null, then the INFO output is not redirected.EX extends java.lang.Throwable