Skip to content

Instantly share code, notes, and snippets.

@xicmiah
Last active February 7, 2018 00:29
Show Gist options
  • Save xicmiah/cc2b51f79a8f57af5166b41f9fee1fd0 to your computer and use it in GitHub Desktop.
Save xicmiah/cc2b51f79a8f57af5166b41f9fee1fd0 to your computer and use it in GitHub Desktop.

Revisions

  1. xicmiah revised this gist Feb 7, 2018. 3 changed files with 6 additions and 4 deletions.
    2 changes: 2 additions & 0 deletions Main.scala
    Original file line number Diff line number Diff line change
    @@ -8,6 +8,8 @@ object Main extends App with PosLogging {

    Logger(getClass).info("Goodbye, MDC")

    logger.debug(s"Messages are still by-name: ${println("this only runs on debug")}")

    logger.info("Hello from main")
    }

    2 changes: 1 addition & 1 deletion logback.xml
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@
    </encoder>
    </appender>

    <root level="debug">
    <root level="info">
    <appender-ref ref="STDOUT" />
    </root>
    </configuration>
    6 changes: 3 additions & 3 deletions stdout.log
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    [info] Loading project definition from /Users/miah/Code/scratchpad/project
    [info] Set current project to Hello (in build file:/Users/miah/Code/scratchpad/)
    [info] Running example.Main 
    [pos=example.Foo#doStuff:16] INFO example.Foo: Hi from Foo
    [pos=example.Foo#doStuff:18] INFO example.Foo: Hi from Foo
    [] INFO example.Main$: Goodbye, MDC
    [pos=example.Main:11] INFO example.Main$: Hello from main
    [success] Total time: 1 s, completed Feb 7, 2018 12:48:23 AM
    [pos=example.Main:13] INFO example.Main$: Hello from main
    [success] Total time: 1 s, completed Feb 7, 2018 1:28:57 AM
  2. xicmiah revised this gist Feb 6, 2018. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions stdout.log
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    [info] Loading project definition from /Users/miah/Code/scratchpad/project
    [info] Set current project to Hello (in build file:/Users/miah/Code/scratchpad/)
    [info] Running example.Main 
    [pos=example.Foo#doStuff:14] INFO example.Foo: Hi from Foo
    [pos=example.Main:9] INFO example.Main$: Hello from main
    [success] Total time: 1 s, completed Feb 6, 2018 11:54:36 PM
    [pos=example.Foo#doStuff:16] INFO example.Foo: Hi from Foo
    [] INFO example.Main$: Goodbye, MDC
    [pos=example.Main:11] INFO example.Main$: Hello from main
    [success] Total time: 1 s, completed Feb 7, 2018 12:48:23 AM
  3. xicmiah revised this gist Feb 6, 2018. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions Main.scala
    Original file line number Diff line number Diff line change
    @@ -6,9 +6,9 @@ import org.slf4j.MDC
    object Main extends App with PosLogging {
    new Foo().doStuff()

    logger.info("Hello from main")

    Logger(getClass).info("Goodbye, MDC")

    logger.info("Hello from main")
    }

    class Foo extends PosLogging {
  4. xicmiah revised this gist Feb 6, 2018. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions Main.scala
    Original file line number Diff line number Diff line change
    @@ -7,6 +7,8 @@ object Main extends App with PosLogging {
    new Foo().doStuff()

    logger.info("Hello from main")

    Logger(getClass).info("Goodbye, MDC")
    }

    class Foo extends PosLogging {
    @@ -35,5 +37,7 @@ object PosLogging {
    MDC.put("pos", pos.value)
    originalMsg
    }

    override def afterLog(a: Pos): Unit = MDC.remove("pos")
    }
    }
  5. xicmiah revised this gist Feb 6, 2018. 2 changed files with 2 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion logback.xml
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    <configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
    <pattern>[%mdc] %level %logger{36}: %msg%n</pattern>
    <pattern>[%mdc] %level %logger{36}: %msg%n</pattern> <!-- For visible MDC -->
    </encoder>
    </appender>

    2 changes: 1 addition & 1 deletion stdout.log
    Original file line number Diff line number Diff line change
    @@ -3,4 +3,4 @@
    [info] Running example.Main 
    [pos=example.Foo#doStuff:14] INFO example.Foo: Hi from Foo
    [pos=example.Main:9] INFO example.Main$: Hello from main
    [success] Total time: 1 s, completed Feb 6, 2018 11:53:05 PM
    [success] Total time: 1 s, completed Feb 6, 2018 11:54:36 PM
  6. xicmiah revised this gist Feb 6, 2018. 2 changed files with 5 additions and 4 deletions.
    3 changes: 2 additions & 1 deletion Main.scala
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,6 @@ package example

    import com.typesafe.scalalogging._
    import org.slf4j.MDC
    import sourcecode._

    object Main extends App with PosLogging {
    new Foo().doStuff()
    @@ -21,6 +20,8 @@ trait PosLogging {
    }

    object PosLogging {
    import sourcecode._

    /** Simple wrapper to get both Enclosing and Line */
    final case class Pos(enclosing: Enclosing, line: Line) {
    def value = s"${enclosing.value}:${line.value}"
    6 changes: 3 additions & 3 deletions stdout.log
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    [info] Loading project definition from /Users/miah/Code/scratchpad/project
    [info] Set current project to Hello (in build file:/Users/miah/Code/scratchpad/)
    [info] Running example.Main 
    [pos=example.Foo#doStuff:15] INFO example.Foo: Hi from Foo
    [pos=example.Main:10] INFO example.Main$: Hello from main
    [success] Total time: 1 s, completed Feb 6, 2018 11:44:09 PM
    [pos=example.Foo#doStuff:14] INFO example.Foo: Hi from Foo
    [pos=example.Main:9] INFO example.Main$: Hello from main
    [success] Total time: 1 s, completed Feb 6, 2018 11:53:05 PM
  7. xicmiah revised this gist Feb 6, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion build.sbt
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ lazy val root = (project in file("."))
    name := "Hello",

    libraryDependencies ++= Seq(
    "com.typesafe.scala-logging" %% "scala-logging" % "3.8.0-SNAPSHOT", // nightly :(, used for Logger$takingImplicit
    "com.typesafe.scala-logging" %% "scala-logging" % "3.8.0-SNAPSHOT", // nightly :(, used for Logger#takingImplicit
    "ch.qos.logback" % "logback-classic" % "1.2.3",
    "com.lihaoyi" %% "sourcecode" % "0.1.4")
    )
  8. xicmiah revised this gist Feb 6, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion build.sbt
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ lazy val root = (project in file("."))
    name := "Hello",

    libraryDependencies ++= Seq(
    "com.typesafe.scala-logging" %% "scala-logging" % "3.8.0-SNAPSHOT", // nightly :(
    "com.typesafe.scala-logging" %% "scala-logging" % "3.8.0-SNAPSHOT", // nightly :(, used for Logger$takingImplicit
    "ch.qos.logback" % "logback-classic" % "1.2.3",
    "com.lihaoyi" %% "sourcecode" % "0.1.4")
    )
  9. xicmiah revised this gist Feb 6, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion build.sbt
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ lazy val root = (project in file("."))
    name := "Hello",

    libraryDependencies ++= Seq(
    "com.typesafe.scala-logging" %% "scala-logging" % "3.8.0-SNAPSHOT",
    "com.typesafe.scala-logging" %% "scala-logging" % "3.8.0-SNAPSHOT", // nightly :(
    "ch.qos.logback" % "logback-classic" % "1.2.3",
    "com.lihaoyi" %% "sourcecode" % "0.1.4")
    )
  10. xicmiah revised this gist Feb 6, 2018. No changes.
  11. xicmiah created this gist Feb 6, 2018.
    38 changes: 38 additions & 0 deletions Main.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    package example

    import com.typesafe.scalalogging._
    import org.slf4j.MDC
    import sourcecode._

    object Main extends App with PosLogging {
    new Foo().doStuff()

    logger.info("Hello from main")
    }

    class Foo extends PosLogging {
    def doStuff(): Unit = {
    logger.info("Hi from Foo")
    }
    }

    trait PosLogging {
    protected val logger: LoggerTakingImplicit[PosLogging.Pos] = Logger.takingImplicit(getClass)(PosLogging.CanLogPos)
    }

    object PosLogging {
    /** Simple wrapper to get both Enclosing and Line */
    final case class Pos(enclosing: Enclosing, line: Line) {
    def value = s"${enclosing.value}:${line.value}"
    }
    object Pos {
    implicit def generate(implicit enclosing: Enclosing, line: Line): Pos = Pos(enclosing, line)
    }

    implicit case object CanLogPos extends CanLog[Pos] {
    override def logMessage(originalMsg: String, pos: Pos): String = {
    MDC.put("pos", pos.value)
    originalMsg
    }
    }
    }
    15 changes: 15 additions & 0 deletions build.sbt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    lazy val root = (project in file("."))
    .settings(
    inThisBuild(List(
    organization := "com.example",
    scalaVersion := "2.12.4",
    version := "0.1.0-SNAPSHOT"
    )),

    name := "Hello",

    libraryDependencies ++= Seq(
    "com.typesafe.scala-logging" %% "scala-logging" % "3.8.0-SNAPSHOT",
    "ch.qos.logback" % "logback-classic" % "1.2.3",
    "com.lihaoyi" %% "sourcecode" % "0.1.4")
    )
    11 changes: 11 additions & 0 deletions logback.xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    <configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
    <pattern>[%mdc] %level %logger{36}: %msg%n</pattern>
    </encoder>
    </appender>

    <root level="debug">
    <appender-ref ref="STDOUT" />
    </root>
    </configuration>
    6 changes: 6 additions & 0 deletions stdout.log
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    [info] Loading project definition from /Users/miah/Code/scratchpad/project
    [info] Set current project to Hello (in build file:/Users/miah/Code/scratchpad/)
    [info] Running example.Main 
    [pos=example.Foo#doStuff:15] INFO example.Foo: Hi from Foo
    [pos=example.Main:10] INFO example.Main$: Hello from main
    [success] Total time: 1 s, completed Feb 6, 2018 11:44:09 PM