Skip to content

Instantly share code, notes, and snippets.

@tototoshi
Created February 10, 2022 11:58
Show Gist options
  • Save tototoshi/f83a3585bd93cfeebd1828d422b23797 to your computer and use it in GitHub Desktop.
Save tototoshi/f83a3585bd93cfeebd1828d422b23797 to your computer and use it in GitHub Desktop.
Loggerの名前の違い
val scalaVersion_3 = "3.1.1"
lazy val root = project
.in(file("."))
.settings(
name := "logger-name-example",
organization := "com.github.tototoshi",
version := "0.1.0-SNAPSHOT",
scalaVersion := scalaVersion_3,
libraryDependencies ++= Seq(
"org.slf4j" % "slf4j-api" % "1.7.36",
"ch.qos.logback" % "logback-classic" % "1.2.10",
"org.scalatest" %% "scalatest" % "3.2.11" % "test"
)
)
package com.example
import org.slf4j.LoggerFactory
trait BaseLogger {
private val classNameLogger = LoggerFactory.getLogger(classOf[BaseLogger])
private val thisLogger = LoggerFactory.getLogger(this.getClass)
def logByClassName(message: String): Unit = {
classNameLogger.info(message)
}
def logByThis(message: String): Unit = {
thisLogger.info(message)
}
}
class ExampleLogger extends BaseLogger
object Main {
private val logger = LoggerFactory.getLogger(getClass)
private val exampleLogger = new ExampleLogger
def main(args: Array[String]): Unit = {
logger.info("Hello from logger")
exampleLogger.logByClassName("Hello from classNameLogger")
exampleLogger.logByThis("Hello from thisLogger")
}
}
2022/02/10 20:55:50.085 [sbt-bg-threads-7] INFO com.example.Main$ - Hello from logger
2022/02/10 20:55:50.087 [sbt-bg-threads-7] INFO com.example.BaseLogger - Hello from classNameLogger
2022/02/10 20:55:50.087 [sbt-bg-threads-7] INFO com.example.ExampleLogger - Hello from thisLogger
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment