public final class Printers extends 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 | Field and Description |
---|---|
static AbstractPrinter |
DEFAULT_PRINTER
Prints errors and warnings to
System.err , and all other messages to System.out . |
static AbstractPrinter |
MUTE_PRINTER
Ignores all messages.
|
static AbstractPrinter |
NORMAL_PRINTER
Prints errors and warnings to
System.err , INFO messages to System.out , and ignores
VERBOSE and DEBUG messages. |
Modifier and Type | Method and Description |
---|---|
static void |
debug(String message)
Prints a debug message on the context printer.
|
static void |
debug(String pattern,
Object... arguments)
Prints a debug message on the context printer.
|
static void |
error(String message)
Prints an error condition on the context printer.
|
static void |
error(String pattern,
Object... arguments)
Prints an error condition on the context printer.
|
static void |
error(String message,
Throwable t)
Prints an error condition on the context printer.
|
static void |
error(String pattern,
Throwable t,
Object... arguments)
Prints an error condition on the context printer.
|
static Printer |
get()
Deprecated.
Use
AbstractPrinter.getContextPrinter() instead |
static void |
info(String message)
Prints an informative ("normal") message on the context printer.
|
static void |
info(String pattern,
Object... arguments)
Prints an informative ("normal") message on the context printer.
|
static <EX extends Throwable> |
redirectInfo(ConsumerWhichThrows<? super String,? extends RuntimeException> infoConsumer,
RunnableWhichThrows<? extends EX> runnable)
Deprecated.
|
static <EX extends Throwable> |
redirectInfo(Writer writer,
RunnableWhichThrows<EX> runnable)
Deprecated.
Use
AbstractPrinter.redirectInfo(Writer) instead |
static <EX extends Throwable> |
redirectInfoToFile(File file,
Charset charset,
RunnableWhichThrows<EX> runnable)
|
static <EX extends Throwable> |
redirectToFile(AbstractPrinter.Level level,
File file,
Charset charset,
RunnableWhichThrows<EX> runnable)
Runs the runnable with messages of the given level redirected into the file.
|
static <EX extends Throwable> |
redirectToFile(EnumSet<AbstractPrinter.Level> levels,
File file,
Charset charset,
Printer delegate,
RunnableWhichThrows<EX> runnable)
Runs the runnable with messages of the given levels redirected into the file.
|
static void |
verbose(String message)
Prints a verbose message on the context printer.
|
static void |
verbose(String pattern,
Object... arguments)
Prints a verbose message on the context printer.
|
static void |
warn(String message)
Prints a warning condition on the context printer.
|
static void |
warn(String pattern,
Object... arguments)
Prints a warning condition on the context printer.
|
static void |
withPrinter(Printer printer,
Runnable runnable)
Deprecated.
Use
AbstractPrinter.run(Runnable) instead |
static <EX extends Throwable> |
withPrinter(Printer printer,
RunnableWhichThrows<EX> runnable)
Deprecated.
Use
AbstractPrinter.run(RunnableWhichThrows) instead |
public static final AbstractPrinter DEFAULT_PRINTER
System.err
, and all other messages to System.out
.public static final AbstractPrinter NORMAL_PRINTER
System.err
, INFO messages to System.out
, and ignores
VERBOSE and DEBUG messages.public static final AbstractPrinter MUTE_PRINTER
@Deprecated public static Printer get()
AbstractPrinter.getContextPrinter()
insteadpublic static void error(@Nullable String message)
public static void error(String pattern, Object... arguments)
MessageFormat
public static void error(@Nullable String message, Throwable t)
public static void error(String pattern, Throwable t, Object... arguments)
MessageFormat
public static void warn(@Nullable String message)
public static void warn(String pattern, Object... arguments)
MessageFormat
public static void info(@Nullable String message)
public static void info(String pattern, Object... arguments)
MessageFormat
public static void verbose(@Nullable String message)
public static void verbose(String pattern, Object... arguments)
MessageFormat
public static void debug(@Nullable String message)
public static void debug(String pattern, Object... arguments)
MessageFormat
@Deprecated public static void withPrinter(Printer printer, Runnable runnable)
AbstractPrinter.run(Runnable)
instead@Deprecated public static <EX extends Throwable> void withPrinter(Printer printer, RunnableWhichThrows<EX> runnable) throws EX extends Throwable
AbstractPrinter.run(RunnableWhichThrows)
insteadEX extends Throwable
public static <EX extends Throwable> void redirectToFile(@Nullable AbstractPrinter.Level level, @Nullable File file, @Nullable Charset charset, RunnableWhichThrows<EX> runnable) throws IOException, EX extends Throwable
Iff level == null ||
file == null
, then messages are not
redirected.
If the runnable completes abruptly, then the file is closed, but not deleted.
IOException
- The file could not be createdEX extends Throwable
public static <EX extends Throwable> void redirectToFile(@Nullable EnumSet<AbstractPrinter.Level> levels, @Nullable File file, @Nullable Charset charset, @Nullable Printer delegate, RunnableWhichThrows<EX> runnable) throws IOException, EX extends Throwable
Iff levels == null ||
levels.isEmpty() ||
file ==
null
, then messages are not redirected.
If the runnable completes abruptly, then the file is closed, but not deleted.
charset
- If null
, then the default character encoding is useddelegate
- Prints the other messagesIOException
- The file could not be createdEX extends Throwable
@Deprecated public static <EX extends Throwable> void redirectInfoToFile(@Nullable File file, @Nullable Charset charset, RunnableWhichThrows<EX> runnable) throws IOException, EX extends Throwable
redirectToFile(de.unkrig.commons.text.AbstractPrinter.Level, File, Charset,
RunnableWhichThrows)
IOException
EX extends Throwable
@Deprecated public static <EX extends Throwable> void redirectInfo(@Nullable Writer writer, RunnableWhichThrows<EX> runnable) throws EX extends Throwable
AbstractPrinter.redirectInfo(Writer)
insteadEX extends Throwable
@Deprecated public static <EX extends Throwable> void redirectInfo(@Nullable ConsumerWhichThrows<? super String,? extends RuntimeException> infoConsumer, RunnableWhichThrows<? extends EX> runnable) throws EX extends Throwable
AbstractPrinter.redirect(de.unkrig.commons.text.AbstractPrinter.Level,
ConsumerWhichThrows)
insteadEX extends Throwable
Copyright © 2018 Arno Unkrig. All rights reserved.