Java中的 Logger getResourceBundle() 方法及示例
Logger类的getResourceBundle()方法用于本地化此记录器的资源包。我们可以通过 setResourceBundle 方法设置 ResourceBundle ,也可以从通过 getLogger 工厂方法设置的资源包名称映射到当前默认语言环境,该方法将返回通过上述方式设置的 ResourceBundle 。如果结果为 null,则 Logger 将使用从其父级继承的资源包或资源包名称。
句法:
public ResourceBundle getResourceBundle()
参数:此方法不接受任何内容。
返回值:此方法返回本地化包。
下面的程序说明了 getResourceBundle() 方法:
方案一:
Java
// Java program to demonstrate
// Logger.getParent() method
import java.util.logging.*;
import java.util.ResourceBundle;
public class GFG {
private static Logger logger
= Logger.getLogger(
String
.class
.getPackage()
.getName());
public static void main(String args[])
{
logger.info("printing the message...");
ResourceBundle rs
= logger.getResourceBundle();
logger.info("Resource Bundle " + rs);
}
}
Java
// Java program to demonstrate
// Logger.getParent() 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);
// Get ResourceBundle from logger
ResourceBundle rs
= logger.getResourceBundle();
// Log the ResourceBundle details
logger.info("Resource Bundle "
+ rs.getBaseBundleName());
}
}
输出:
eclipse IDE上打印的输出如下所示 -
方案二:
Java
// Java program to demonstrate
// Logger.getParent() 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);
// Get ResourceBundle from logger
ResourceBundle rs
= logger.getResourceBundle();
// Log the ResourceBundle details
logger.info("Resource Bundle "
+ rs.getBaseBundleName());
}
}
对于上述程序,有一个属性文件名为 myResource。我们必须在类旁边添加这个文件来执行程序。
输出:
eclipse IDE上打印的输出如下所示 -
参考: https: Java/util/logging/Logger.html#getResourceBundle()