Thursday, October 23, 2025
HomeLanguagesJavaLogger setResourceBundle() method in Java with Examples

Logger setResourceBundle() method in Java with Examples

setResourceBundle() method of a Logger class used to set a resource bundle on this logger.we have to pass resource bundle object as a parameter to this method.After setting resource bundle all messages will be logged using the given resource bundle for its specific locale.
Syntax: 
 

public void setResourceBundle(ResourceBundle bundle)

Parameters: This method accepts one parameter bundle which represents resource bundle that this logger shall use.
Return value: This method returns nothing.
Exception: This method throws following Exceptions: 
 

  • NullPointerException – if the given bundle is null.
  • IllegalArgumentException – if the given bundle doesn’t have a base name, or if this logger already has a resource bundle set but the given bundle has a different base name.
  • SecurityException – if a security manager exists, this logger is not anonymous, and the caller does not have LoggingPermission(“control”). 
     

Below programs illustrate the setResourceBundle() method: 
Program 1: 
 

Java




// Java program to demonstrate
// Logger.setResourceBundle() method
 
import java.util.logging.*;
import java.util.ResourceBundle;
 
public class GFG {
 
    private static Logger logger
        = Logger.getLogger(
            GFG
                .class
                .getPackage()
                .getName());
 
    public static void main(String args[])
    {
 
        // Create ResourceBundle using getBundle
        // myResource is a properties file
        ResourceBundle bundle
            = ResourceBundle
                  .getBundle("myResource");
 
        // Set ResourceBundle to logger
        logger.setResourceBundle(bundle);
 
        // Log the ResourceBundle Name details
        logger.info("Resource Bundle "
                    + logger.getResourceBundleName());
    }
}


Output: 
For the above program, there is a properties file name resourceBundle.properties. we have to add this file alongside the class to execute the program. 
The output printed on console of Eclipse is shown below- 
 

Program 2: 
 

Java




import java.util.ResourceBundle;
// Java program to demonstrate
// Logger.setResourceBundle() method
 
import java.util.logging.Logger;
 
public class GFG {
 
    private static Logger logger
        = Logger.getLogger(
            GFG
                .class
                .getPackage()
                .getName());
 
    public static void main(String args[])
    {
 
        // Create ResourceBundle using getBundle
        // Resource is a properties file
        ResourceBundle resbundle
            = ResourceBundle.getBundle("resourceBundle");
 
        // Set ResourceBundle to logger
        logger.setResourceBundle(resbundle);
 
        // Log the ResourceBundle Name details
        System.out.println("Resource Bundle Name - "
                           + logger.getResourceBundleName()
                           + " and Locale - "
                           + logger
                                 .getResourceBundle()
                                 .getKeys());
    }
}


Output: 
For the above program, there is a properties file name myResource. we have to add this file alongside the class to execute the program. 
output printed on console output is shown below- 
 

Reference: https://docs.oracle.com/javase/10/docs/api/java/util/logging/Logger.html#setResourceBundle(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
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS