How do I obtain or create a Logger?

Since JDK 1.4 a logging API was introduced into the Java class libraries. This API enable our application to logs some messages to record our application activities.

To create an instance of Logger we can call the Logger.getLogger() factory method which will return the available logger for the given namespace or it will create a new one when it doesn’t exist.

package org.kodejava.example.util.logging;

import java.util.logging.Logger;

public class LoggingDemo {
    public static void main(String[] args) {
        // Obtaining an instance of Logger. This will create a new Logger
        // is it doesn't exist.
        Logger log = Logger.getLogger(LoggingDemo.class.getName());

        // Log some message using a different type of severity level."Info Message");
        log.warning("Warning Message");
        log.severe("Severe Message");
        log.config("Config Message");
        log.fine("Fine Message");
        log.finer("Finer Message");
        log.finest("Finest Message");

After we create the Logger instance we can create a message log by calling the logging method such as info(String message), warning(String message) and severe(String message). Below are some message produces by the Logger.

04 Dec 18 13:01:07 org.kodejava.example.util.logging.LoggingDemo main INFO: Info Message
04 Dec 18 13:01:07 org.kodejava.example.util.logging.LoggingDemo main WARNING: Warning Message
04 Dec 18 13:01:07 org.kodejava.example.util.logging.LoggingDemo main SEVERE: Severe Message

Wayan Saryada

Founder at Kode Java Org
I am a programmer, a runner, a recreational diver, currently live in the island of Bali, Indonesia. Mostly programming in Java, Spring Framework, Hibernate / JPA. You can support my works by donating here. Thank you 🥳

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.