开发者

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

开发者 https://www.devze.com 2023-02-25 16:36 出处:网络
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.

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)


See this tutorial on jul:

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

But I would recommend to go for Logback


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.


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.


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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消