zio-logging icon indicating copy to clipboard operation
zio-logging copied to clipboard

LogAnnotation does not work with newtypes

Open Ellzord opened this issue 2 years ago • 1 comments

Trying to use newtypes with LogAnnotation but you get compile errors.

Using zio 2.0.0-RC5, zio-prelude 1.0.0-RC13, zio-logging-slf4j 2.0.0-RC8.

example

  object SomeNewType extends Newtype[String]
  type SomeNewType = SomeNewType.Type

  val SomeAnnotation: LogAnnotation[SomeNewType] =
    LogAnnotation[SomeNewType](name = "client_id", combine = (_: SomeNewType, r: SomeNewType) => r, render = SomeNewType.unwrap)

error

A Tag may not contain an intersection type, yet have provided: com.pirum.coacs.logging.SomeNewType.Base with com.pirum.coacs.logging.SomeNewType.Tag
    LogAnnotation[SomeNewType](name = "client_id", combine = (_: SomeNewType, r: SomeNewType) => r, render = SomeNewType.unwrap)

Ellzord avatar May 10 '22 15:05 Ellzord

it is look like, it is also related to https://github.com/zio/zio-prelude/issues/821 and https://github.com/zio/izumi-reflect/issues/309

justcoon avatar Jul 10 '22 16:07 justcoon

may be related to https://github.com/zio/zio-prelude/pull/949

justcoon avatar Aug 18 '22 08:08 justcoon

@Ellzord

new zio prelude 1.0.0-RC16 was released, and it fixed problem

package zio.logging.example

import zio.logging.{LogAnnotation, LogFormat, consoleJson}
import zio.prelude.Newtype
import zio.{ExitCode, Runtime, Scope, ZIO, ZIOAppDefault, _}

import java.util.UUID

object ConsoleJsonApp extends ZIOAppDefault {

  object UserType extends Newtype[UUID]

  type UserType = UserType.Type

  private val userLogAnnotation = LogAnnotation[UserType]("user", (_, i) => i, _.toString)

  private val logger =
    Runtime.removeDefaultLoggers >>> consoleJson(
      LogFormat.default + LogFormat.annotation(LogAnnotation.TraceId) + LogFormat.annotation(
        userLogAnnotation
      )
    )

  private val users = List.fill(2)(UUID.randomUUID())

  override def run: ZIO[Scope, Any, ExitCode] =
    (for {
      traceId <- ZIO.succeed(UUID.randomUUID())
      _       <- ZIO.foreachPar(users) { uId =>
                   {
                     ZIO.logInfo("Starting operation") *>
                       ZIO.sleep(500.millis) *>
                       ZIO.logInfo("Stopping operation")
                   } @@ userLogAnnotation(UserType(uId))
                 } @@ LogAnnotation.TraceId(traceId)
      _       <- ZIO.logInfo("Done")
    } yield ExitCode.success).provide(logger)

}
{"timestamp":"2022-10-03T20:08:07.715714+02:00","level":"INFO","thread":"zio-fiber-8","message":"Starting operation","trace_id":"f4be2469-85ee-405f-9262-ce895f9f55e2","user":"a61f078c-d969-4bf3-b82a-bfee3ef6464f"}
{"timestamp":"2022-10-03T20:08:07.715763+02:00","level":"INFO","thread":"zio-fiber-7","message":"Starting operation","trace_id":"f4be2469-85ee-405f-9262-ce895f9f55e2","user":"af66d14e-5840-4c78-8b8e-ef194f0c3da6"}
{"timestamp":"2022-10-03T20:08:08.255788+02:00","level":"INFO","thread":"zio-fiber-7","message":"Stopping operation","trace_id":"f4be2469-85ee-405f-9262-ce895f9f55e2","user":"af66d14e-5840-4c78-8b8e-ef194f0c3da6"}
{"timestamp":"2022-10-03T20:08:08.255788+02:00","level":"INFO","thread":"zio-fiber-8","message":"Stopping operation","trace_id":"f4be2469-85ee-405f-9262-ce895f9f55e2","user":"a61f078c-d969-4bf3-b82a-bfee3ef6464f"}
{"timestamp":"2022-10-03T20:08:08.26167+02:00 ","level":"INFO","thread":"zio-fiber-6","message":"Done"}

justcoon avatar Oct 03 '22 18:10 justcoon

LGTM

Ellzord avatar Oct 06 '22 07:10 Ellzord