Skip to content

Commit

Permalink
write some zio2 logging tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Malyshev committed Dec 13, 2022
1 parent b512897 commit 00f0bf6
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package tofu.logging.zlogs

import ch.qos.logback.classic.Logger
import ch.qos.logback.classic.spi.ILoggingEvent
import ch.qos.logback.core.read.ListAppender
import derevo.derive
import org.slf4j.LoggerFactory
import tofu.logging.derivation.loggable
import zio.{Clock, FiberRef, LogSpan, ZIO}
import zio.{Clock, FiberRef, LogSpan, ULayer, ZIO, ZLayer}
import scala.jdk.CollectionConverters._

import zio._

object TestStuff {

Expand All @@ -15,4 +22,24 @@ object TestStuff {

@derive(loggable)
case class User(name: String)

type LogAppender = ListAppender[ILoggingEvent]

object LogAppender {
val events: URIO[LogAppender, List[ILoggingEvent]] =
ZIO.serviceWith[LogAppender](_.list.asScala.toList)

def layer(loggerName: String): ULayer[LogAppender] = ZLayer.scoped {
ZIO.acquireRelease(
ZIO.succeed {
val logger = LoggerFactory.getLogger(loggerName).asInstanceOf[Logger]
val appender = new ListAppender[ILoggingEvent]
appender.start()
logger.addAppender(appender)
appender
}
)(a => ZIO.succeed(a.stop()))
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,14 @@ object TofuDefaultContextSpec extends ZIOSpecDefault {
LogKey.status.name -> testStatus.asJson
).asJson

val expectedJsonWithZIOContext: Json = JsonObject(
val justZIOContextJson: Json = JsonObject(
"zSpans" -> JsonObject(
"testSpan" -> Json.fromLong(adjustDuration.toMillis)
).asJson,
"zAnnotations" -> JsonObject(
"foo" -> "bar".asJson
).asJson
).asJson.deepMerge(expectedJsonOnlyTofuContext)
).asJson

val expectedJsonWithZIOContext: Json = justZIOContextJson.deepMerge(expectedJsonOnlyTofuContext)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package tofu.logging.zlogs

import zio._
import zio.test._
import TestStuff._
import ch.qos.logback.classic.Level
import io.circe.{Json, JsonObject}
import tofu.logging.{LogTree, LoggedValue}
import tofu.logging.impl.ContextMarker
import tofu.logging.zlogs.TofuDefaultContextSpec.{testCount, testStatus, testUser}

object ZLogsSpec extends ZIOSpecDefault {

val loggerName: String = classOf[ZLogsSpec.type].getName.replace("$", "")

val myLoggerName = "myLogger"

val expectedContextValue: LoggedValue = LogKey.user -> testUser

val contextProvider: ULayer[ContextProvider] = ZLayer.succeed(new ContextProvider {
override def getCtx: UIO[LoggedValue] = ZIO.succeed(expectedContextValue)
})

override def spec: Spec[TestEnvironment with Scope, Any] =
suite("Tofu ZIO2 Logging")(
test("TofuZLogger parses default logger name") {
addLogSpan("testSpan")(
for {
_ <- TestClock.adjust(5.seconds)
_ <- ZIO.log("Some message")
events <- LogAppender.events
} yield {
val e = events.head
val ctx = e.getMarker.asInstanceOf[ContextMarker].ctx
assertTrue(LogTree(ctx) == TofuDefaultContextSpec.justZIOContextJson) &&
assertTrue(
e.getArgumentArray.toSet
.asInstanceOf[Set[LoggedValue]]
.map(LogTree(_)) ==
Set(LogKey.status -> testStatus, LogKey.count -> testCount).map(LogTree(_))
) &&
assertTrue(e.getLevel == Level.WARN)
}
) @@ ZIOAspect.annotated("foo", "bar") @@ LogKey.status(testStatus) @@ LogKey.count(
testCount
) @@ LogLevel.Warning
}.provide(
Runtime.removeDefaultLoggers,
TofuZLogger.addToRuntime,
LogAppender.layer(loggerName)
),
test("TofuZLogger uses user-defined logger name") {
(
for {
_ <- ZIO.log("Some message")
events <- LogAppender.events
} yield {
val e = events.head
val ctx = e.getMarker.asInstanceOf[ContextMarker].ctx
val args = e.getArgumentArray.toSet.asInstanceOf[Set[LoggedValue]]
assertTrue(LogTree(ctx).asObject.get == JsonObject.empty) &&
assertTrue(args.size == 1) && // non-printable loggerName
assertTrue(LogTree(args.head).asObject.get == JsonObject.empty) &&
assertTrue(e.getLevel == Level.INFO)
}
) @@ ZLogAnnotation.loggerName(myLoggerName)
}.provide(
Runtime.removeDefaultLoggers,
TofuZLogger.addToRuntime,
LogAppender.layer(myLoggerName)
),
test("ZLogs") {
val logArg: LoggedValue = LogKey.count -> testCount
for {
_ <- ZIO.serviceWithZIO[ULogging](_.info("Some message", logArg))
events <- LogAppender.events
} yield {
val e = events.head
val ctx = e.getMarker.asInstanceOf[ContextMarker].ctx
assertTrue(e.getArgumentArray()(0).asInstanceOf[LoggedValue] == logArg) &&
assertTrue(LogTree(ctx) == LogTree(expectedContextValue)) &&
assertTrue(e.getLevel == Level.INFO)
}
}.provide(
ZLogs.layerPlainWithContext,
contextProvider,
ZLayer(ZIO.serviceWithZIO[ULogs](_.byName(loggerName))),
LogAppender.layer(loggerName)
),
test("ZLogging.Make") {
val logArg1: LoggedValue = LogKey.status -> testStatus
val logArg2: LoggedValue = LogKey.user -> testUser
for {
_ <- ZIO.serviceWithZIO[ULogging](_.error("Some message", logArg1, logArg2))
events <- LogAppender.events
} yield {
val e = events.head
val ctx = e.getMarker.asInstanceOf[ContextMarker].ctx
assertTrue(e.getArgumentArray()(0).asInstanceOf[LoggedValue] == logArg1) &&
assertTrue(e.getArgumentArray()(1).asInstanceOf[LoggedValue] == logArg2) &&
assertTrue(LogTree(ctx).asObject.get == JsonObject.empty) &&
assertTrue(e.getLevel == Level.ERROR)
}
}.provide(
ZLogging.Make.layerPlain,
contextProvider,
ZLayer(ZIO.serviceWith[ZLogging.Make](_.byName(loggerName))),
LogAppender.layer(loggerName)
)
) @@ TestAspect.sequential

}

This file was deleted.

0 comments on commit 00f0bf6

Please sign in to comment.