How to change slf4j logging level
Asked Answered
C

3

16

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");
Crabbed answered 1/9, 2017 at 10:1 Comment(2)
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.Nonanonage
For slf4j used in a spring-boot project with Lombok, this was helpful to me: #43902310Furore
C
14

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");
}
Crabbed answered 5/9, 2017 at 10:20 Comment(3)
I needed this for JGit - and this was a life-saver!Brilliancy
Important: this setProperty must be executed before the logger is loaded.Pickmeup
That no longer works. The class org.slf4j.impl.SimpleLogger no longer exists.Salahi
N
6

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

Natator answered 30/12, 2019 at 4:33 Comment(1)
Thanks I used it like java -Dorg.slf4j.simpleLogger.defaultLogLevel=debug -jar app.jar and that works.Flesh
B
1

To set Logger level for specific class, it depends how did you initialize it.

In my case I initialized logger name just by string: logger = LoggerFactory.getLogger("MetricsService");

So in application.properties I had to use: logging.level.MetricsService=DEBUG

But if you using the second method with .class signature: logger = LoggerFactory.getLogger(MetricsService.class); , then you should provide the fully qualified path

Basaltware answered 26/5 at 14:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.