pekko/akka-docs/rst/scala/code/docs/stream/cookbook/RecipeLoggingElements.scala

51 lines
1.2 KiB
Scala
Raw Normal View History

2014-12-08 17:29:40 +01:00
package docs.stream.cookbook
import akka.event.Logging
import akka.stream.Attributes
import akka.stream.scaladsl.{ Sink, Source }
2014-12-08 17:29:40 +01:00
import akka.testkit.{ EventFilter, TestProbe }
class RecipeLoggingElements extends RecipeSpec {
"Simple logging recipe" must {
"work with println" in {
val printProbe = TestProbe()
def println(s: String): Unit = printProbe.ref ! s
val mySource = Source(List("1", "2", "3"))
//#println-debug
val loggedSource = mySource.map { elem => println(elem); elem }
//#println-debug
loggedSource.runWith(Sink.ignore)
printProbe.expectMsgAllOf("1", "2", "3")
}
"use log()" in {
2014-12-08 17:29:40 +01:00
val mySource = Source(List("1", "2", "3"))
def analyse(s: String) = s
2014-12-08 17:29:40 +01:00
//#log-custom
// customise log levels
mySource.log("before-map")
.withAttributes(Attributes.logLevels(onElement = Logging.WarningLevel))
.map(analyse)
2014-12-08 17:29:40 +01:00
// or provide custom logging adapter
implicit val adapter = Logging(system, "customLogger")
mySource.log("custom")
//#log-custom
2014-12-08 17:29:40 +01:00
val loggedSource = mySource.log("custom")
EventFilter.debug(start = "[custom] Element: ").intercept {
2014-12-08 17:29:40 +01:00
loggedSource.runWith(Sink.ignore)
}
}
}
}