scalaplayframework-2.6

Can I configure a logger for a trait separately from logger of the class implementing the trait


My play application has a Component defined as as trait as follows: It is the interface to database connection.

trait CassandraRepositoryComponents {
  def environment: Environment
  def configuration: Configuration
  def applicationLifecycle: ApplicationLifecycle
  val utilities:HelperMethods 
  val cassandraConnectionService = CassandraConnectionManagementService()

  val myLogger = LoggerFactory.getLogger(this.getClass.getName)
...
}

The AppComponent classes implements the trait.

class AppComponents (context: Context) extends BuiltInComponentsFromContext(context)
  with CassandraRepositoryComponents /
..
}

Is is possible to control logging for AppComponent and CassandraRepositoryComponents separately. At the moment, the CassandraRepositoryComponents takes logging configuration of AppComponent maybe because the main class is AppComponent. So the code val myLogger = LoggerFactory.getLogger(this.getClass.getName) gets AppComponent class name? How can I do this? May be I need to change CassandraRepositoryComponents from being a trait to something else. What could that be?

UPDATE I tried to provide a harded-coded name to CassandraRepositoryComponents

val cassandraRepositoryComponentsLogger = LoggerFactory.getLogger("CassandraRepositoryComponents")

But I am not seeing the logging even when all levels are enabled in logback.xml.

  <logger name="cassandraRepositoryComponentsLogger" level="ALL" additivity="false"> <appender-ref ref="STDOUT"/> </logger>

In one of the methods of CassandraRepositoryComponents, I am using the logger and println but I can see only println message.

println(s"database will connect with keyspace ${keyspaceName}") //SHOWS
    cassandraRepositoryComponentsLogger.debug(s"debugger - database will connect with keyspace ${keyspaceName}") //DOESN'T SHOW

Solution

  • You should use the same name in LoggerFactory.getLogger(...) and in <logger name="...".