Search code examples
loggingkotlinkotlin-logging

how to configure kotlin-logging logger


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?


Solution

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