Apache logging services logo Apache log4j logo

Log4j IOStreams

The IOStreams component is a Log4j API extension that provides numerous classes from java.io that can either write to a Logger while writing to another OutputStream or Writer, or the contents read by an InputStream or Reader can be wiretapped by a Logger.

Requirements

The Log4j IOStreams API extension requires the Log4j 2 API. This component was introduced in Log4j 2.1. For more information, see Runtime Dependencies.

Usage

The main entry point for the IOStreams module is the builder class IoBuilder, and in particular, the IoBuilder.forLogger() methods. One primary usage of this API extension is for setting up loggers in the JDBC API. For example:

PrintWriter logger = IoBuilder.forLogger(DriverManager.class)
                              .setLevel(Level.DEBUG)
                              .buildPrintWriter();
DriverManager.setLogWriter(logger);
      

Using the IoBuilder class, there are a few more options that can be set. In general, there are six primary classes one can build from it: Reader, Writer, PrintWriter, InputStream, OutputStream, and PrintStream. The input-oriented classes are for wiretapping, and the output-oriented classes are for creating either an output class that solely outputs its lines as log messages, or an output filter class that logs all lines output through it to its delegate output class.