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

<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>

Gradle configuration

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

log4j.properties configuration

The log4j.properties 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).
log4j.properties 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:
log4j.appender.stdout=org.apache.log4j.ConsoleAppender

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

# Messages from DataProcessor only from INFO level:
log4j.logger.com.farenda.loggers.log4j.DataProcessor=INFO

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) {
        logger.info("Processing: " + data);

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

        logger.info("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();
        app.compute(value);
    }

    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(Math.java:1052)
        at com.farenda.loggers.log4j.ConsoleLogging.compute(ConsoleLogging.java:22)
        at com.farenda.loggers.log4j.ConsoleLogging.main(ConsoleLogging.java:15)

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

References:

Share with the World!