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 on github. Enjoy!

Unless otherwise specified the contents of this page are copyright © 2015 Sean Hunter. This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.