31

By default slf4j, when using with jdk (slf4j-jdk14-1.6.1.jar), does not log debug messages. How do I enable them?

I can’t find info neither in the official docs, the web or here on how to enable it.

I found some info on (failed though) creating a file in %JDK_HOME%/lib and defining the level there, in a config file. However, I would like to define the level at compile-/run-time so I can both run and debug my app from my IDE with different logging levels.

Isn’t there some environment variable I can set, or VM arg?

Neeme Praks
  • 8,956
  • 5
  • 47
  • 47
Kissaki
  • 8,810
  • 5
  • 40
  • 42

6 Answers6

26

Why do you think it does not log DEBUG messages?

If you mean that your log.debug(String) logging calls do not end up in java.util.logging log files, then I guess you have to configure the logging.properties configuration file to allow log messages at FINE level.

If you do not want to mess with the global %JRE_HOME%/lib/logging.properties, then you can just pass in -Djava.util.logging.config.file=logging.properties on the command line - this will force the logging system to look for that configuration file in the current directory.

Or use some other (programmatic) way to configure java.util.logging, see below for tutorial.

This has nothing to do with configuring SLF4J; in fact, SLF4J does not have any configuration, everything is configured by simply swapping JAR files.


For your reference:

Neeme Praks
  • 8,956
  • 5
  • 47
  • 47
  • I want the debug messages to show up in my console (stdout / stderr). Using eclipse here. Also, as stated in the question, I’d prefer to set this at compile- or runtime instead of a static file which applies to all projects and all run-configurations. Is this possible? – Kissaki Nov 25 '10 at 16:08
  • 1
    did you try -Djava.util.logging.config.file=logging.properties? – Neeme Praks Nov 25 '10 at 16:14
  • Ok, I’ll try that. Thanks. (Better than nothing, although I’d still prefer var-/par-/env-setting instead of using a file.) – Kissaki Nov 25 '10 at 16:40
  • 1
    well, what you could do is one configuration file for all your projects and then point that -Djava.util.logging.config.file setting to that file. This way you could avoid creating a separate file for each project. And, if you do not want to specify anything on command line, I guess you could define an environment variable with name "java.util.logging.config.file" - if I remember correctly, JRE imports all environment variables as if they were specified on command line. – Neeme Praks Nov 25 '10 at 16:55
  • Hey Naeeme, just a heads up, your "Java Logging API and How To Use It" link is dead – CubeJockey Apr 05 '16 at 13:34
  • Thanks CubeJockey, I linked another tutorial. – Neeme Praks Apr 06 '16 at 08:14
14

If you are using slf4j SimpleLogger implementation read this.

There you can see that simpleLogger use INFO as default log level. You can change it by using a system property. This is usefull for non-production evironments:

static {

    System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "trace");
}
River
  • 8,585
  • 14
  • 54
  • 67
KingQuercus
  • 141
  • 1
  • 2
11

You can add -Dorg.slf4j.simpleLogger.defaultLogLevel=debug to the VM options.

Antoine
  • 111
  • 1
  • 2
4

I just put my logging.properties file in my applications WEB-INF/classes file (or use the command line argument identified by Neeme Praks if you're not deploying in a war), and have the properties file open in eclipse so I can fine tune it to log the packages and at the level I'm interested in.

In the logging.properties file, you need to make sure that both the logger level and the handler level are set to the level you want. For example, if you want your output to go to the console, you need to have at least the following:

#logging.properties file contents

#Define handlers
handlers=java.util.logging.ConsoleHandler

#Set handler log level
java.util.logging.ConsoleHandler.level=FINE

#Define your logger level
com.company.application.package.package.level=FINE

#Assign your handler to your logger
com.company.application.package.package.handlers=java.util.logging.ConsoleHandler

You mentioned the slf4j-jdk14-1.6.1.jar. This provides the slf4j binding to java.util.logging. You need to have that in your classpath, but make sure you also have the slf4j api (slf4j-api-1.7.12.jar) in your classpath as well.

I find the example logging.properties file in this link useful for creating a variety of loggers and handlers, to give you fine-grained control over what logs go to the console, and what logs go to a file:.

And here's the slf4j manual.

stites
  • 4,903
  • 5
  • 32
  • 43
Jibby
  • 850
  • 1
  • 9
  • 21
  • Plus one for this! Didn't realize I had to set the level for both the handler and the logger. – Pubudu Jul 03 '17 at 06:53
1

if you are using lombok Slf4j

 package com.space.slf4j;

    import lombok.extern.slf4j.Slf4j;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;

    /**
     * @author anson
     * @date 2019/6/18 16:17
     */
    @Slf4j
    @RestController
    public class TestController {

        @RequestMapping("/log")
        public String testLog(){
            log.info("#########  info  #########");
            log.debug("#########  debug  #########");
            log.error("#########  error  #########");
            return null;
        }
    }

application.yml

logging:
   level:
      root: debug
anson
  • 1,436
  • 14
  • 16
0

In runtime with the default configuration you can enable it with this code:

public class MyTest {

static {
    Logger rootLogger = Logger.getLogger("");
    rootLogger.setLevel(Level.ALL);
    rootLogger.getHandlers()[0].setLevel(Level.ALL);

}

I'm using this code inside TestNG Unit.

Daniel De León
  • 13,196
  • 5
  • 87
  • 72