Class IoBuilder
- java.lang.Object
-
- org.apache.logging.log4j.io.IoBuilder
-
public class IoBuilder extends java.lang.ObjectBuilder class to wrapLoggersinto Java IO compatible classes.Both the
InputStream/OutputStreamandReader/Writerfamily of classes are supported.OutputStreamandWriterinstances can be wrapped by a filtered version of their corresponding classes (FilterOutputStreamandFilterWriter) in order to log all lines written to these instances.InputStreamandReaderinstances 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, orBufferedOutputStreamthat is backed by aLogger. 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
Levelit should use (defaults to the level of the underlying Logger), and an optionalMarker. The other configurable objects are explained in more detail below.- Since:
- 2.1
-
-
Field Summary
Fields Modifier and Type Field Description private booleanautoFlushprivate booleanbufferedprivate intbufferSizeprivate java.nio.charset.Charsetcharsetprivate java.lang.Stringfqcnprivate java.io.InputStreaminputStreamprivate Levellevelprivate ExtendedLoggerloggerprivate Markermarkerprivate java.io.OutputStreamoutputStreamprivate java.io.Readerreaderprivate java.io.Writerwriter
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.io.InputStreambuildInputStream()Builds a newInputStreamthat is wiretapped by its underlying Logger.java.io.OutputStreambuildOutputStream()Builds a newOutputStreamthat is backed by a Logger and optionally writes to another OutputStream as well.java.io.PrintStreambuildPrintStream()Builds a newPrintStreamthat is backed by a Logger and optionally writes to another OutputStream as well.java.io.PrintWriterbuildPrintWriter()Builds a newPrintWriterthat is backed by a Logger and optionally writes to another Writer as well.java.io.ReaderbuildReader()Builds a newReaderthat is wiretapped by its underlying Logger.java.io.WriterbuildWriter()Builds a newWriterthat is backed by a Logger and optionally writes to another Writer as well.IoBuilderfilter(java.io.InputStream inputStream)Configures anInputStreamto be wiretapped when building an InputStream.IoBuilderfilter(java.io.OutputStream outputStream)Configures anOutputStreamto be written to in addition to the underlying Logger.IoBuilderfilter(java.io.Reader reader)Configures aReaderto be wiretapped when building a Reader.IoBuilderfilter(java.io.Writer writer)Configures aWriterto be written to in addition to the underlying Logger.static IoBuilderforLogger()Creates a new builder using a Logger named after the calling Class.static IoBuilderforLogger(java.lang.Class<?> clazz)Creates a new builder using a Logger named after a given Class.static IoBuilderforLogger(java.lang.String loggerName)Creates a new builder using a Logger name.static IoBuilderforLogger(Logger logger)Creates a new builder for a givenLogger.IoBuildersetAutoFlush(boolean autoFlush)Indicates whether or not a builtPrintWriterorPrintStreamshould automatically flush when one of theprintln,printf, orformatmethods are invoked, or when a new line character is printed.IoBuildersetBuffered(boolean buffered)Enables or disables using a buffered variant of the desired IO class.IoBuildersetBufferSize(int bufferSize)Configures the buffer size to use when building aBufferedReaderorBufferedInputStreamLoggerStream.IoBuildersetCharset(java.nio.charset.Charset charset)Specifies the character set to use when building anInputStream,OutputStream, orPrintStream.IoBuildersetLevel(Level level)Specifies theLevelto log at.IoBuildersetMarker(Marker marker)Specifies an optionalMarkerto use in all logging messages.IoBuildersetWrapperClassName(java.lang.String fqcn)Specifies the fully qualified class name of the IO wrapper class implementation.
-
-
-
Field Detail
-
logger
private final ExtendedLogger logger
-
level
private Level level
-
marker
private Marker marker
-
fqcn
private java.lang.String fqcn
-
autoFlush
private boolean autoFlush
-
buffered
private boolean buffered
-
bufferSize
private int bufferSize
-
charset
private java.nio.charset.Charset charset
-
reader
private java.io.Reader reader
-
writer
private java.io.Writer writer
-
inputStream
private java.io.InputStream inputStream
-
outputStream
private java.io.OutputStream outputStream
-
-
Constructor Detail
-
IoBuilder
protected IoBuilder(Logger logger)
Constructs a new IoBuilder for the given Logger. This method is provided for extensibility of this builder class. The static factory methods should be used normally.- Parameters:
logger- theExtendedLoggerto wrap
-
-
Method Detail
-
forLogger
public static IoBuilder forLogger(Logger logger)
Creates a new builder for a givenLogger. The Logger instance must implementExtendedLoggeror an exception will be thrown.- Parameters:
logger- the Logger to wrap into a LoggerStream- Returns:
- a new IoBuilder
- Throws:
java.lang.UnsupportedOperationException- ifloggerdoes not implementExtendedLoggeror ifloggerisnull
-
forLogger
public static IoBuilder forLogger(java.lang.String loggerName)
Creates a new builder using a Logger name. The name provided is used to get a Logger fromLogManager.getLogger(String)which will be wrapped into a LoggerStream.- Parameters:
loggerName- the name of the Logger to wrap into a LoggerStream- Returns:
- a new IoBuilder
-
forLogger
public static IoBuilder forLogger(java.lang.Class<?> clazz)
Creates a new builder using a Logger named after a given Class. The Class provided is used to get a Logger fromLogManager.getLogger(Class)which will be wrapped into a LoggerStream.- Parameters:
clazz- the Class to use as the Logger name to wrap into a LoggerStream- Returns:
- a new IoBuilder
-
forLogger
public static IoBuilder forLogger()
Creates a new builder using a Logger named after the calling Class. This is equivalent to the following:IoBuilder builder = IoBuilder.forLogger(LogManager.getLogger());- Returns:
- a new IoBuilder
-
setLevel
public IoBuilder setLevel(Level level)
Specifies theLevelto log at. If no Level is configured, then the Level of the wrapped Logger will be used.- Parameters:
level- the Level to use for logging- Returns:
this
-
setMarker
public IoBuilder setMarker(Marker marker)
Specifies an optionalMarkerto use in all logging messages. If no Marker is specified, then no Marker will be used.- Parameters:
marker- the Marker to associate with all logging messages- Returns:
this
-
setWrapperClassName
public IoBuilder setWrapperClassName(java.lang.String fqcn)
Specifies the fully qualified class name of the IO wrapper class implementation. This method should only be used when making significant extensions to the provided classes in this component and is normally unnecessary.- Parameters:
fqcn- the fully qualified class name of the IO wrapper class being built- Returns:
this
-
setAutoFlush
public IoBuilder setAutoFlush(boolean autoFlush)
Indicates whether or not a builtPrintWriterorPrintStreamshould automatically flush when one of theprintln,printf, orformatmethods are invoked, or when a new line character is printed.- Parameters:
autoFlush- iftrue, thenprintln,printf, andformatwill auto flush- Returns:
this
-
setBuffered
public IoBuilder setBuffered(boolean buffered)
Enables or disables using a buffered variant of the desired IO class. If this is set totrue, then the instances returned bybuildReader()andbuildInputStream()can be safely cast (if necessary) toBufferedReaderandBufferedInputStreamrespectively. This option does not have any effect on the other built variants.- Parameters:
buffered- indicates whether or not a wrappedInputStreamorReadershould be buffered- Returns:
this
-
setBufferSize
public IoBuilder setBufferSize(int bufferSize)
Configures the buffer size to use when building aBufferedReaderorBufferedInputStreamLoggerStream.- Parameters:
bufferSize- the buffer size to use or a non-positive integer to use the default size- Returns:
this
-
setCharset
public IoBuilder setCharset(java.nio.charset.Charset charset)
Specifies the character set to use when building anInputStream,OutputStream, orPrintStream. If no character set is specified, thenCharset.defaultCharset()is used.- Parameters:
charset- the character set to use when building an InputStream, OutputStream, or PrintStream- Returns:
this
-
filter
public IoBuilder filter(java.io.Reader reader)
Configures aReaderto be wiretapped when building a Reader. This must be set to a non-nullvalue in order to callbuildReader().- Parameters:
reader- the Reader to wiretap- Returns:
this
-
filter
public IoBuilder filter(java.io.Writer writer)
Configures aWriterto 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.- Parameters:
writer- the Writer to write to in addition to the Logger- Returns:
this
-
filter
public IoBuilder filter(java.io.InputStream inputStream)
Configures anInputStreamto be wiretapped when building an InputStream. This must be set to a non-nullvalue in order to callbuildInputStream().- Parameters:
inputStream- the InputStream to wiretap- Returns:
this
-
filter
public IoBuilder filter(java.io.OutputStream outputStream)
Configures anOutputStreamto 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.- Parameters:
outputStream- the OutputStream to write to in addition to the Logger- Returns:
this
-
buildReader
public java.io.Reader buildReader()
Builds a newReaderthat is wiretapped by its underlying Logger. If buffering is enabled, then aBufferedReaderwill be returned.- Returns:
- a new Reader wiretapped by a Logger
- Throws:
java.lang.IllegalStateException- if no Reader was configured for this builder
-
buildWriter
public java.io.Writer buildWriter()
Builds a newWriterthat 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.- Returns:
- a new Writer or
FilterWriterbacked by a Logger
-
buildPrintWriter
public java.io.PrintWriter buildPrintWriter()
Builds a newPrintWriterthat 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.- Returns:
- a new PrintWriter that optionally writes to another Writer in addition to its underlying Logger
-
buildInputStream
public java.io.InputStream buildInputStream()
Builds a newInputStreamthat is wiretapped by its underlying Logger. If buffering is enabled, then aBufferedInputStreamwill be returned.- Returns:
- a new InputStream wiretapped by a Logger
- Throws:
java.lang.IllegalStateException- if no InputStream was configured for this builder
-
buildOutputStream
public java.io.OutputStream buildOutputStream()
Builds a newOutputStreamthat 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.- Returns:
- a new OutputStream that optionally writes to another OutputStream in addition to its underlying Logger
-
buildPrintStream
public java.io.PrintStream buildPrintStream()
Builds a newPrintStreamthat 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.- Returns:
- a new PrintStream that optionally writes to another OutputStream in addition to its underlying Logger
- Throws:
LoggingException- if the configured character set is unsupported byPrintStream
-
-