Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

log4j example configuration – console logger

farenda 2016-11-03 0

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:

  • Read Log4j 1.2 manual for detailed description of Log4j
  • See PatternLayout for description of layout format
Share with the World!
Categories Java Tags java, log4j, logging
Previous: Java int to String
Next: log4j example configuration file logger

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2021

sponsored