slf4j + java.util.logging:如何配置?

时间:2021-06-08 22:03:45

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.

我正在尝试使用slf4j + java.util.logging。我知道如何通过logger = LoggerFactory.getLogger(...)和logger.warn('...')等设置Java源代码。

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.

但是在slf4j中设置配置的文档在哪里?我很困惑...我有log4j手册,熟悉日志适配器的基础知识,但我不知道如何使用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?

    我需要指定哪个.properties文件和/或JVM -D命令行参数指向我的配置文件?

  • where's the documentation for the configuration file for java.util.logging?

    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)

    使用slf4j会导致我的配置文件发生任何变化吗? (即我必须以不同方式声明的东西,而不是直接使用java.util.logging或log4j)

6 个解决方案

#1


13  

See this tutorial on jul:

在jul上看到这个教程:

java -Djava.util.logging.config.file=myLoggingConfigFilePath

But I would recommend to go for Logback

但我建议去Logback

#2


11  

There is no configuration in the slf4j layer. It is just an API, which the backend must provide the implementation for (more or less).

slf4j层中没有配置。它只是一个API,后端必须为(或多或少)提供实现。

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.util.logging作为slf4j后端,必须从类路径上的slf4j发行版中获取slf4j-jdk14-mumle.jar,并执行javadoc中列出的魔术来启用它。如果不是,您将遇到运行时错误,表示没有slf4j实现处于活动状态。

#3


3  

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.

我已经将Java日志记录用于相同的目的并进行了回溯。实际上,使用SLF4J配置回溯无关。只需将logback.xml放入带有logback配置的jar的根目录,然后将logback-XX.jar放在classpath上。

<configuration>

  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <!-- encoders are assigned the type
         ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>

  <root level="warn">
    <appender-ref ref="STDOUT" />
  </root>
</configuration>

This is a config for logging to console, but logback manual has more examples.

这是用于登录到控制台的配置,但是logback手册有更多示例。

#4


2  

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

您没有配置SLF4J - 您需要绑定和配置提供程序。我使用专为SLF4J构建的Logback。你也可以使用log4j。请参阅手册中的相关条目:http://www.slf4j.org/manual.html#binding

#5


2  

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}

基本上,您只需要在类路径上使用slf4j-api.1.7.7.jar和slf4j-jdk14.1.7.7.jar。然后为您的应用程序创建logging.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.

查看本教程,其中给出了如何使用不同的日志框架(如java logger,log4j,logback等)配置slf4j的示例。

#6


-2  

Sometimes it appears you can get away with editing your file like this

有时看起来你可以像这样编辑你的文件

tomcat-directory/webapps/your_app_name/WEB-INF/classes/log4j.properties

(standard log4j config file)

(标准log4j配置文件)

#1


13  

See this tutorial on jul:

在jul上看到这个教程:

java -Djava.util.logging.config.file=myLoggingConfigFilePath

But I would recommend to go for Logback

但我建议去Logback

#2


11  

There is no configuration in the slf4j layer. It is just an API, which the backend must provide the implementation for (more or less).

slf4j层中没有配置。它只是一个API,后端必须为(或多或少)提供实现。

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.util.logging作为slf4j后端,必须从类路径上的slf4j发行版中获取slf4j-jdk14-mumle.jar,并执行javadoc中列出的魔术来启用它。如果不是,您将遇到运行时错误,表示没有slf4j实现处于活动状态。

#3


3  

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.

我已经将Java日志记录用于相同的目的并进行了回溯。实际上,使用SLF4J配置回溯无关。只需将logback.xml放入带有logback配置的jar的根目录,然后将logback-XX.jar放在classpath上。

<configuration>

  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <!-- encoders are assigned the type
         ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>

  <root level="warn">
    <appender-ref ref="STDOUT" />
  </root>
</configuration>

This is a config for logging to console, but logback manual has more examples.

这是用于登录到控制台的配置,但是logback手册有更多示例。

#4


2  

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

您没有配置SLF4J - 您需要绑定和配置提供程序。我使用专为SLF4J构建的Logback。你也可以使用log4j。请参阅手册中的相关条目:http://www.slf4j.org/manual.html#binding

#5


2  

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}

基本上,您只需要在类路径上使用slf4j-api.1.7.7.jar和slf4j-jdk14.1.7.7.jar。然后为您的应用程序创建logging.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.

查看本教程,其中给出了如何使用不同的日志框架(如java logger,log4j,logback等)配置slf4j的示例。

#6


-2  

Sometimes it appears you can get away with editing your file like this

有时看起来你可以像这样编辑你的文件

tomcat-directory/webapps/your_app_name/WEB-INF/classes/log4j.properties

(standard log4j config file)

(标准log4j配置文件)