Java Programming Tutorials

Java programming tutorials with many code examples!

log4j example configuration – console logger

In this post we show Log4j example configuration that logs messages to console (standard output), which is very useful when testing applications!

log4j 1.2 project dependency

Add log4j 1.2 dependency to your project:

Maven configuration


Gradle configuration

compile group: 'log4j', name: 'log4j', version: '1.2.17' configuration

The file has to be on classpath (or WEB-INF/classpath for webapps) during run-time, so put it inside src/main/resources directory (in case of Maven and Gradle). defines the following:

  • what loggers are used (rootLogger),
  • where they output messages (appenders),
  • in what format (layout),
  • and on what level (DEBUG, INFO, etc.)

Here we define a logger that by default will print every DEBUG message into ConsoleAppender (named stdout), but messages from com.farenda.loggers.log4j.DataProcessor will be logged only from INFO level and above – so DEBUG logs will be suppressed:

# Root logger logs DEBUG messages into 'stdout' appender
log4j.rootLogger=DEBUG, stdout

# stdout (arbitrary name) defined as Console Appender:

# Layout of messages for the appender:
log4j.appender.stdout.layout.conversionPattern=%d [%t] %-5p %c - %m%n

# Messages from DataProcessor only from INFO level:

Java Application logging to Console

DataProcessor logs INFO and DEBUG messages

This class simply prints INFO and DEBUG messages, but above log4j configuration (last line) suppresses logging of DEBUG messages:

package com.farenda.loggers.log4j;

import org.apache.log4j.Logger;

public class DataProcessor {

    private static final Logger logger
            = Logger.getLogger(DataProcessor.class);

    public int process(String data) {"Processing: " + data);

        int value = data.length();
        logger.debug("Processed value: " + value);"End of processing.");

        return value;

Main Application logs DEBUG and ERROR messages

This application simply creates above DataProcessor, calls its process method, then instantiates ConsoleLogging and executes its compute method:

package com.farenda.loggers.log4j;

import org.apache.log4j.Logger;

public class ConsoleLogging {

    private static final Logger logger
            = Logger.getLogger(ConsoleLogging.class);

    public static void main(String[] args) {
        DataProcessor processor = new DataProcessor();
        int value = processor.process("Hello, World!");

        ConsoleLogging app = new ConsoleLogging();

    private int compute(int value) {
        logger.debug("Computing: " + value);

        try {
            return Math.floorDiv(value, 0);
        } catch (Exception e) {
            logger.error("Uh! Oh!", e);
        return 0;

The above code produces the following output:

2016-11-03 14:55:22,221 [main] INFO  com.farenda.loggers.log4j.DataProcessor - Processing: Hello, World!
2016-11-03 14:55:22,226 [main] INFO  com.farenda.loggers.log4j.DataProcessor - End of processing.
2016-11-03 14:55:22,226 [main] DEBUG com.farenda.loggers.log4j.ConsoleLogging - Computing: 13
2016-11-03 14:55:22,231 [main] ERROR com.farenda.loggers.log4j.ConsoleLogging - Uh! Oh!
java.lang.ArithmeticException: / by zero
        at java.lang.Math.floorDiv(
        at com.farenda.loggers.log4j.ConsoleLogging.compute(
        at com.farenda.loggers.log4j.ConsoleLogging.main(

Note that the DEBUG message from DataProcessor have been surpressed, because in this class is configured from INFO level and above.


Share with the World!