Created
February 10, 2022 11:58
-
-
Save tototoshi/f83a3585bd93cfeebd1828d422b23797 to your computer and use it in GitHub Desktop.
Loggerの名前の違い
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | |
) | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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