scala中等效的log4j语句对于下面的java代码是什么

时间:2017-02-22 11:45:29

标签: scala

我正在尝试在scala中使用log4j。我将log4j文件正确放置在资源目录中。

我遵循java

中遵循的相同log4j方法

我的Java代码:这很好用。完全没问题

package pack1;
import org.apache.log4j.Logger;

public class SampleMain {
final static Logger logger = Logger.getLogger(SampleMain.class);

public static void main(String[] args) {
    SampleMain lld = new SampleMain();
    lld.log();
}


public void log() {
    logger.fatal("This is fatal.");
    logger.error("This is error.");
    logger.warn("This is warn.");
    logger.info("This is info.");
    logger.debug("This is debug.");
    logger.trace("This is trace.");
  }


}

我的Scala代码:

import org.apache.log4j.Logger;


class SampleScala {

   val logger = Logger.getLogger(SampleScala.class)//getting compile time error at this line .  

  def method1():Unit = {

  }

}
object ScalaLogging {

def main(args: Array[String]): Unit = {

val obj = new SampleScala();
obj.method1()

 }
}

编译时上述scala代码中的错误:

 identifier expected but 'class' found.

所以我想知道下面java代码的等效scala语句是什么

  final static Logger logger = Logger.getLogger(SampleMain.class); 

1 个答案:

答案 0 :(得分:0)

val logger = Logger getLogger classOf[SampleMain]