Log4j for Scala

Scala is nice. Logging is nice. Scala + logging.....?

I've recently been playing about a bit with Scala. It's a great language in which you can write real programs very easily. As an example, in Java you often want to be able to make use of log4j to log various tracing info. Well, here's a little helper trait you can mix in to scala classes make logging completely trivial:

package com.uncarved.helpers

import org.apache.log4j.Logger;

/**
 * LogHelper is a trait you can mix in to provide easy log4j logging 
 * for your scala classes. 
 **/
trait LogHelper {
    val loggerName = this.getClass.getName
    lazy val logger = Logger.getLogger(loggerName)
}

You use it like this:

class MyClass extends LogHelper {
    logger.debug("We got ourselves a class")
    def someMethod(temp: Int) = {
        logger.debug("entering someMethod")

        if(temp>25) {
           logger.info("It's mighty hot in here")

           //...do something

        }
        //..... etc

        logger.debug("leaving someMethod")
    }
}

Easy peasy logging. This class is one of several that I have released using sbaz. Enjoy!

To download it, follow the tutorial to set up sbaz in a managed directory, then (assuming you have changed to that directory), do:

bin/sbaz install log4j
bin/sbaz install commons-logging
bin/sbaz install httpcore-4
bin/sbaz install httpclient-4
bin/sbaz install uncarved-helpers

Validate

Unless otherwise specified the contents of this page are copyright © 2006 Sean Hunter and are released under a creative commons attribution 2.5 license.
Machine-readable metadata for this article can be found here.
Last modified: Wed Jul 21 09:25:34 2010.