slf4j + java.util.logging: how to configure?

2019-02-11 10:39发布

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)

6条回答
手持菜刀,她持情操
2楼-- · 2019-02-11 11:08

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.

查看更多
聊天终结者
3楼-- · 2019-02-11 11:19

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

查看更多
仙女界的扛把子
4楼-- · 2019-02-11 11:22

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)

查看更多
我命由我不由天
5楼-- · 2019-02-11 11:26

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.

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

查看更多
姐就是有狂的资本
6楼-- · 2019-02-11 11:28

See this tutorial on jul:

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

But I would recommend to go for Logback

查看更多
可以哭但决不认输i
7楼-- · 2019-02-11 11:31

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.

查看更多
登录 后发表回答