我发现logback
(由Ceki Gülcü 编写)效果很好,并且还保留了行号!
(它可以替代log4j
:太棒了!)
import ch.qos.logback._
import org.slf4j._
object Main {
def logger = LoggerFactory.getLogger("Main")
var thingy = {
x:Int =>
logger.info("x=" + x)
x + 1
}
def main(args: Array[String]) {
logger.info("Hello.")
logger.info("Hello again!")
val myInts : List[Int] = List(-25,1,5,20)
val myInts2 : List[Int] = myInts.filter { x:Int => x > 0 }
logger.info("my ints2:" + myInts2)
val myInts3 = myInts2.map(p => p * 2 )
logger.info("my ints3:" + myInts3)
logger.info(thingy(1) + "")
}
}
对于任何努力开始使用 Scala 的人来说,这就是我为构建基本框架所做的工作:
1)下载sbt-launcher.jar
并把它放在/opt/
我用“ sbt-launch-0.7.5.RC0.jar
”的地方
2) 创建一个 bash 脚本作为 sbt 启动器的快捷方式nano /opt/bin/sbt
:
#!/bin/bash
java -jar /opt/sbt-launch-0.7.5.RC0.jar "$@"
(使其可执行)
$ sudo chmod ug+x ./sbt
确保它也在你的路径中。
3)创建和配置sbt项目:
$ mkdir ./sc01
$ cd ./sc01
$ sbt
$ mkdir ./project/build</pre>
$ nano ./project/build/Project.scala</pre>
把它放在那里:
import sbt._
class sc01(info: ProjectInfo) extends DefaultProject(info)
{
// dependencies
val logback_core = "ch.qos.logback" % "logback-core" % "0.9.24" % "compile" //LGPL 2.1
val logback_classic = "ch.qos.logback" % "logback-classic" % "0.9.24" % "compile" //LGPL 2.1
val log4j_over_slf4j = "org.slf4j" % "log4j-over-slf4j" % "1.6.1"
// if you are going to have any unmanaged (manually-added) jars
// def baseDirectories = "lib"
// def extraJars = descendents(baseDirectories, "*.jar")
// override def unmanagedClasspath = super.unmanagedClasspath +++ extraJars
// tasks - easy to define
lazy val hi = task { println("Hello World"); None }
// classpath
//override def mainScalaSourcePath = "src"
}
4)将上面的东西粘贴到Main:
$ nano ./src/main/scala/Main.scala
5)我差点忘了!把它放进去/src/main/resources/logback.xml
(它需要得到行号)
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} %line --- %msg%n</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="STDOUT" />
</root>
</configuration>
6)$ sbt
现在您应该在sbt
shell 的控制台中:
> update
> compile
> run
希望这可以帮助。