使用 jdk 进行 slf4j 日志记录 –如何启用调试?

发布于 2024-10-04 14:05:13 字数 262 浏览 1 评论 0原文

默认情况下,slf4j 与 jdk (slf4j-jdk14-1.6.1.jar) 一起使用时,不会记录调试消息。 我如何启用它们?

我在官方文档、网络或此处都找不到有关如何启用它的信息。

我找到了一些有关在 %JDK_HOME%/lib 中创建文件并在配置文件中定义级别的信息(尽管失败)。 但是,我想在编译/运行时定义级别,以便我可以使用不同的日志记录级别从 IDE 运行和调试我的应用程序。

没有我可以设置的环境变量或 VM arg 吗?

By default slf4j, when using with jdk (slf4j-jdk14-1.6.1.jar), does not log debug messages.
How do I enable them?

I can’t find info neither in the official docs, the web or here on how to enable it.

I found some info on (failed though) creating a file in %JDK_HOME%/lib and defining the level there, in a config file.
However, I would like to define the level at compile-/run-time so I can both run and debug my app from my IDE with different logging levels.

Isn’t there some environment variable I can set, or VM arg?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

醉生梦死 2024-10-11 14:05:13

您认为为什么它不记录 DEBUG 消息?

如果您的意思是您的 log.debug(String) 日志记录调用不会最终出现在 java.util.logging 日志文件中,那么我想您必须配置 >logging.properties 配置文件,允许在 FINE 级别记录消息。

如果你不想弄乱全局%JRE_HOME%/lib/logging.properties,那么你可以传入-Djava.util.logging.config.file=logging.properties 在命令行上 - 这将强制日志系统在当前目录中查找该配置文件。

或者使用其他(编程)方式来配置 java.util.logging,请参阅下面的教程。

这与配置SLF4J无关;事实上,SLF4J 没有任何配置,一切都通过简单地交换 JAR 文件来配置


供您参考:

Why do you think it does not log DEBUG messages?

If you mean that your log.debug(String) logging calls do not end up in java.util.logging log files, then I guess you have to configure the logging.properties configuration file to allow log messages at FINE level.

If you do not want to mess with the global %JRE_HOME%/lib/logging.properties, then you can just pass in -Djava.util.logging.config.file=logging.properties on the command line - this will force the logging system to look for that configuration file in the current directory.

Or use some other (programmatic) way to configure java.util.logging, see below for tutorial.

This has nothing to do with configuring SLF4J; in fact, SLF4J does not have any configuration, everything is configured by simply swapping JAR files.


For your reference:

吹泡泡o 2024-10-11 14:05:13

如果您使用 slf4j SimpleLogger 实现,请阅读本文

在那里您可以看到 simpleLogger 使用 INFO 作为默认日志级别。您可以使用系统属性来更改它。这对于非生产环境很有用:

static {

    System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "trace");
}

If you are using slf4j SimpleLogger implementation read this.

There you can see that simpleLogger use INFO as default log level. You can change it by using a system property. This is usefull for non-production evironments:

static {

    System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "trace");
}
第七度阳光i 2024-10-11 14:05:13

您可以将 -Dorg.slf4j.simpleLogger.defaultLogLevel=debug 添加到 VM 选项。

You can add -Dorg.slf4j.simpleLogger.defaultLogLevel=debug to the VM options.

怪异←思 2024-10-11 14:05:13

我只是将我的logging.properties文件放在我的应用程序WEB-INF/classes文件中(或者使用Neeme Praks识别的命令行参数,如果你没有在战争中部署),并在Eclipse中打开属性文件,这样我就可以对其进行微调以记录包和我感兴趣的级别。

在logging.properties文件中,您需要确保记录器级别和处理程序级别都设置为您想要的级别。例如,如果您希望输出发送到控制台,则至少需要具有以下内容:

#logging.properties file contents

#Define handlers
handlers=java.util.logging.ConsoleHandler

#Set handler log level
java.util.logging.ConsoleHandler.level=FINE

#Define your logger level
com.company.application.package.package.level=FINE

#Assign your handler to your logger
com.company.application.package.package.handlers=java.util.logging.ConsoleHandler

您提到了 slf4j-jdk14-1.6.1.jar。这提供了 slf4j 到 java.util.logging 的绑定。您需要将其添加到您的类路径中,但请确保您的类路径中也包含 slf4j api (slf4j-api-1.7.12.jar)。

我在此找到 示例logging.properties 文件链接对于创建各种记录器和处理程序很有用,可以让您对哪些日志进入控制台以及哪些日志进入文件进行细粒度控制:。

这是 slf4j 手册

I just put my logging.properties file in my applications WEB-INF/classes file (or use the command line argument identified by Neeme Praks if you're not deploying in a war), and have the properties file open in eclipse so I can fine tune it to log the packages and at the level I'm interested in.

In the logging.properties file, you need to make sure that both the logger level and the handler level are set to the level you want. For example, if you want your output to go to the console, you need to have at least the following:

#logging.properties file contents

#Define handlers
handlers=java.util.logging.ConsoleHandler

#Set handler log level
java.util.logging.ConsoleHandler.level=FINE

#Define your logger level
com.company.application.package.package.level=FINE

#Assign your handler to your logger
com.company.application.package.package.handlers=java.util.logging.ConsoleHandler

You mentioned the slf4j-jdk14-1.6.1.jar. This provides the slf4j binding to java.util.logging. You need to have that in your classpath, but make sure you also have the slf4j api (slf4j-api-1.7.12.jar) in your classpath as well.

I find the example logging.properties file in this link useful for creating a variety of loggers and handlers, to give you fine-grained control over what logs go to the console, and what logs go to a file:.

And here's the slf4j manual.

没企图 2024-10-11 14:05:13

如果您使用的是 lombok Slf4j

 package com.space.slf4j;

    import lombok.extern.slf4j.Slf4j;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;

    /**
     * @author anson
     * @date 2019/6/18 16:17
     */
    @Slf4j
    @RestController
    public class TestController {

        @RequestMapping("/log")
        public String testLog(){
            log.info("#########  info  #########");
            log.debug("#########  debug  #########");
            log.error("#########  error  #########");
            return null;
        }
    }

application.yml

logging:
   level:
      root: debug

if you are using lombok Slf4j

 package com.space.slf4j;

    import lombok.extern.slf4j.Slf4j;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;

    /**
     * @author anson
     * @date 2019/6/18 16:17
     */
    @Slf4j
    @RestController
    public class TestController {

        @RequestMapping("/log")
        public String testLog(){
            log.info("#########  info  #########");
            log.debug("#########  debug  #########");
            log.error("#########  error  #########");
            return null;
        }
    }

application.yml

logging:
   level:
      root: debug
夏至、离别 2024-10-11 14:05:13

在运行时使用默认配置,您可以使用以下代码启用它:

public class MyTest {

static {
    Logger rootLogger = Logger.getLogger("");
    rootLogger.setLevel(Level.ALL);
    rootLogger.getHandlers()[0].setLevel(Level.ALL);

}

我在 TestNG 单元内使用此代码。

In runtime with the default configuration you can enable it with this code:

public class MyTest {

static {
    Logger rootLogger = Logger.getLogger("");
    rootLogger.setLevel(Level.ALL);
    rootLogger.getHandlers()[0].setLevel(Level.ALL);

}

I'm using this code inside TestNG Unit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文