如何配置slf4j-simple

时间:2013-01-27 06:21:30

标签: java logging slf4j

api 1.7和slf4j-simple as implementation。我只是找不到如何使用此组合配置日志记录级别。

任何人都可以帮忙吗?

4 个答案:

答案 0 :(得分:185)

通过系统属性

-Dorg.slf4j.simpleLogger.defaultLogLevel=debug
类路径上的

simplelogger.properties文件

有关详细信息,请参阅http://www.slf4j.org/api/org/slf4j/impl/SimpleLogger.html

答案 1 :(得分:84)

这是一个示例simplelogger.properties,您可以将其置于类路径中(取消注释您希望使用的属性):

# SLF4J's SimpleLogger configuration file
# Simple implementation of Logger that sends all enabled log messages, for all defined loggers, to System.err.

# Default logging detail level for all instances of SimpleLogger.
# Must be one of ("trace", "debug", "info", "warn", or "error").
# If not specified, defaults to "info".
#org.slf4j.simpleLogger.defaultLogLevel=info

# Logging detail level for a SimpleLogger instance named "xxxxx".
# Must be one of ("trace", "debug", "info", "warn", or "error").
# If not specified, the default logging detail level is used.
#org.slf4j.simpleLogger.log.xxxxx=

# Set to true if you want the current date and time to be included in output messages.
# Default is false, and will output the number of milliseconds elapsed since startup.
#org.slf4j.simpleLogger.showDateTime=false

# The date and time format to be used in the output messages.
# The pattern describing the date and time format is the same that is used in java.text.SimpleDateFormat.
# If the format is not specified or is invalid, the default format is used.
# The default format is yyyy-MM-dd HH:mm:ss:SSS Z.
#org.slf4j.simpleLogger.dateTimeFormat=yyyy-MM-dd HH:mm:ss:SSS Z

# Set to true if you want to output the current thread name.
# Defaults to true.
#org.slf4j.simpleLogger.showThreadName=true

# Set to true if you want the Logger instance name to be included in output messages.
# Defaults to true.
#org.slf4j.simpleLogger.showLogName=true

# Set to true if you want the last component of the name to be included in output messages.
# Defaults to false.
#org.slf4j.simpleLogger.showShortLogName=false

答案 2 :(得分:67)

您可以通过设置系统属性以编程方式更改它:

public class App {

    public static void main(String[] args) {

        System.setProperty(org.slf4j.impl.SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "TRACE");

        final org.slf4j.Logger log = LoggerFactory.getLogger(App.class);

        log.trace("trace");
        log.debug("debug");
        log.info("info");
        log.warn("warning");
        log.error("error");

    }
}

日志级别为ERROR>警告>信息>调查> TRACE。

请注意,创建记录器后,无法更改日志级别。如果您需要动态更改日志记录级别,可能需要将log4j与SLF4J一起使用。

答案 3 :(得分:1)

我注意到Eemuli说你创建后不能改变日志级别 - 虽然这可能是设计,但并不完全正确。

我遇到了一个使用登录到slf4j的库的情况 - 我在编写maven mojo插件时使用了该库。

Maven使用slf4j SimpleLogger的(黑客)版本,我无法获取我的插件代码,将其日志重新路由到log4j,我可以控制。

我无法更改maven日志配置。

所以,为了安静下来一些嘈杂的信息消息,我发现我可以在运行时使用SimpleLogger来使用这样的反射。

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.spi.LocationAwareLogger;
    try
    {
        Logger l = LoggerFactory.getLogger("full.classname.of.noisy.logger");  //This is actually a MavenSimpleLogger, but due to various classloader issues, can't work with the directly.
        Field f = l.getClass().getSuperclass().getDeclaredField("currentLogLevel");
        f.setAccessible(true);
        f.set(l, LocationAwareLogger.WARN_INT);
    }
    catch (Exception e)
    {
        getLog().warn("Failed to reset the log level of " + loggerName + ", it will continue being noisy.", e);
    }

当然,请注意,这不是一个非常稳定/可靠的解决方案......因为它会在下一次maven人改变记录器时中断。