Open In App

LogManager getLogger() method in Java with Examples

Last Updated : 29 Oct, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
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: Java
// 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));
    }
}
Output:
List of Logger Names: 
GFG
global


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

Get Logger Name Geeks: null

Next Article

Similar Reads