How to set up a logger

For logging in my Kotlin project, I use kotlin-logging , which is really nice to use, but I miss a very central point: How do I set the log level of the log?

By default, it is set to info, and I would like to configure it for debugging. Since there is nothing on the Github page, and there is no way to set the level programmatically, I looked at slf4j since kotlin-logging is a wrapper around this.

Apparently, I need to set the system property as follows:

-Dorg.slf4j.simpleLogger.defaultLogLevel=DEBUG 

However, I do not know how to do this in Kotlin.

Can anybody help me?

+5
source share
2 answers

We have no way to change the log level from slf4j api, and we need to rely on the implementation

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


From your question, it seems that you are using slf4j SimpleLogger as the layer behind slf4j . For SimpleLogger you can only change the log level using properties, as you have already done. See this question for more details.

 System.setProperty(org.slf4j.impl.SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "TRACE"); 

Please note that after creating a logger, the log level cannot be changed. If you need to dynamically change the logging level, you can use log4j with SLF4J.

+4
source

If you use kotlin-logging on Android, the simple answer is: just use slf4j-handroid .

This is a simple (recently deprecated!) slf4j-android , but which does not inherit amazing behavior, for example, requiring that the system properties be set so that you can see your debug logs.

0
source

Source: https://habr.com/ru/post/1266180/


All Articles