Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LogAnnotation does not work with newtypes #451

Closed
Ellzord opened this issue May 10, 2022 · 4 comments
Closed

LogAnnotation does not work with newtypes #451

Ellzord opened this issue May 10, 2022 · 4 comments

Comments

@Ellzord
Copy link

Ellzord commented May 10, 2022

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)
@justcoon
Copy link
Contributor

justcoon commented Jul 10, 2022

it is look like, it is also related to zio/zio-prelude#821 and zio/izumi-reflect#309

@justcoon
Copy link
Contributor

may be related to zio/zio-prelude#949

@justcoon
Copy link
Contributor

justcoon commented Oct 3, 2022

@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"}

@Ellzord
Copy link
Author

Ellzord commented Oct 6, 2022

LGTM

@Ellzord Ellzord closed this as completed Oct 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants