build.sbt中加入
scalaVersion := "2.9.1"
libraryDependencies += "log4j" % "log4j" % "1.2.14"
src/main/scala/MyApp.scala中写入:
import org.apache.log4j.Logger
object MyApp {
val logger = Logger.getLogger(MyApp.getClass.getName)
def main(args: Array[String]) = {
logger.debug("Hello, this is an debug message")
logger.info("Hello, this is an info message")
}
}
src/main/resources/log4j.properties中写入:
# Define the root logger with appender file
log = /tmp/log4j
log4j.rootLogger = DEBUG, FILE
# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=${log}/log.out
# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n
sbt run即可,在/tmp/log4j/log.out中可以看到log,但是比较粗陋,需要进一步配置.
参考:http://www.tutorialspoint.com/log4j/log4j_sample_program.htm