javaloggingejbslf4j

How to change slf4j logging level


I'm using the org.slf4j.Logger in a Java EJB-Project running on a glassfish 3.1.2 server.

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class main {

private static final Logger LOGGER = LoggerFactory.getLogger(main.class);

public static void main(String[] args) {
  LOGGER.info("--- show this everytime");
  if(LOGGER.isDebugEnabled()) {
        LOGGER.debug("--- show this only if debug is enable");
  }
  LOGGER.info("--- show this everytime");
}

So my Problem is, I don't know how to turn on/off the different log level (info, debug, error, trace, warn). I read about create a config-file or xml-file, but I don't know where to put these files in a EJB-Project. And is there a way to configure it like this?

LOGGER.setLevel("info");

Solution

  • I found the solution. I'm able to change the logging level with the following line:

    System.setProperty(org.slf4j.impl.SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "Info");
    

    I also have to add the slf4j-api-1.7.25.jar and slf4j-simple-1.7.25.jar to the Build Path and add the jars to the glassfish lib.

    The complete code looks like this:

    import org.slf4j.LoggerFactory;
    
    public class main {
    
    public static void main(String[] args) {
    
       System.setProperty(org.slf4j.impl.SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "Info");
       final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(LogTest.class);
    
       LOGGER.info("--- show this everytime");
       if(LOGGER.isDebugEnabled()) {
            LOGGER.debug("--- show this only if debug is enable");
       }
    
       LOGGER.info("--- show this everytime");
    }