如何配置slf4j-simple

Gel*_*Luo 149 java logging slf4j

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

任何人都可以帮忙吗?

Evg*_*eev 197

它是通过系统属性

-Dorg.slf4j.simpleLogger.defaultLogLevel=debug
Run Code Online (Sandbox Code Playgroud)

simplelogger.properties类路径上的文件

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

  • 请注意:实际上两个答案都很好,具体取决于您使用的SimpleLogger的版本.例如,defaultLogLevel适用于1.7.5,但defaultlog适用于1.6.6.在配置我的项目日志记录并发布此帖子时,我发现了这一点 (10认同)
  • 尝试使用org.slf4j.simplelogger.defaultlog而不是org.slf4j.simpleLogger.defaultLogLevel.文件必须位于类路径的默认包中 (2认同)
  • 实际上它(`defaultLogLevel`)工作.我发现我在一个错误的文件夹中修改程序;-)而且`defaultlog`不起作用.所以你可能想要编辑你的答案,虽然我接受了它 (2认同)

Rob*_*unt 97

这是一个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
Run Code Online (Sandbox Code Playgroud)

  • @Devavrata添加属性`org.slf4j.simpleLogger.logFile` - 输出目标,可以是文件的路径,或特殊值"System.out"和"System.err".默认为"System.err".见http://www.slf4j.org/api/org/slf4j/impl/SimpleLogger.html (6认同)
  • @LOLWTFasdasdasdad 不幸的是,它只支持单个目标,System.out、System.err 或文件路径。它的设计很简单,如果您想要更高级的功能,您应该考虑完整的日志记录实现,例如 Log4J 或 Logback。 (2认同)

Eem*_*uli 68

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

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");

    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

  • "请注意,创建记录器后,无法更改日志级别." - 谢谢你的澄清,这让我抓狂! (22认同)
  • "请注意,创建记录器后,无法更改日志级别." - 这实际指定在哪里? (3认同)
  • ksl,在org.slf4j.impl.SimpleLogger中.创建第一个记录器时,将运行init()方法,并从系统属性中获取默认日志记录级别.这在任何时候都不会刷新.此外,org.slf4j.impl.SimpleLoggerFactory只为类创建一次记录器,因此,对于给定的类(或名称),始终返回相同的记录器.但是,可以使用不同级别的记录器.因此,当您想要更改日志记录级别时,可能的解决方法是将这些不同级别的记录器分配给"日志"变量.这不是一个非常简洁的解决方案,但应该工作. (2认同)