19

For logging purposes in my Kotlin project I am using kotlin-logging which is really nice to use however I am missing a very central point: How can I configure the log level of the logger?

Per default it is set to info and I would like to set it to debug. As there is nothing about that on the Github page nor is there any method to set the level programmatically I had a look into slf4j as kotlin-logging is a wrapper around that.

Apparently I have to set a System Property like this:

-Dorg.slf4j.simpleLogger.defaultLogLevel=DEBUG

However I have no idea how to do this in Kotlin.

Anyone can help me out?

Jakob Abfalter
  • 4,980
  • 17
  • 54
  • 94
  • Setting log level is done per implementation. Kotlin-logging and slf4j are just facades for the underlying logging lib (log4j etc'). Which one do you use? – oshai Apr 01 '17 at 06:57

4 Answers4

14

We do not have a way to change log level from slf4j api and we need to rely on implementation

By looking at the Logger interface of slf4j, you can see that it has isLevelEnabled() for all the levels, but not a setter. Therefore, setting the level is implementation specific and it is based on the underlying logging platform you use.


From your question it looks like you use slf4j SimpleLogger as the layer behind slf4j. For SimpleLogger, you can only change the log level through properties like you've already done. See this question for more information.

System.setProperty(org.slf4j.impl.SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "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.

Community
  • 1
  • 1
Yoav Sternberg
  • 6,421
  • 3
  • 28
  • 30
5

I'm using kotlin-logging in a Spring Boot application.

What worked

Run application with

-Dlogging.level.com.mydomain.myapp=DEBUG

(Replace com.mydomain.myapp with the desired package namespace or root for all.)

Alternatively you can add the property to an application properties file

logging.level.com.mydomain.myapp = DEBUG

What didn't work

  • -Dorg.slf4j.simpleLogger.defaultLogLevel=DEBUG
  • -Dorg.slf4j.simplelogger.defaultlog=DEBUG
  • --debug (as one of the program options)
Paulo Merson
  • 13,270
  • 8
  • 79
  • 72
1

If you're using using kotlin-logging on Android, the easy answer is: just use slf4j-handroid.

It's a simple fork of the (recently deprecated!) slf4j-android, but which doesn't inherit delightful behaviors like requiring system properties to be set just so you can see your debug logs.

Michael Piefel
  • 18,660
  • 9
  • 81
  • 112
jensck
  • 154
  • 1
  • 11
0

If you can't access org.slf4j.impl.SimpleLogger.DEFAULT_LOG_LEVEL_KEY, this seems to be working with org.slf4j:slf4j-simple:2.0.6

    System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "TRACE")
Roberto Leinardi
  • 10,641
  • 6
  • 65
  • 69