43

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)

Jason S
  • 184,598
  • 164
  • 608
  • 970
  • 1
    Did you not find those in the SL4J documentation website? http://www.slf4j.org/docs.html – Edwin Dalorzo Apr 15 '11 at 19:13
  • I don't get what you want to achieve, do you want to use slf4j's logger in the code and java.logging for output the logging or the other way? – moritz Apr 15 '11 at 19:18
  • slf4j's API, java.util.logging for the underlying implementation, that works fine so far, but I don't know how to configure java.util.logging via either the slf4j API or a .properties file or a JVM `-D` parameter. – Jason S Apr 15 '11 at 19:21
  • ...where I would rather use .properties files and not configure it programmatically. – Jason S Apr 15 '11 at 19:21

6 Answers6

14

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

Nishi
  • 10,634
  • 3
  • 27
  • 36
moritz
  • 5,094
  • 1
  • 26
  • 33
  • 4
    There is one distinct advantage of using j.u.l, namely that it is easy to use across classloaders. We use it to propagate log messages out of a web application (WAR) into the servlet containers logs. – Thorbjørn Ravn Andersen Apr 15 '11 at 20:06
12

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.

Ceki
  • 26,753
  • 7
  • 62
  • 71
Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
  • "the slf4j site is down right now so I cannot check the manual page or the FAQ)" -- exactly. I have slf4j-api-*.jar and slf4j-jdk14-*.jar in my classpath, I just don't know how to "do the magic listed in the javadoc" to get it to redirect to a file instead of stdout. – Jason S Apr 15 '11 at 19:19
  • 1
    If you get log messages to your console, it means that the logging system works correctly. You then need to configure it _COMPLETELY AS YOU WOULD OTHERWISE_ to have anything work differently. I.e. for jdk logging you need to do exactly the same work with properties and for log4j you need to provide a log4j.xml file telling it what to do. – Thorbjørn Ravn Andersen Apr 15 '11 at 19:21
  • OK, then I guess my question becomes where's the docs on jdk14 logging configuration. – Jason S Apr 15 '11 at 19:24
  • Here perhaps? http://download.oracle.com/javase/1.4.2/docs/guide/util/logging/overview.html#1.8 – Thorbjørn Ravn Andersen Apr 15 '11 at 19:31
  • @ThorbjørnRavnAndersen that link is broken. – Joe Sep 30 '15 at 18:42
  • 1
    @Joe Probably EOL'ed by Oracle. For Java 8 it is at https://docs.oracle.com/javase/8/docs/technotes/guides/logging/overview.html#a1.8 – Thorbjørn Ravn Andersen Feb 12 '18 at 13:08
3

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.

ylu
  • 867
  • 8
  • 5
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.

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

Vladislav Rastrusny
  • 29,378
  • 23
  • 95
  • 156
  • 5
    Although I agree, this doesn't answer the question. To warrant the up-votes, include something like: similar to logback using `java -Dlogback.configurationFile=/path/to/config.xml`, j.u.l. can use: `java -Djava.util.logging.config.file=myLoggingConfig` w/ either a [properties file](http://stackoverflow.com/questions/805701/load-java-util-logging-config-file-for-default-initialization) or [class](http://docs.oracle.com/javase/1.4.2/docs/api/java/util/logging/LogManager.html) . Logback by default uses `logback.xml` from the classpath; j.u.l uses `logging.properties` from your Java install dir. – michael Feb 12 '13 at 00:25
0

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

jabbie
  • 2,606
  • 1
  • 17
  • 9
  • 8
    ...and of course, to actually _answer the original question_ (why is everyone up-voting non-answers?): one can of course use `java.util.logging` as the logging implementation by putting `slf4j-jdk14-{slf4j-version}.jar` in the classpath instead of any other `slf4j-{binding}.jar`; then set the j.u.l. config using the `-Djava.util.logging.config.file` property. (I _would_ downvote, but won't since I also prefer logback :-) . But I also like _answers_ .) – michael Feb 12 '13 at 00:33
-3

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.

rogerdpack
  • 62,887
  • 36
  • 269
  • 388
  • Are you specifying a path to use? If so, can you clarify why it works and in which version of tomcat? – Andreas Jun 23 '17 at 20:29
  • @Andreas wherever your webapps folder lives. This was with tomcat 6 I believe but I think it doesn't matter, since it's more of a log4j thing. Cheers! – rogerdpack Jun 23 '17 at 20:56