12

My console app takes a log-level=<LEVEL> option. Looking at some examples in Java, it looks like changing SLF4J logger level is generally possible, but with scala-logging library it seems like this is not the case - regardless of how I create the logger it doesn't have setLevel method available. Any suggestions?

Community
  • 1
  • 1
Kombajn zbożowy
  • 8,755
  • 3
  • 28
  • 60

2 Answers2

11

The library needs a logging backend (you can check out the prerequisites). Once you define it, you can set the logging level via a configuration file, for example:

// src/main/resources/logback.xml
<configuration>    
  <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender"/>

  <root level="debug">
    <appender-ref ref="stdout"/>
  </root>
</configuration>

This will result in setting the log level to DEBUG for that particular logger. Anyway, this should work if you're using the slf4j backend. I hope this helps you.

Andrei T.
  • 2,455
  • 1
  • 13
  • 28
  • But this way I can only set logging level at build time, right? What about runtime? – Kombajn zbożowy Mar 12 '17 at 00:15
  • I'm not sure you can do this using logback. Alternatively you can use a log4j backend which provides the method you are looking for. This works fine because scala-logging can work with a log4j backend :) – Andrei T. Mar 12 '17 at 00:22
5

Based on this answer I used this piece of code and it did the work for me:

import ch.qos.logback.classic.{Level,Logger}
import org.slf4j.LoggerFactory

LoggerFactory
  .getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME)
  .asInstanceOf[Logger]
  .setLevel(Level.INFO)
Rodrigue
  • 3,617
  • 2
  • 37
  • 49
Kombajn zbożowy
  • 8,755
  • 3
  • 28
  • 60
  • 1
    Caused by: java.lang.ClassCastException: org.slf4j.impl.Log4jLoggerAdapter cannot be cast to ch.qos.logback.classic.Logger – Havnar Sep 19 '19 at 12:54
  • This answer is deprecated as the `Logger` instance does not have a method `setLevel` anymore. – Sim Feb 16 '21 at 09:20