【问题标题】:How to configure slf4j-simple如何配置 slf4j-simple
【发布时间】:2013-01-10 18:51:02
【问题描述】:

api 1.7 和 slf4j-simple 作为实现。我只是找不到如何使用这种组合配置日志记录级别。

有人可以帮忙吗?

【问题讨论】:

标签: java logging slf4j


【解决方案1】:

要么通过系统属性

-Dorg.slf4j.simpleLogger.defaultLogLevel=debug

simplelogger.properties 类路径中的文件

详情请见http://www.slf4j.org/api/org/slf4j/impl/SimpleLogger.html

【讨论】:

  • 感谢我在 System.properties 中将“org.slf4j.simpleLogger.defaultLogLevel”设置为“error”,但是 slf4j 仍然会记录 INFO 级别的消息。任何想法?顺便说一句,我应该把 simplelogger.properties 放在哪里?
  • 尝试 org.slf4j.simplelogger.defaultlog 而不是 org.slf4j.simpleLogger.defaultLogLevel。文件必须在类路径中,默认包
  • 实际上它 (defaultLogLevel) 有效。刚刚发现我在错误的文件夹中修改程序 ;-) 而 defaultlog 不起作用。所以你可能想编辑你的答案,虽然我已经接受了
  • 请注意:实际上两个答案都很好,具体取决于您使用的 SimpleLogger 版本。例如,defaultLogLevel 适用于 1.7.5,但 defaultlog 适用于 1.6.6。我在配置我的项目日志记录并看到这篇文章时发现了这一点
  • 我花了很多时间想知道为什么这不起作用,最终意识到我在simplelogger.properties 中大写了L。应该都是小写的!
【解决方案2】:

这是一个示例 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

在 Maven 或 Gradle 项目中,“类路径”上的一个方便位置是 src/main/resources/simplelogger.properties

【讨论】:

  • @RobertHunt 如何将此日志保存到文件中?
  • @Devavrata 添加属性org.slf4j.simpleLogger.logFile - 输出目标,可以是文件路径,或特殊值“System.out”和“System.err”。默认为“System.err”。见slf4j.org/api/org/slf4j/impl/SimpleLogger.html
  • 是否可以有多个值?如果是怎么办?就像我想要 org.slf4j.simpleLogger.logFile=test.log, System.err?
  • @LOLWTFasdasdasdad 不幸的是,它只支持单个目标,System.out、System.err 或文件路径。它的设计很简单,如果你想要更高级的功能,你应该考虑一个完整的日志实现,比如 Log4J 或 Logback。
  • 我将此文件保存在类路径中并删除了 cmets 但仍然可以看到日志。
【解决方案3】:

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

public class App {
  public static void main(String[] args) {
    // for the code below to work, it must be executed before the
    ​// logger is created. see note below
    System.setProperty(org.slf4j.impl.SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "TRACE");
       
    ​org.slf4j.Logger log = LoggerFactory.getLogger(App.class);
       ​
    ​log.trace("trace");
    ​log.debug("debug");
    ​log.info("info");
    ​log.warn("warning");
    ​log.error("error");
  ​}
​}

日志级别为ERROR > WARN > INFO > DEBUG > TRACE

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

【讨论】:

  • "请注意,一旦创建了记录器,就无法更改日志级别。" - 这实际上是在哪里指定的?
  • ksl,在 org.slf4j.impl.SimpleLogger 中。创建第一个记录器时,会运行 init() 方法并从系统属性中获取默认的日志记录级别。这在任何时候都不会刷新。此外,org.slf4j.impl.SimpleLoggerFactory 只为一个类创建一次记录器,因此,总是为给定的类(或名称)返回相同的记录器。但是,可能有不同级别的记录器。因此,可能的解决方法是在您想要更改日志记录级别时将这些不同级别的记录器分配给您的“日志”变量。这不是很整洁的解决方案,但应该可以工作。
  • @Eemuli org.slf4j.impl.SimpleLogger 你的意思是实际的源代码而不是文档?
  • 创建记录器后,LOG_FILE_KEY 属性是否也不能更改?
  • 是的,我的意思是实际的源代码。我不确定 LOG_FILE_KEY。
【解决方案4】:

我注意到 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 人员更换记录器时中断。

【讨论】:

    猜你喜欢
    • 2020-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 2019-01-03
    相关资源
    最近更新 更多