Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is the default logging file in Scala's SBT?

Tags:

scala

sbt

The SBT document says that "When a command is run, more detailed logging output is sent to a file than to the screen (by default). ..."

Where is the logging file?

If I use a logging function in my program, where can I find those logs after the program is finished?

like image 374
Qi Qi Avatar asked Jan 19 '13 21:01

Qi Qi


People also ask

How do you add logs to scala?

To apply logging we can simply use scala logging for this. Scala provides us one logger class which is present inside this com. typesafe. scalalogging package also this class internally wraps the SLF4J logger.

Where are SBT jars?

All new SBT versions (after 0.7. x ) by default put the downloaded JARS into the . ivy2 directory in your home directory. If you are using Linux, this is usually /home/<username>/.

What is SBT file?

sbt is a popular tool for compiling, running, and testing Scala projects of any size. Using a build tool such as sbt (or Maven/Gradle) becomes essential once you create projects with dependencies or more than one code file.


1 Answers

If you are simply trying to log to a file using an SLF4J compatible logger (as suggested by the link in your comment) I'd propose you use Logback for logging, as explained here.

It is fairly simple to configure (for simple use cases), including where log outputs are sent to. The linked to tutorial only configures a console appender, that is, the logged output will be sent to the console. You can configure a FileAppender (i.e. sending log outputs to a file) like this (in your logback.xml):

<appender name="FILE" class="ch.qos.logback.core.FileAppender">
   <file>testFile.log</file>
   <append>true</append>
   <encoder>
     <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
   </encoder>
 </appender>

And then changing <appender-ref ref="STDOUT" /> from the tutorial to <appender-ref ref="FILE" />. You should really take a look at the logback / SLF4J docs in order to figure out how to set up more complex logging configurations if you need it, but this should get you started.

After setting everything up (i.e. adding logback as a dependency in build.sbt and creating your logback.xml configuration in src/main/resources), you can then invoke the logger like so:

import org.slf4j.LoggerFactory
import ch.qos.logback.core.util.StatusPrinter
import ch.qos.logback.classic.LoggerContext

object LogTester extends App{
  def logger = LoggerFactory.getLogger("KDURLFilter")
  StatusPrinter.print((LoggerFactory.getILoggerFactory).asInstanceOf[LoggerContext])
  logger.info("vikas")
}

Example

I've created an example SBT project in which you can see a running logback configuration.

like image 156
fresskoma Avatar answered Sep 18 '22 19:09

fresskoma