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

后端 未结 6 880
孤独总比滥情好
孤独总比滥情好 2021-01-01 09:35

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

相关标签:
6条回答
  • 2021-01-01 09:43

    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

    0 讨论(0)
  • 2021-01-01 09:52

    See this tutorial on jul:

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

    But I would recommend to go for Logback

    0 讨论(0)
  • 2021-01-01 09:52

    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.

    0 讨论(0)
  • 2021-01-01 09:52

    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 讨论(0)
  • 2021-01-01 09:55

    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.

    0 讨论(0)
  • 2021-01-01 09:56

    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.

    0 讨论(0)
提交回复
热议问题