Friday, October 10, 2025
HomeLanguagesJavaLogManager addLogger() method in Java with Examples

LogManager addLogger() method in Java with Examples

The addLogger() method of java.util.logging.LogManager is used to insert the specified Logger in this LogManager instance. This Logger must be a named Logger. This method will add this Logger in this LogManager if it does not exist already. If it exists already, then this method returns false.

Syntax:

public boolean addLogger(Logger logger)

Parameters: This method accepts a parameter logger which is the name of the Logger to be inserted in this LogManager instance, if it does not exist already.

Return Value: This method returns a boolean value false, if this Logger does exist already. Else this method does not return anything.

Exceptions: This method throws NullPointerException if the logger to be added is null.

Below programs illustrate addLogger() method:




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


Output:

Earlier List of Logger Names: 
global

Adding Logger: GFG

Updated List of Logger Names: 
GFG
global

Reference: https://docs.oracle.com/javase/9/docs/api/java/util/logging/LogManager.html#addLogger-java.util.logging.Logger-

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32349 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6715 POSTS0 COMMENTS
Nicole Veronica
11878 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6837 POSTS0 COMMENTS
Ted Musemwa
7097 POSTS0 COMMENTS
Thapelo Manthata
6792 POSTS0 COMMENTS
Umr Jansen
6791 POSTS0 COMMENTS