Complete “Scala Logging” Example

For those who’re still struggling for how to make your scala-logging work in your sbt project. They just need to follow these steps:

  1. Add these two dependencies in your build.sbt:

    libraryDependencies += "com.typesafe.scala-logging" %% "scala-logging" % "3.1.0"
    libraryDependencies += "ch.qos.logback" % "logback-classic" % "1.1.2"
    
  2. Create a file logback.xml in your /src/main/resources/ and paste below mentioned content in that file.

    <configuration>
        <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
            <encoder>
                <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
            </encoder>
        </appender>
    
        <appender name="FILE" class="ch.qos.logback.core.FileAppender">
            <!-- path to your log file, where you want to store logs -->
            <file>/Users/yourusername/test.log</file>
            <append>false</append>
            <encoder>
                <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
            </encoder>
        </appender>
    
        <root level="debug">
            <appender-ref ref="STDOUT" />
            <appender-ref ref="FILE" />
        </root>
    </configuration>
    
  3. Extend your Scala class or object with trait LazyLogging:

    import com.typesafe.scalalogging.slf4j.LazyLogging
    class MyClass extends LazyLogging {
      logger.debug("This is very convenient ;-)")
    }
    
  4. It’s done.

P.S: Only a side note that logger is already a member of trait LazyLogging so you don’t need to create it (as shown in above example)

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)