Jason S
Jason S

Reputation: 189626

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

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:

Upvotes: 46

Views: 77513

Answers (6)

moritz
moritz

Reputation: 5224

See this tutorial on jul:

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

But I would recommend to go for Logback

Also see an official "Java logging overview".

Upvotes: 15

rogerdpack
rogerdpack

Reputation: 66701

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.

Upvotes: -3

ylu
ylu

Reputation: 867

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.

Upvotes: 3

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.

Upvotes: 12

Vladislav Rastrusny
Vladislav Rastrusny

Reputation: 29955

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.

Upvotes: 3

jabbie
jabbie

Reputation: 2716

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

Upvotes: 0

Related Questions