Skip to content

Commit

Permalink
Add prefixed and withField to LogOf
Browse files Browse the repository at this point in the history
  • Loading branch information
nviliunov-evolution-throwaway committed Nov 22, 2024
1 parent 9453017 commit 43e36e5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 2 additions & 0 deletions core/src/main/scala/com/evolutiongaming/catshelper/Log.scala
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,8 @@ object Log {

def prefixed(prefix: String): Log[F] = mapMsg(msg => s"$prefix $msg")

def withField(key: String, value: String): Log[F] = prefixed(s"$key=$value")

def mapMdc(f: Log.Mdc => Log.Mdc): Log[F] = new Log[F] {

def trace(msg: => String, mdc: Mdc): F[Unit] = self.trace(msg, f(mdc))
Expand Down
13 changes: 12 additions & 1 deletion core/src/main/scala/com/evolutiongaming/catshelper/LogOf.scala
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,16 @@ object LogOf {
}

def forClass[C](implicit C: ClassTag[C]): F[Log[F]] = self.apply(C.runtimeClass)

def mapMsg(f: String => String)(implicit F: Monad[F]): LogOf[F] = new MapMsg(self, f)

def prefixed(prefix: String)(implicit F: Monad[F]): LogOf[F] = mapMsg(msg => s"$prefix $msg")

def withField(key: String, value: String)(implicit F: Monad[F]): LogOf[F] = prefixed(s"$key=$value")
}
}

private class MapMsg[F[_]: Monad](logOf: LogOf[F], mapper: String => String) extends LogOf[F] {
override def apply(source: String): F[Log[F]] = logOf.apply(source).map(_.mapMsg(mapper))
override def apply(source: Class[?]): F[Log[F]] = logOf.apply(source).map(_.mapMsg(mapper))
}
}

0 comments on commit 43e36e5

Please sign in to comment.