How to Configure Slf4J-Simple

How to configure slf4j-simple

It's either through system property

-Dorg.slf4j.simpleLogger.defaultLogLevel=debug

or simplelogger.properties file on the classpath

see https://www.slf4j.org/api/org/slf4j/simple/SimpleLogger.html for details

How to change slf4j logging level

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

How to set logging level for different packages in simplelogger.properties for SLF4J-simple

The SLF4J SimpleLogger has all its documentation in its Javadoc.

As it says,

org.slf4j.simpleLogger.log.a.b.c - Logging detail level for a SimpleLogger instance named "a.b.c". Right-side value must be one of "trace", "debug", "info", "warn", "error" or "off". When a SimpleLogger named "a.b.c" is initialized, its level is assigned from this property. If unspecified, the level of nearest parent logger will be used, and if none is set, then the value specified by org.slf4j.simpleLogger.defaultLogLevel will be used.

So you need to include a line like this in your simplelogger.properties:

org.slf4j.simpleLogger.log.com.xxx.yyy=error

If you start needing a more complex logging system than the SLF4J SimpleLogger, then you probably want to switch to using something like Log4j or Logback.

SLF4J: SimpleLogger is not logging trace and debug and also not logging exceptions

Problem #1 (trace and debug level is not logged)

From the doc of SimpleLogger:

org.slf4j.simpleLogger.defaultLogLevel - Default log level for all
instances of SimpleLogger. Must be one of ("trace", "debug", "info",
"warn", "error" or "off"). If not specified, defaults to "info".

Because the defult level is info, and trace and debug levels are "under" info, they will be filtered out by the logger and will not reach the appender.

So if you add the following line in the properties file:

org.slf4j.simpleLogger.defaultLogLevel = trace

you should see also messages with trace and debug level.

Problem #2 (the error trace is not logged properly)

You are catching IOExceptions, but an InvocationTargetException is actually thrown (which one is not catched), therefore the execution will never enter your catch block. What you see on the console is an unhandled runtime exception printed.

Suppress debug logs with slf4j simple logger

From the docs;

org.slf4j.simpleLogger.log.a.b.c - Logging detail level for a SimpleLogger instance named "a.b.c". Right-side value must be one of "trace", "debug", "info", "warn", "error" or "off". When a SimpleLogger named "a.b.c" is initialized, its level is assigned from this property

So, to set the log level for loggers in this package: springfox you should set this property in simplelogger.properties:

org.slf4j.simpleLogger.log.springfox=info

SLF4J with SimpleLogger: Is it possible to log to a file AND System.out?

Short Answer

You can't do that with SimipleLogger.

More Answer

Give up on SimpleLogger and move on to something else. You have options:

  1. Instead of using slf4j-simple-1.x.x.jar get logback (logback-classic.jar and logback-core.jar). With logback you can define two appenders; one for the file output and one for console (also-known-as System.out) output.

  2. Instead of using slf4j-simple.1.x.x.jar get xxx (substitute any logging system supported by slf4j) and blah blah blah (do the same as in 1 obove).

  3. SLF4j is open source; derive your own logger (lets call it TeeLogger) that logs to System.out and a file.

  4. Create a logging class that that sits in front of SLF4j (in your application). Have it take a Logger and a messasge then have it write the message to System.out and the Logger.

  5. Something that I have not though about.

Here is a (super simplistic) example of #4 above:

import org.slf4j.Logger;

public class LoggyLoo
{
public static void logZoreInfo(
final Logger logger,
final String message)
{
System.out.println(message);
logger.info(message);
}
}


Related Topics



Leave a reply



Submit