Skip to content

Commit

Permalink
zio2-log draft 3
Browse files Browse the repository at this point in the history
  • Loading branch information
vagroz committed Nov 9, 2022
1 parent fed8444 commit fca7807
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 48 deletions.
2 changes: 1 addition & 1 deletion examples/src/main/resources/logback.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ appender("STRUCTURED", ConsoleAppender) {
}
}

root(DEBUG, ["PLAIN-COLORED", "STRUCTURED"])
root(DEBUG, ["STRUCTURED"])
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package tofu.example.zio2

import derevo.derive
import tofu.logging.TofuAnnotation
import tofu.logging.TofuAnnotation.make
import tofu.logging.LogAnnotation
import tofu.logging.LogAnnotation.make
import tofu.logging.derivation.{hidden, loggable}

import java.util.UUID
Expand All @@ -17,7 +17,7 @@ case class TestUser(

object LogKey {

val user: TofuAnnotation[TestUser] = make[TestUser]("user")
val count: TofuAnnotation[Int] = make[Int]("count")
val requestId: TofuAnnotation[UUID] = make[UUID]("requestId")
val user: LogAnnotation[TestUser] = make[TestUser]("user")
val count: LogAnnotation[Int] = make[Int]("count")
val requestId: LogAnnotation[UUID] = make[UUID]("requestId")
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ object ZioLoggingNativeExample extends ZIOAppDefault {
_ <- ZIO.fail(new IllegalStateException(s"$i divided by 5")).when(i % 5 == 0)
_ <- ZIO.logInfo("Request completed")
} yield ()
} @@ TofuZAspect()(LogKey.count -> Some(i))
}
} yield ()

zio.catchAllCause(c => ZIO.logErrorCause("Request failed", c))
Expand All @@ -28,7 +28,7 @@ object ZioLoggingNativeExample extends ZIOAppDefault {
ZIO.logSpan("full_app") {
for {
_ <- ZIO.logWarning("Hello everybody") @@ ZIOAspect.annotated("detail" -> "Good day!")
_ <- ZIO.collectAllParDiscard(tasks) @@ TofuZAspect(LogKey.user -> TestUser("Vasya", "12345"))
_ <- ZIO.collectAllParDiscard(tasks) @@ ZLogAspect(LogKey.user -> TestUser("Vasya", "12345"))()
_ <- ZIO.log("Application shut down")
} yield ()
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package tofu.logging.zlogs

import tofu.logging.LogAnnotation.AnnotatedValue
import tofu.logging._
import zio.{Trace, ZIO, ZIOAspect}

class ZLogAspect private(
appendedContext: Map[LogAnnotation[_], Any],
) extends ZIOAspect[Nothing, Any, Nothing, Any, Nothing, Any] {

override def apply[R, E, A](zio: ZIO[R, E, A])(implicit trace: Trace): ZIO[R, E, A] = {
if (appendedContext.isEmpty) zio
else {
ZLogContextLive.TofuLogContextRef
.locallyWith(_ ++ appendedContext)(zio)
}
}

}

object ZLogAspect {
def apply(value: AnnotatedValue, values: AnnotatedValue*)(valuesOpt: Option[AnnotatedValue]*): ZLogAspect = {
new ZLogAspect(
(value +: values).toMap ++ valuesOpt.flatten.toMap
)
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
package tofu.logging.zlogs

import ZLogContextLive.TofuLogContextRef
import tofu.logging.{LoggedValue, TofuAnnotation}
import tofu.logging.{LoggedValue, LogAnnotation}
import zio._

trait ZLogContext {

/** Adds the couple of `LogAnnotation` and corresponding value to the context.
*/
def update[A](key: TofuAnnotation[A], value: A): UIO[Unit]
def update[A](key: LogAnnotation[A], value: A): UIO[Unit]

/** Adds a new value and returns it if the annotation is absent in the context, returns an actual value from the
* context otherwise.
*/
def getOrUpdate[A](key: TofuAnnotation[A], value: A): UIO[A]
def getOrUpdate[A](key: LogAnnotation[A], value: A): UIO[A]

/** Returns a value from the context if the annotation exists.
*/
def get[A](key: TofuAnnotation[A]): UIO[Option[A]]
def get[A](key: LogAnnotation[A]): UIO[Option[A]]

/** Scopes the result of calling the specified function only for the given effect.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package tofu.logging.zlogs

import tofu.logging.zlogs.ZLogContextLive.ContextDump
import tofu.logging.{LoggedValue, TofuAnnotation}
import tofu.logging.{LoggedValue, LogAnnotation}
import zio._

class ZLogContextLive(
ref: => FiberRef[ContextDump]
) extends ZLogContext with ZLogContext.LoggedValuesExtractor {

override def update[A](key: TofuAnnotation[A], value: A): UIO[Unit] =
override def update[A](key: LogAnnotation[A], value: A): UIO[Unit] =
ref.update(_ + (key -> value))

override def get[A](key: TofuAnnotation[A]): UIO[Option[A]] =
override def get[A](key: LogAnnotation[A]): UIO[Option[A]] =
ref.get.map(_.get(key).asInstanceOf[Option[A]])

override def getOrUpdate[A](key: TofuAnnotation[A], value: A): UIO[A] = {
override def getOrUpdate[A](key: LogAnnotation[A], value: A): UIO[A] = {

ref.modify[A] { dataMap =>
if (dataMap.contains(key)) {
Expand Down Expand Up @@ -42,7 +42,7 @@ class ZLogContextLive(

object ZLogContextLive {

type ContextDump = Map[TofuAnnotation[_], Any]
type ContextDump = Map[LogAnnotation[_], Any]

private[zlogs] lazy val TofuLogContextRef: FiberRef[ContextDump] =
Unsafe.unsafe(implicit unsafe =>
Expand Down

0 comments on commit fca7807

Please sign in to comment.