lundi 9 janvier 2017

Declaring an SLF4J logger from inside a Scala driver

I have the following Scala class (MyApp.scala):

package me.myapp

import org.slf4j.Logger
import org.slf4j.LoggerFactory

object MyApp {
  val logger = LoggerFactory.getLogger(classOf[MyApp])

  def main(args : Array[String]) : Unit = {
    logger.info("Well hello SLF4J!")
  }
}

When I run this I get a compiler error:

/Users/myuser/workspace/myapp/src/main/scala/me/myapp/MyApp.scala:7: not found: type MyApp
  val logger = LoggerFactory.getLogger(classOf[MyApp])
                                               ^
one error found

So I need MyApp to be an object (not a class) so that I can have a static main method (for my executable JAR), but it seems that, because MyApp is an object, that classOf[MyApp] doesn't actually resolve to anything.

So a few related concerns:

  1. Why are objects not classes? And if they're not classes then how the heck am I able to get away with putting a main method inside of it?!?; and
  2. How can I get an SLF4J logger for MyApp?




Aucun commentaire:

Enregistrer un commentaire