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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4278438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 15:17:37  来源:igfitidea点击:

slf4j logging with jdk – how to enable debug?

javaloggingslf4jjava.util.logging

提问by Kissaki

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

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

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.

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

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

是不是有一些我可以设置的环境变量,或者 VM arg?

采纳答案by Neeme Praks

Why do you think it does not log DEBUG messages?

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

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

如果您的意思是您的log.debug(String)日志记录调用不会以java.util.logging日志文件结束,那么我猜您必须配置logging.properties配置文件以允许FINE级别的日志消息。

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.propertieson the command line - this will force the logging system to look for that configuration file in the current directory.

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

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

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

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

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



For your reference:

供你参考:

回答by Jibby

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.

我只是将我的 logging.properties 文件放在我的应用程序 WEB-INF/classes 文件中(或者,如果您不是在War中部署,则使用 Neeme Praks 标识的命令行参数),并在 eclipse 中打开属性文件,这样我就可以微调它以记录包和我感兴趣的级别。

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 文件中,您需要确保将记录器级别和处理程序级别都设置为您想要的级别。例如,如果您希望输出到控制台,则至少需要具备以下条件:

#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.

你提到了slf4j-jdk14-1.6.1.jar. 这提供了对 java.util.logging 的 slf4j 绑定。你需要在你的类路径中有它,但要确保你的类路径中也有 slf4j api ( slf4j-api-1.7.12.jar) 。

I find the example logging.properties file in this linkuseful 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:.

我发现此链接中的示例 logging.properties 文件对于创建各种记录器和处理程序很有用,让您可以精细控制哪些日志进入控制台,哪些日志进入文件:。

And here's the slf4j manual.

是 slf4j 手册

回答by KingQuercus

If you are using slf4j SimpleLogger implementation read this.

如果您使用 slf4j SimpleLogger 实现,请阅读此内容

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

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

static {

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

回答by anson

if you are using lombok Slf4j

如果您使用的是 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

应用程序.yml

logging:
   level:
      root: debug

回答by Antoine

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

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