How do I configure Scala Breeze to use log4j or slf4j

431 Views Asked by At

I am using the breeze.optimize package of Scala Breeze, and it looks like Breeze ahs implemented its own logging library.

Is there a way to configure Breeze to use standard logging like log4j or slf4j so I can configure logging for optimizations the same way I do for everything else in my application?

Alternatively, how do I just turn off the logging messages. They are on by defaults and logging every iteration of a function minimization for me, which is generating a lot of log noise for me.

Wrapper for Apache Commons based ont he answer below:

import breeze.util.logging.Logger
import breeze.util.logging.Logger.Level
import org.apache.commons.logging.LogFactory

class BreezeCommonsLogger[T: ClassManifest] extends Logger {
    private val log = LogFactory.getLog(classManifest[T].erasure)

    def trace(f: => Any) { if (log.isTraceEnabled()) log.trace(f.toString) }
    def debug(f: => Any) { if (log.isDebugEnabled()) log.debug(f.toString) }
    def info(f: => Any) { if (log.isInfoEnabled()) log.info(f.toString) }
    def warn(f: => Any) { if (log.isWarnEnabled()) log.warn(f.toString) }
    def error(f: => Any) { if (log.isErrorEnabled()) log.error(f.toString) }
    def fatal(f: => Any) { if (log.isFatalEnabled()) log.fatal(f.toString) }
    def level_=(level: Level) {}
    def level = Logger.NEVER
}

I was able to use this like:

val lbfgs = new LBFGS[Mat](maxIters, 5, tolerance) {
    override val log = new BreezeCommonsLogger[LBFGS[Mat]]
}
1

There are 1 best solutions below

1
On BEST ANSWER

Notice how the optimization classes have a log member variable that is of type breeze.util.logging.Logger. To turn off logging messages, there is a provided class called breeze.util.logging.NullLogger. To override the default log, you can do something like this:

val opt = new breeze.optimize.OWLQN { 
  override val log = breeze.util.logging.NullLogger
}

This will create an anonymous class that keeps everything the same for the OWLQN optimizer, sans the logging.

In terms of using an alternative logging source like log4j or slf4j, try using implicit conversions to convert from log4j loggers to Breeze loggers, or look into wrapping a logger in a class that inherits from Logger.