📜  Java中的 Logger setResourceBundle() 方法及示例

📅  最后修改于: 2022-05-13 01:55:24.885000             🧑  作者: Mango

Java中的 Logger setResourceBundle() 方法及示例

用于在此记录器上设置资源包的Logger类的setResourceBundle()方法。我们必须将资源包对象作为参数传递给此方法。设置资源包后,将使用给定资源包为其特定语言环境记录所有消息.
句法:

public void setResourceBundle(ResourceBundle bundle)

参数:此方法接受一个参数,该参数包代表此记录器应使用的资源包。
返回值:此方法不返回任何内容。
异常:此方法抛出以下异常:

  • NullPointerException – 如果给定的包为空。
  • IllegalArgumentException – 如果给定的包没有基本名称,或者如果此记录器已经设置了资源包但给定的包具有不同的基本名称。
  • SecurityException – 如果存在安全管理器,则此记录器不是匿名的,并且调用者没有 LoggingPermission(“control”)。

下面的程序说明了 setResourceBundle() 方法:
方案一:

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());
    }
}


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());
    }
}


输出:
对于上面的程序,有一个属性文件名为resourceBundle.properties。我们必须在类旁边添加这个文件来执行程序。
在 Eclipse 的控制台上打印的输出如下所示 -

方案二:

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());
    }
}

输出:
对于上述程序,有一个名为 myResource 的属性文件。我们必须在类旁边添加这个文件来执行程序。
打印在控制台输出上的输出如下所示 -

参考: https: Java Java.util.logging.Logger)