How to configure slf4j-simple

JavaLoggingSlf4j

Java Problem Overview


api 1.7 and slf4j-simple as implementation. I just can't find how to configure the logging level with this combination.

Can anyone help out?

Java Solutions


Solution 1 - Java

It's either through system property

-Dorg.slf4j.simpleLogger.defaultLogLevel=debug

or simplelogger.properties file on the classpath

see http://www.slf4j.org/api/org/slf4j/impl/SimpleLogger.html for details

Solution 2 - Java

This is a sample simplelogger.properties which you can place on the classpath (uncomment the properties you wish to use):

# SLF4J's SimpleLogger configuration file
# Simple implementation of Logger that sends all enabled log messages, for all defined loggers, to System.err.

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

# Logging detail level for a SimpleLogger instance named "xxxxx".
# Must be one of ("trace", "debug", "info", "warn", or "error").
# If not specified, the default logging detail level is used.
#org.slf4j.simpleLogger.log.xxxxx=

# Set to true if you want the current date and time to be included in output messages.
# Default is false, and will output the number of milliseconds elapsed since startup.
#org.slf4j.simpleLogger.showDateTime=false

# The date and time format to be used in the output messages.
# The pattern describing the date and time format is the same that is used in java.text.SimpleDateFormat.
# If the format is not specified or is invalid, the default format is used.
# The default format is yyyy-MM-dd HH:mm:ss:SSS Z.
#org.slf4j.simpleLogger.dateTimeFormat=yyyy-MM-dd HH:mm:ss:SSS Z

# Set to true if you want to output the current thread name.
# Defaults to true.
#org.slf4j.simpleLogger.showThreadName=true

# Set to true if you want the Logger instance name to be included in output messages.
# Defaults to true.
#org.slf4j.simpleLogger.showLogName=true

# Set to true if you want the last component of the name to be included in output messages.
# Defaults to false.
#org.slf4j.simpleLogger.showShortLogName=false

In a Maven or Gradle project, a convenient place "on the classpath" is src/main/resources/simplelogger.properties.

Solution 3 - Java

You can programatically change it by setting the system property:

public class App {
  public static void main(String[] args) {
    // for the code below to work, it must be executed before the// logger is created. see note below
    System.setProperty(org.slf4j.impl.SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "TRACE");
       
    ​org.slf4j.Logger log = LoggerFactory.getLogger(App.class);
       ​
    ​log.trace("trace");
    ​log.debug("debug");
    ​log.info("info");
    ​log.warn("warning");
    ​log.error("error");
  ​}
​}

The log levels are ERROR > WARN > INFO > DEBUG > TRACE.

Please note that once the logger is created the log level can't be changed. If you need to dynamically change the logging level you might want to use log4j with SLF4J.

Solution 4 - Java

I noticed that Eemuli said that you can't change the log level after they are created - and while that might be the design, it isn't entirely true.

I ran into a situation where I was using a library that logged to slf4j - and I was using the library while writing a maven mojo plugin.

Maven uses a (hacked) version of the slf4j SimpleLogger, and I was unable to get my plugin code to reroute its logging to something like log4j, which I could control.

And I can't change the maven logging config.

So, to quiet down some noisy info messages, I found I could use reflection like this, to futz with the SimpleLogger at runtime.

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.spi.LocationAwareLogger;
    try
	{
		Logger l = LoggerFactory.getLogger("full.classname.of.noisy.logger");  //This is actually a MavenSimpleLogger, but due to various classloader issues, can't work with the directly.
		Field f = l.getClass().getSuperclass().getDeclaredField("currentLogLevel");
		f.setAccessible(true);
		f.set(l, LocationAwareLogger.WARN_INT);
	}
	catch (Exception e)
	{
		getLog().warn("Failed to reset the log level of " + loggerName + ", it will continue being noisy.", e);
	}

Of course, note, this isn't a very stable / reliable solution... as it will break the next time the maven folks change their logger.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGelin LuoView Question on Stackoverflow
Solution 1 - JavaEvgeniy DorofeevView Answer on Stackoverflow
Solution 2 - JavaRobert HuntView Answer on Stackoverflow
Solution 3 - JavaeemelipaView Answer on Stackoverflow
Solution 4 - Javauser2163960View Answer on Stackoverflow