log4j - ConsoleAppender



There may be a requirement to generate your on console as well along with log files which makes debugging easier during development phases.

To write your logging information on console, you would have to use org.apache.logging.log4j.ConsoleAppender class

log4j2.properties

appender.0.type = Console
appender.0.name = logFile

log4j2.xml

<Appenders>
   <Console name="logFile">
      <PatternLayout pattern="%d [%t] %p %c - %m%n"/>
</Appenders>

ConsoleAppender Configuration

ConsoleAppender has the following configurable parameters:

Property Type Default Value Description
name String   The name of the appender.
bufferSize int 8192 The size of the ByteBuffer internally used by the appender.
direct boolean false If set to true, log events will be written directly to either FileDescriptor.out or FileDescriptor.err. This setting bypasses the buffering of System.out and System.err and might provide a performance comparable to a file appender. This setting is incompatible with the follow attribute.
follow boolean false If set to true, the appender will honor reassignments of System.out (resp. System.err) via System.setOut (resp. System.setErr). Otherwise, the value of System.out (resp. System.err) at configuration time will be used. This setting is incompatible with the direct attribute.
ignoreExceptions boolean true If false, logging exception will be forwarded to the caller of the logging statement. Otherwise, they will be ignored.
immediateFlush boolean true If set to true, the appender will flush its internal buffer after each event.
target Target SYSTEM_OUT It specifies which standard output stream to use: SYSTEM_OUT - It uses the standard output. SYSTEM_ERR - It uses the standard error output.
filter String   Allows filtering log events just before they are formatted and sent.
layout Layout   Formats log events.

ConsoleAppender Configuration Examples

Following is a sample configuration file log4j2.properties for ConsoleAppender −

log4j2.properties

# Define the appender
appender.0.type = Console
appender.0.name = CONSOLE

appender.0.layout.type = PatternLayout
appender.0.layout.pattern = %d [%t] %p %c - %m%n

# Define the root logger with appender console
rootLogger.level = DEBUG
rootLogger.appenderRef.0.ref = CONSOLE

If you wish to have an XML configuration file equivalent to the above log4j.properties file, then here is the content:

log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="logFile" packages="">
   <Appenders>
      <Console name="CONSOLE">
         <PatternLayout pattern="%d [%t] %p %c - %m%n"/>
      </Console>
   </Appenders>
   <Loggers>
      <Root level="debug">
         <AppenderRef ref="CONSOLE"/>
      </Root>
   </Loggers>
</Configuration>

Example - Usage of ConsoleAppender

Now consider the following Java Example which would generate logging information:

Log4jDemo.java

package com.tutorialspoint;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Log4jDemo{

   /* Get actual class name to be printed on */
   private static final Logger LOGGER = LogManager.getLogger();

   public static void main(String[] args) {
      LOGGER.debug("Hello this is an debug message");
      LOGGER.info("Hello this is an info message");
   }
}

We're using log4j2.properties file.

Output

Compile and run the above program. It would print the following log information on the console:

2025-09-07 12:10:28,511 [main] DEBUG com.tutorialspoint.Log4jDemo - Hello this is an debug message
2025-09-07 12:10:28,517 [main] INFO com.tutorialspoint.Log4jDemo - Hello this is an info message
Advertisements