14

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");
  • I don't think there is a way to call a setter like in your code sample because setting the level of a logger is an implementation level concern not something the SLF4J interface specifies. You could probably find a way to do it using code specific to your logging implementation, but then your code depends on the logging implementation which is exactly what SLF4J is meant to help you avoid. – D.B. Sep 04 '17 at 03:49
  • For slf4j used in a spring-boot project with Lombok, this was helpful to me: https://stackoverflow.com/questions/43901810/spring-boot-logging-with-lombok – Razzle Mar 12 '20 at 09:33

2 Answers2

15

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");
}
4

.properties file

You said:

I read about create a config-file or xml-file, but I don't know where to put these files in a EJB-Project.

You can create a .properties file for your logger. That file must appear on the classpath, appropriately.

In a Servlet-based project driven by Maven (a Vaadin 14 project), I place a simplelogger.properties file with this content (Atmosphere being a library used by Vaadin) for the SimpleLogger implementation:

org.slf4j.simpleLogger.defaultLogLevel = error
org.slf4j.simpleLogger.log.org.atmosphere = warn

…in the resources folder of my IntelliJ project.

In the target created at build time, this file appears in: myProjectName > WEB-INF > classes > simplelogger.properties.

This project is aimed at a web container such as Eclipse Jetty or Apache Tomcat. I do not use a full EJB server like Glassfish. Not sure if this helps you, but might give you a clue.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154