public class IoBuilder extends Object
Loggers
into Java IO compatible classes.
Both the InputStream
/OutputStream
and Reader
/Writer
family of classes are
supported. OutputStream
and Writer
instances can be wrapped by a filtered version of their
corresponding classes (FilterOutputStream
and FilterWriter
) in order to log all
lines written to these instances. InputStream
and Reader
instances can be wrapped by a sort of
wiretapped version of their respective classes; all lines read from these instances will be logged.
The main feature, however, is the ability to create a PrintWriter
, PrintStream
, Writer
,
BufferedWriter
, OutputStream
, or BufferedOutputStream
that is backed by a
Logger
. The main inspiration for this feature is the JDBC API which uses a PrintWriter to perform debug
logging. In order to properly integrate APIs like JDBC into Log4j, create a PrintWriter using this class.
The IoBuilder support configuration of the logging Level
it should use (defaults to the level of
the underlying Logger), and an optional Marker
. The other configurable objects are explained in more
detail below.
Modifier | Constructor and Description |
---|---|
protected |
IoBuilder(org.apache.logging.log4j.Logger logger)
Constructs a new IoBuilder for the given Logger.
|
Modifier and Type | Method and Description |
---|---|
InputStream |
buildInputStream()
Builds a new
InputStream that is wiretapped by its underlying Logger. |
OutputStream |
buildOutputStream()
Builds a new
OutputStream that is backed by a Logger and optionally writes to another OutputStream as
well. |
PrintStream |
buildPrintStream()
Builds a new
PrintStream that is backed by a Logger and optionally writes to another OutputStream as
well. |
PrintWriter |
buildPrintWriter()
Builds a new
PrintWriter that is backed by a Logger and optionally writes to another Writer as well. |
Reader |
buildReader()
Builds a new
Reader that is wiretapped by its underlying Logger. |
Writer |
buildWriter()
Builds a new
Writer that is backed by a Logger and optionally writes to another Writer as well. |
IoBuilder |
filter(InputStream inputStream)
Configures an
InputStream to be wiretapped when building an InputStream. |
IoBuilder |
filter(OutputStream outputStream)
Configures an
OutputStream to be written to in addition to the underlying Logger. |
IoBuilder |
filter(Reader reader)
Configures a
Reader to be wiretapped when building a Reader. |
IoBuilder |
filter(Writer writer)
Configures a
Writer to be written to in addition to the underlying Logger. |
static IoBuilder |
forLogger()
Creates a new builder using a Logger named after the calling Class.
|
static IoBuilder |
forLogger(Class<?> clazz)
Creates a new builder using a Logger named after a given Class.
|
static IoBuilder |
forLogger(org.apache.logging.log4j.Logger logger)
Creates a new builder for a given
Logger . |
static IoBuilder |
forLogger(String loggerName)
Creates a new builder using a Logger name.
|
IoBuilder |
setAutoFlush(boolean autoFlush)
Indicates whether or not a built
PrintWriter or PrintStream should automatically flush when
one of the println , printf , or format methods are invoked, or when a new line character
is printed. |
IoBuilder |
setBuffered(boolean buffered)
Enables or disables using a buffered variant of the desired IO class.
|
IoBuilder |
setBufferSize(int bufferSize)
Configures the buffer size to use when building a
BufferedReader or
BufferedInputStream LoggerStream. |
IoBuilder |
setCharset(Charset charset)
|
IoBuilder |
setLevel(org.apache.logging.log4j.Level level)
Specifies the
Level to log at. |
IoBuilder |
setMarker(org.apache.logging.log4j.Marker marker)
Specifies an optional
Marker to use in all logging messages. |
IoBuilder |
setWrapperClassName(String fqcn)
Specifies the fully qualified class name of the IO wrapper class implementation.
|
protected IoBuilder(org.apache.logging.log4j.Logger logger)
logger
- the ExtendedLogger
to wrappublic static IoBuilder forLogger(org.apache.logging.log4j.Logger logger)
Logger
. The Logger instance must implement ExtendedLogger
or
an exception will be thrown.logger
- the Logger to wrap into a LoggerStreamUnsupportedOperationException
- if logger
does not implement ExtendedLogger
or if
logger
is null
public static IoBuilder forLogger(String loggerName)
LogManager.getLogger(String)
which will be wrapped into a LoggerStream.loggerName
- the name of the Logger to wrap into a LoggerStreampublic static IoBuilder forLogger(Class<?> clazz)
LogManager.getLogger(Class)
which will be wrapped into a LoggerStream.clazz
- the Class to use as the Logger name to wrap into a LoggerStreampublic static IoBuilder forLogger()
IoBuilder builder = IoBuilder.forLogger(LogManager.getLogger());
public IoBuilder setLevel(org.apache.logging.log4j.Level level)
Level
to log at. If no Level is configured, then the Level of the wrapped Logger will be
used.level
- the Level to use for loggingthis
public IoBuilder setMarker(org.apache.logging.log4j.Marker marker)
Marker
to use in all logging messages. If no Marker is specified, then no Marker
will be used.marker
- the Marker to associate with all logging messagesthis
public IoBuilder setWrapperClassName(String fqcn)
fqcn
- the fully qualified class name of the IO wrapper class being builtthis
public IoBuilder setAutoFlush(boolean autoFlush)
PrintWriter
or PrintStream
should automatically flush when
one of the println
, printf
, or format
methods are invoked, or when a new line character
is printed.autoFlush
- if true
, then println
, printf
, and format
will auto flushthis
public IoBuilder setBuffered(boolean buffered)
true
, then the
instances returned by buildReader()
and buildInputStream()
can be safely cast (if necessary)
to BufferedReader
and BufferedInputStream
respectively. This option does not
have any effect on the other built variants.buffered
- indicates whether or not a wrapped InputStream
or Reader
should be bufferedthis
public IoBuilder setBufferSize(int bufferSize)
BufferedReader
or
BufferedInputStream
LoggerStream.bufferSize
- the buffer size to use or a non-positive integer to use the default sizethis
public IoBuilder setCharset(Charset charset)
InputStream
, OutputStream
, or
PrintStream
. If no character set is specified, then Charset.defaultCharset()
is used.charset
- the character set to use when building an InputStream, OutputStream, or PrintStreamthis
public IoBuilder filter(Reader reader)
Reader
to be wiretapped when building a Reader. This must be set to a non-null
value in order to call buildReader()
.reader
- the Reader to wiretapthis
public IoBuilder filter(Writer writer)
Writer
to be written to in addition to the underlying Logger. If no Writer is specified,
then the built Writer or PrintWriter will only write to the underlying Logger.writer
- the Writer to write to in addition to the Loggerthis
public IoBuilder filter(InputStream inputStream)
InputStream
to be wiretapped when building an InputStream. This must be set to a
non-null
value in order to call buildInputStream()
.inputStream
- the InputStream to wiretapthis
public IoBuilder filter(OutputStream outputStream)
OutputStream
to be written to in addition to the underlying Logger. If no OutputStream is
specified, then the built OutputStream or PrintStream will only write to the underlying Logger.outputStream
- the OutputStream to write to in addition to the Loggerthis
public Reader buildReader()
Reader
that is wiretapped by its underlying Logger. If buffering is enabled, then a
BufferedReader
will be returned.IllegalStateException
- if no Reader was configured for this builderpublic Writer buildWriter()
Writer
that is backed by a Logger and optionally writes to another Writer as well. If no
Writer is configured for this builder, then the returned Writer will only write to its underlying Logger.FilterWriter
backed by a Loggerpublic PrintWriter buildPrintWriter()
PrintWriter
that is backed by a Logger and optionally writes to another Writer as well. If
no Writer is configured for this builder, then the returned PrintWriter will only write to its underlying
Logger.public InputStream buildInputStream()
InputStream
that is wiretapped by its underlying Logger. If buffering is enabled, then a
BufferedInputStream
will be returned.IllegalStateException
- if no InputStream was configured for this builderpublic OutputStream buildOutputStream()
OutputStream
that is backed by a Logger and optionally writes to another OutputStream as
well. If no OutputStream is configured for this builder, then the returned OutputStream will only write to its
underlying Logger.public PrintStream buildPrintStream()
PrintStream
that is backed by a Logger and optionally writes to another OutputStream as
well. If no OutputStream is configured for this builder, then the returned PrintStream will only write to its
underlying Logger.org.apache.logging.log4j.LoggingException
- if the configured character set is unsupported by PrintStream
Copyright © 1999-2021 Apache Software Foundation. All Rights Reserved.
Apache Logging, Apache Log4j, Log4j, Apache, the Apache feather logo, the Apache Logging project logo, and the Apache Log4j logo are trademarks of The Apache Software Foundation.