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