211

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?

Gelin Luo
  • 14,035
  • 27
  • 86
  • 139
  • 1
    The default is INFO to stdout, FWIW: http://saltnlight5.blogspot.com/2013/08/how-to-configure-slf4j-with-different.html – rogerdpack Dec 04 '18 at 19:04

5 Answers5

287

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

Sergey Ushakov
  • 2,425
  • 1
  • 24
  • 15
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • thanks I set "org.slf4j.simpleLogger.defaultLogLevel" to "error" in System.properties, however slf4j still log INFO level messages. Any idea? BTW, where should I put simplelogger.properties? – Gelin Luo Jan 27 '13 at 09:27
  • 2
    try org.slf4j.simplelogger.defaultlog instead of org.slf4j.simpleLogger.defaultLogLevel. File must be on the classpath, default package – Evgeniy Dorofeev Jan 27 '13 at 09:55
  • 2
    Actually it (`defaultLogLevel`) works.Just found I was modifying the program in a wrong folder ;-) And `defaultlog` doesn't work. So you probably want to edit your answer though I've accepted it – Gelin Luo Jan 27 '13 at 10:08
  • 11
    Just a note: actually both answers are good, depending on the version of SimpleLogger you're using. For example, defaultLogLevel works for 1.7.5, but defaultlog works for 1.6.6. I found this out when configuring my project logging and coming upon this post – Ken Shih Oct 16 '13 at 14:31
  • I spent a bunch of time wondering why this wasn't working, and eventually realized that I had capitalized the `L` in `simplelogger.properties`. It's supposed to be all lowercase! – Dylan Mar 05 '21 at 18:18
161

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.

Robert Hunt
  • 7,914
  • 5
  • 40
  • 43
  • 1
    @RobertHunt How to save this log into file ? – Devavrata Jun 28 '15 at 21:56
  • 8
    @Devavrata add the property `org.slf4j.simpleLogger.logFile` - The output target which can be the path to a file, or the special values "System.out" and "System.err". Default is "System.err". See http://www.slf4j.org/api/org/slf4j/impl/SimpleLogger.html – Robert Hunt Jun 28 '15 at 22:01
  • Is it possible to have multiple values? If yes how? Like I want org.slf4j.simpleLogger.logFile=test.log, System.err? – LOLWTFasdasd asdad Dec 04 '19 at 10:58
  • 2
    @LOLWTFasdasdasdad Unfortunately not, it only supports single targets, either System.out, System.err or a path to a file. It's designed to be simple, you should consider a full logging implementation like Log4J or Logback if you want more advanced features. – Robert Hunt Dec 04 '19 at 13:18
  • I saved this file in the class path and removed the comments but still see the logs. – zendevil Dec 18 '20 at 08:45
  • Is this really working? my logger doesnt seem to pick this file from resource classpath. using bazel and scala. – ss301 Feb 21 '22 at 19:06
  • How can I keep simplelogger.properties file outside at some system path rather keeping it in classpath? – Jaraws Mar 09 '22 at 17:53
91

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.

acdcjunior
  • 132,397
  • 37
  • 331
  • 304
eemelipa
  • 1,180
  • 8
  • 11
  • 3
    "Please note that once the logger is created the log level can't be changed." - Where is this actually specified? – ksl Nov 25 '15 at 10:24
  • 2
    ksl, in org.slf4j.impl.SimpleLogger. When the first logger is created, init() method is run and it fetches the default logging level from system properties. This isn't refreshed at any point. Also, org.slf4j.impl.SimpleLoggerFactory creates a logger for a class only once, thus, the same logger is always returned for given class (or name). However, it is possible to have loggers with different level. So possible workaround could be that you assign these different level loggers to your "log" variable when you want to change logging level. It is not very neat solution but should work. – eemelipa Nov 28 '15 at 13:17
  • 1
    @Eemuli By `org.slf4j.impl.SimpleLogger` you mean the actual source code rather than doc? – ksl Jan 26 '16 at 12:08
  • Is it also true that the `LOG_FILE_KEY` property can't be changed either once the logger is created? – ksl Jan 26 '16 at 12:09
  • 1
    Yes, I mean the actual source code. I'm not sure about LOG_FILE_KEY. – eemelipa Feb 06 '16 at 17:37
  • 1
    I got a 'Cannot resolve symbol SimpleLogger` error. – Anthony Kong May 27 '20 at 10:36
  • Does that tightly couple your app to the SimpleLogger? – ingyhere Oct 11 '21 at 22:47
  • If I remember correctly, yes, it tightly couples the app to SimpleLogger. Main reason being that we use "org.slf4j.impl.SimpleLogger.DEFAULT_LOG_LEVEL_KEY" object. Also setting the log level like this is only SimpleLogger specific so probably doesn't work with other loggers. – eemelipa Oct 17 '21 at 07:51
  • @AnthonyKong I had to add two import lines: `import static org.slf4j.impl.SimpleLogger.DEFAULT_LOG_LEVEL_KEY;` and `import static org.slf4j.impl.SimpleLogger.SHOW_THREAD_NAME_KEY;`, as well as a Maven dependency: ` org.slf4j slf4j-simple 2.0.5 ` – jtpereyda Mar 23 '23 at 20:57
4

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.

user2163960
  • 1,871
  • 19
  • 22
0

I don't know why. I use simplelogger.properties and org.slf4j.simpleLogger.showDatetime, it's not working.

I lookup the SimpleLogger class source code and got this part of the code

static {
      // Add props from the resource simplelogger.properties
      InputStream in = (InputStream)AccessController.doPrivileged(
              new PrivilegedAction() {
                  public Object run() {
                      ClassLoader threadCL = Thread.currentThread().getContextClassLoader();
                      if (threadCL != null) {
                          return threadCL.getResourceAsStream(CONFIGURATION_FILE);
                      } else {
                          return ClassLoader.getSystemResourceAsStream(CONFIGURATION_FILE);
                      }
                  }
              });
      if(null != in) {
          try {
              simpleLoggerProps.load(in);
              in.close();
          } catch(java.io.IOException e) {
              // ignored
          }
      }

      showLogName    = getBooleanProperty(systemPrefix + "showlogname",      showLogName);
      showShortName  = getBooleanProperty(systemPrefix + "showShortLogname", showShortName);
      showDateTime   = getBooleanProperty(systemPrefix + "showdatetime",     showDateTime);
      showThreadName = getBooleanProperty(systemPrefix + "showthreadname",   showThreadName);
      dateTimeFormat = getStringProperty(systemPrefix + "dateTimeFormat",    dateTimeFormat);

      if(showDateTime) {
          try {
              dateFormatter = new SimpleDateFormat(dateTimeFormat);
          } catch(IllegalArgumentException e) {
              Util.report("Bad date format in " + CONFIGURATION_FILE + "; reverting to default", e);
              // If the format pattern is invalid - use the default format
              dateTimeFormat = DEFAULT_DATE_TIME_FORMAT;
              dateFormatter = new SimpleDateFormat(dateTimeFormat);
          }
      }
  }

systemPrefix + "showdatetime" is org.slf4j.simplelogger.showdatetime When I try write org.slf4j.simplelogger.showdatetime=true to simplelogger.properties, It works normally. I hope it can help some people.

JackJun
  • 21
  • 1