Introduction to Logging With Finatra

Finatra uses the SLF4J API for framework logging. By coupling the framework to only the SLF4J API, application developers are free to choose their actual logging implementation.

From the SLF4J documentation

“The Simple Logging Facade for Java serves as a simple facade or abstraction for various logging frameworks, such as java.util.logging, Logback and log4j. SLF4J allows the end-user to plug in the desired logging framework at deployment time.”

finatra/inject/inject-app and finatra/inject/inject-server transitively provide SLF4J bridges for the following logging providers:

Tip

Note, if you are not using c.t.inject.server.TwitterServer or a subclass, e.g., you are building a command line application directly with c.t.inject.app.App, you can include the LoggingModule to attempt installation the SLF4JBridgeHandler.

For more information on the SLF4J bridges see the SLF4J Bridging legacy APIs documentation.

Since SLF4J is an interface, it requires an actual logging implementation. However, you should ensure that you do not end-up with multiple logging implementations on your classpath, e.g., you should not have multiple SLF4J bindings (slf4j-nop, slf4j-log4j12, slf4j-jdk14, etc.) nor the java.util.logging implementation, etc. on your classpath as these are all competing implementations and since classpath order is non-deterministic this will lead to unexpected logging behavior.

While there are several scala-wrappers for SLF4J, Finatra uses and exposes some additional features on top of the TwitterUtil util-slf4j-api project.

The main logging utility is the c.t.inject.Logging trait which can be mixed into any object or class:

import com.twitter.inject.Logging

class MyClass extends Logging {
  def foo() = {
    info("Calculating...")
    "bar"
  }
}

This trait is a wrapper with some added utility over the c.t.util.logging.Logging.

Important

Scala users should prefer using the logging methods of the c.t.inject.Logging trait as opposed to directly accessing the underlying Logger instance in the Logging as these methods use “call-by-name” parameters.

For more information see the c.t.inject.Logging scaladocs or the util-slf4j-api README.