使用 jdk 进行 slf4j 日志记录 –如何启用调试?
默认情况下,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您认为为什么它不记录 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 injava.util.logging
log files, then I guess you have to configure thelogging.properties
configuration file to allow log messages atFINE
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:
如果您使用 slf4j SimpleLogger 实现,请阅读本文。
在那里您可以看到
simpleLogger
使用INFO
作为默认日志级别。您可以使用系统属性来更改它。这对于非生产环境很有用:If you are using slf4j SimpleLogger implementation read this.
There you can see that
simpleLogger
useINFO
as default log level. You can change it by using a system property. This is usefull for non-production evironments:您可以将
-Dorg.slf4j.simpleLogger.defaultLogLevel=debug
添加到 VM 选项。You can add
-Dorg.slf4j.simpleLogger.defaultLogLevel=debug
to the VM options.我只是将我的logging.properties文件放在我的应用程序WEB-INF/classes文件中(或者使用Neeme Praks识别的命令行参数,如果你没有在战争中部署),并在Eclipse中打开属性文件,这样我就可以对其进行微调以记录包和我感兴趣的级别。
在logging.properties文件中,您需要确保记录器级别和处理程序级别都设置为您想要的级别。例如,如果您希望输出发送到控制台,则至少需要具有以下内容:
您提到了
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:
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.
如果您使用的是 lombok Slf4j
application.yml
if you are using lombok Slf4j
application.yml
在运行时使用默认配置,您可以使用以下代码启用它:
我在
TestNG
单元内使用此代码。In runtime with the default configuration you can enable it with this code:
I'm using this code inside
TestNG
Unit.