No announcement
 

Logging

When we encounter any error or bug in the code , to fix it, we mostly use System.out.println to print statements. Once the fix has been done, we remove those message strings. Logging API is the appropriate solution for such scenarios.

Example:

BasicLoggingDemo.java

package com.codingnous.tutorial;

import java.util.logging.Level;
import java.util.logging.Logger;

public class BasicLoggingDemo {

	public static void main(String[] args) {
		// Log a message
		Logger.getGlobal().info("Logging example string.");

		// Set logging off
		Logger.getGlobal().setLevel(Level.OFF);

		// This will not be logged as we have disabled logging.
		Logger.getGlobal().info("This log will no be displayed.");

		// Set logging on again
		Logger.getGlobal().setLevel(Level.ALL);

		// This will be logged
		Logger.getGlobal().info("Another logging example string.");
	}
}

Output
May 10, 2022 8:36:02 PM com.codingnous.tutorial.BasicLoggingDemo main INFO: Logging example string. May 10, 2022 8:36:02 PM com.codingnous.tutorial.BasicLoggingDemo main INFO: Another logging example string.