diff --git a/core/src/main/scala/com/evolutiongaming/catshelper/Log.scala b/core/src/main/scala/com/evolutiongaming/catshelper/Log.scala index 0a354445..19d4477a 100644 --- a/core/src/main/scala/com/evolutiongaming/catshelper/Log.scala +++ b/core/src/main/scala/com/evolutiongaming/catshelper/Log.scala @@ -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)) diff --git a/core/src/main/scala/com/evolutiongaming/catshelper/LogOf.scala b/core/src/main/scala/com/evolutiongaming/catshelper/LogOf.scala index 43b7141d..2c789bef 100644 --- a/core/src/main/scala/com/evolutiongaming/catshelper/LogOf.scala +++ b/core/src/main/scala/com/evolutiongaming/catshelper/LogOf.scala @@ -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") } -} \ No newline at end of file + + 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)) + } +}