LogManager getLogger() method in Java with Examples

The getLogger() method of java.util.logging.LogManager is used to get the specified Logger in this LogManager instance. This Logger must be a named Logger. This method will get this Logger in this LogManager if it exists. If it does not exists, then this method returns null.

Syntax:

public Logger getLogger(Logger logger)

Parameters: This method accepts a parameter logger which is the name of the Logger to be get in this LogManager instance.

Return Value: This method returns the name of this Logger in this LogManager if it exists. If it does not exists, then this method returns null.

Below programs illustrate getLogger() method:



filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate
// LogManager getLogger() method
  
import java.util.logging.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Create LogManager object
        LogManager logManager
            = LogManager.getLogManager();
  
        String LoggerName = "GFG";
        Logger logger
            = Logger.getLogger(LoggerName);
        logManager.addLogger(logger);
  
        System.out.println("\nList of Logger Names: ");
        Enumeration<String> listOfNames
            = logManager.getLoggerNames();
        while (listOfNames.hasMoreElements())
            System.out.println(listOfNames.nextElement());
  
        System.out.println(
            "\nGet Logger Name "
            + LoggerName + ": "
            + logManager.getLogger(LoggerName));
  
        LoggerName = "Geeks";
  
        System.out.println(
            "\nGet Logger Name "
            + LoggerName + ": "
            + logManager.getLogger(LoggerName));
    }
}

chevron_right


Output:

List of Logger Names: 
GFG
global


Get Logger Name GFG: java.util.logging.Logger@1540e19d

Get Logger Name Geeks: null

Reference: https://docs.oracle.com/javase/9/docs/api/java/util/logging/LogManager.html#getLogger-java.lang.String-

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.