pekko/akka-docs/src/test/scala/docs/stream/cookbook/RecipeLoggingElements.scala

68 lines
1.7 KiB
Scala
Raw Normal View History

/*
* Copyright (C) 2018 Lightbend Inc. <https://www.lightbend.com>
*/
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
2017-10-06 10:30:28 +02:00
val loggedSource = mySource.map { elem println(elem); elem }
2014-12-08 17:29:40 +01:00
//#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,
onFinish = Logging.InfoLevel,
onFailure = Logging.DebugLevel
)
)
.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)
}
}
"use log() for error logging" in {
//#log-error
Source(-5 to 5)
.map(1 / _) //throwing ArithmeticException: / by zero
.log("error logging")
.runWith(Sink.ignore)
//#log-error
}
2014-12-08 17:29:40 +01:00
}
}