[英]Changing logging levels localized name with jdk logging/slf4j
我看到使用 jdk 日志記錄的日志記錄級別已本地化,運行此示例:
package com.stackoverflow.tests.logging;
import java.util.Locale;
import java.util.logging.Logger;
public class TestLogging
{
public static void doLog(Locale l)
{
Locale.setDefault(l);
Logger logger = Logger.getLogger(TestLogging.class.getName());
final String msg = "Hello logging world";
logger.severe(msg);
logger.info(msg);
logger.config(msg);
logger.fine(msg);
logger.finer(msg);
logger.finest(msg);
}
public static void main(String args[])
{
doLog(Locale.ITALY);
// doLog(Locale.UK);
// doLog(Locale.FRANCE);
}
}
意大利語的輸出(使用默認記錄器配置,因此只記錄嚴重和信息級別)
dic 15, 2014 10:48:43 AM com.stackoverflow.tests.logging.TestLogging doLog
Grave: Hello logging world
dic 15, 2014 10:48:43 AM com.stackoverflow.tests.logging.TestLogging doLog
Informazioni: Hello logging world
我想為日志級別設置自定義本地化消息,而不是默認提供的消息。
我怎樣才能做到這一點
Logger.getLogger
可以使用 ResourceBundle 但我沒有設法使其工作,而且我不知道該文件中的預期內容)LoggerFactory.getLogger
函數,這是我用來獲取記錄器的函數)根據一些測試,JDK 日志記錄似乎無法做到這一點。
傳遞給Logger
的資源包名稱不會影響創建本地化名稱的Level
對象。 相反,它使用默認資源包 ( sun.util.logging.resources.logging
) 來獲取關卡的文本版本。
我的測試包含一個名為logmessages.properties
的文件,其中包含:
INFO="NEWINFO"
testmessage="fooballs"
和一個簡單的類:
import java.util.logging.Logger;
public class LocaleLoggingTest {
public static void main(String[] args) throws Exception {
Logger logger = Logger.getLogger("name", "logmessages");
logger.info("testmessage");
}
}
輸出:
Dec 15, 2014 10:23:39 AM LocaleLoggingTest main
INFO: "fooballs"
如果您調試,當您到達java.util.logging.SimpleFormatter.format(LogRecord)
時,日志記錄有一個帶有默認資源包的Level
對象。 令人沮喪,嗯?
在java.util.logging
,您可以使用不同的包名稱或根本不定義自己的級別。 這樣做的方法是繼承java.util.logging.Level
並添加您自己的常量。 例如,如果您想要非本地化名稱,您可以按如下方式創建關卡:
public class Level extends java.util.logging.Level {
private static final long serialVersionUID = -1283674772992561191L;
public static final Level OFF = new Level("OFF", Integer.MAX_VALUE);
public static final Level SEVERE = new Level("SEVERE", 1000);
public static final Level WARNING = new Level("WARNING", 900);
public static final Level INFO = new Level("INFO", 800);
public static final Level CONFIG = new Level("CONFIG", 700);
public static final Level FINE = new Level("FINE", 500);
public static final Level FINER = new Level("FINER", 400);
public static final Level FINEST = new Level("FINEST", 300);
public static final Level ALL = new Level("ALL", Integer.MIN_VALUE);
protected Level(String name, int value) {
super(name, value);
}
}
如果要使用其他捆綁包名稱,請執行以下操作:
public class Level extends java.util.logging.Level {
private static final long serialVersionUID = -1283674772992561191L;
static final String my_bundle_name = "my.bundle.name";
public static final Level OFF = new Level("OFF", Integer.MAX_VALUE, my_bundle_name);
public static final Level SEVERE = new Level("SEVERE", 1000, my_bundle_name);
public static final Level WARNING = new Level("WARNING", 900, my_bundle_name);
public static final Level INFO = new Level("INFO", 800, my_bundle_name);
public static final Level CONFIG = new Level("CONFIG", 700, my_bundle_name);
public static final Level FINE = new Level("FINE", 500, my_bundle_name);
public static final Level FINER = new Level("FINER", 400, my_bundle_name);
public static final Level FINEST = new Level("FINEST", 300, my_bundle_name);
public static final Level ALL = new Level("ALL", Integer.MIN_VALUE, my_bundle_name);
protected Level(String name, int value, String resourceBundleName) {
super(name, value, resourceBundleName);
}
}
當然,您必須使用自定義級別而不是默認級別,因此您必須使用像Logger.log(Level level, String msg)
這樣的調用,在其中明確指定級別,而不是Logger.info(String msg)
,即始終使用默認級別。
編輯:我從https://docs.oracle.com/javase/8/docs/api/java/util/logging/Level.html#Level-java.lang.String-int-得到這個想法
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.