使用jdk logging / slf4j更改日志记录级别本地化名称

时间:2014-12-15 09:54:03

标签: java logging localization 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

我想为日志记录级别设置自定义本地化消息,而不是默认提供的消息。

我该怎么做

  1. 直接使用JDK日志记录(我看到Logger.getLogger可以使用ResourceBundle,但我没有设法让它工作,而且我也不知道该文件中应该是什么)
  2. 使用SLF4外观(我想这意味着调整SLF4J的LoggerFactory.getLogger功能,这是我用来获取记录器的功能)

2 个答案:

答案 0 :(得分:4)

基于某些测试,使用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对象。令人沮丧,是吗?

答案 1 :(得分:0)

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-

得到了这个想法