Daniel Cukier
Daniel Cukier

Reputation: 11952

Logging for classes outside play framework

I have an application that uses Scala Play Framework (2.3.X). One of my classes needs to write things in the log. For that I use

play.Logger.info("message")

This works fine when running under Play Application.

But when I run this class using an external standalone application (for example a static main method), the logs don't come out to application.log

How to setup a logger that works both when Play is running and also when I run the class outside Play?

Upvotes: 0

Views: 127

Answers (1)

Liza Shakury
Liza Shakury

Reputation: 738

You can try SLF4J LoggerFactory. If you use sbt, then you can add it in build.sbt like this:

libraryDependencies ++= Seq("org.slf4j" % "slf4j-api" % "1.7.5",
                            "org.slf4j" % "slf4j-simple" % "1.7.5")

Then:

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

class SayHi {
  val logger = LoggerFactory.getLogger(classOf[SayHi])
  logger.info("Hi!")
}

object Main extends App {
  val p = new SayHi
}

Upvotes: 1

Related Questions