slf4j + java.util.logging:如何配置?
我正在尝试使用 slf4j + java.util.logging。我知道如何设置 Java 源代码来通过 logger = LoggerFactory.getLogger(...)
和 logger.warn('...'
) 或任何。
但是在 slf4j 中设置配置的文档在哪里?我很困惑...我有 log4j 手册并且熟悉日志记录适配器的基础知识,但我只是不知道确定如何让它与 slf4j + java.util.logging 一起使用。
即:
我需要指定哪个 .properties 文件和/或 JVM
-D
命令行参数以将其指向我的配置文件?java.util.logging 配置文件的文档在哪里?
使用 slf4j 是否会导致我的配置文件发生任何更改? (即我必须以不同方式声明的内容,而不是直接使用 java.util.logging 或 log4j)
I'm trying to use slf4j + java.util.logging. I know how to set up the Java source code to do this via logger = LoggerFactory.getLogger(...)
and logger.warn('...'
) or whatever.
But where's the documentation for setting up the configuration in slf4j? I'm very confused... I have the log4j manual and am familiar w/ the very basics of logging adapters, but I'm just not sure how to get this to work with slf4j + java.util.logging.
namely:
which .properties file and/or JVM
-D
command-line argument do I need to specify to point it at my configuration file?where's the documentation for the configuration file for java.util.logging?
does using slf4j cause any change in my configuration file? (i.e. something that I would have to declare differently, vs. just using java.util.logging or log4j directly)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
请参阅7 月的本教程:
但我建议选择 Logback
另请参阅官方“Java 日志记录概述”。
See this tutorial on jul:
But I would recommend to go for Logback
Also see an official "Java logging overview".
slf4j层没有配置。它只是一个 API,后端必须提供它的实现(或多或少)。
要使用 java.util.logging 作为 slf4j 后端,您的类路径中必须有 slf4j 发行版中的 slf4j-jdk14-mumle.jar,并执行 javadoc 中列出的魔法来启用它。如果没有,您将收到运行时错误,指出没有活动的 slf4j 实现。
There is no configuration in the slf4j layer. It is just an API, which the backend must provide the implementation for (more or less).
To use java.util.logging as the slf4j backend, you must have slf4j-jdk14-mumle.jar from the slf4j distribution on your classpath, and do the magic listed in the javadoc to enable it. If not you will have a runtime error saying there is no slf4j implementation active.
为了同样的目的,我放弃了 Java 日志记录,转而使用 logback。实际上,使用 SLF4J 配置 logback 无需执行任何操作。只需将 logback.xml 放置到具有 logback 配置的 jar 的根目录中,并将 logback-XX.jar 放置在类路径上即可。
这是用于记录到控制台的配置,但 logback 手册有更多示例。
I've dropped Java logging on the same purpose and went for logback. There is nothing to do to configure logback with SLF4J actually. Just put logback.xml to the root of the jar with logback configuration and put logback-XX.jar on classpath.
This is a config for logging to console, but logback manual has more examples.
基本上,您只需要在类路径上有 slf4j-api.1.7.7.jar 和 slf4j-jdk14.1.7.7.jar 即可。然后为您的应用程序创建一个logging.properties文件并设置系统属性
-Djava.util.logging.config.file={path_to_logging.properties}
检查 本教程为您提供了如何使用不同的日志框架(例如 java logger、log4j、logback 等)配置 slf4j 的示例。
Basically, you just need to have slf4j-api.1.7.7.jar and slf4j-jdk14.1.7.7.jar on your classpath. And then create a logging.properties file for your application and set System properties
-Djava.util.logging.config.file={path_to_logging.properties}
check this tutorial out which gives you examples how to configure slf4j with different logging frameworks such as java logger, log4j, logback etc.
您不需要配置 SLF4J - 您需要绑定和配置提供程序。我使用 Logback,它是专门为 SLF4J 构建的。您还可以使用 log4j。请参阅手册中的相关条目:http://www.slf4j.org/manual.html#binding
You don't configure SLF4J - you need to bind and configure the provider. I use Logback, which was built specifically for SLF4J. You could also use log4j. See the relevant entry in the manual: http://www.slf4j.org/manual.html#binding
有时,您似乎可以使用这样的文件名来编辑文件:
tomcat-directory/webapps/your_app_name/WEB-INF/classes/log4j.properties
(标准 log4j 配置文件)。无论如何为我工作。
Sometimes it appears you can get away with editing your file with a filename like this
tomcat-directory/webapps/your_app_name/WEB-INF/classes/log4j.properties
(standard log4j config file). Worked for me anyway.