简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM