diff --git a/core/shared/src/main/scala/org/typelevel/log4cats/LoggerFactory.scala b/core/shared/src/main/scala/org/typelevel/log4cats/LoggerFactory.scala index c71d61fb..7f5674e7 100644 --- a/core/shared/src/main/scala/org/typelevel/log4cats/LoggerFactory.scala +++ b/core/shared/src/main/scala/org/typelevel/log4cats/LoggerFactory.scala @@ -16,6 +16,10 @@ package org.typelevel.log4cats +import cats.Functor +import cats.syntax.functor._ +import cats.~> + import scala.annotation.implicitNotFound @implicitNotFound(""" @@ -24,8 +28,24 @@ Learn about LoggerFactory at https://typelevel.org/log4cats/#logging-using-capab """) trait LoggerFactory[F[_]] extends LoggerFactoryGen[F] { type LoggerType = SelfAwareStructuredLogger[F] + + def mapK[G[_]](fk: F ~> G)(implicit F: Functor[F]): LoggerFactory[G] = + LoggerFactory.mapK[F, G](fk)(this) } object LoggerFactory extends LoggerFactoryGenCompanion { def apply[F[_]: LoggerFactory]: LoggerFactory[F] = implicitly + + private def mapK[F[_]: Functor, G[_]](fk: F ~> G)(lf: LoggerFactory[F]): LoggerFactory[G] = + new LoggerFactory[G] { + + def getLoggerFromName(name: String): LoggerType = lf + .getLoggerFromName(name) + .mapK(fk) + + def fromName(name: String): G[LoggerType] = { + val logger = lf.fromName(name).map(_.mapK(fk)) + fk(logger) + } + } } diff --git a/core/shared/src/test/scala/org/typelevel/log4cats/extras/syntax/LoggerFactorySyntaxCompilation.scala b/core/shared/src/test/scala/org/typelevel/log4cats/extras/syntax/LoggerFactorySyntaxCompilation.scala new file mode 100644 index 00000000..5894d8e0 --- /dev/null +++ b/core/shared/src/test/scala/org/typelevel/log4cats/extras/syntax/LoggerFactorySyntaxCompilation.scala @@ -0,0 +1,33 @@ +/* + * Copyright 2018 Typelevel + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.typelevel.log4cats.extras.syntax + +import cats._ +import cats.data.Kleisli +import org.typelevel.log4cats.LoggerFactory + +object LoggerFactorySyntaxCompilation { + def loggerFactorySyntaxMapK[F[_]: Functor, G[_]](lf: LoggerFactory[F])( + fk: F ~> G + ): LoggerFactory[G] = + lf.mapK[G](fk) + + def loggerFactoryKleisliLiftK[F[_]: Functor, A]( + lf: LoggerFactory[F] + ): LoggerFactory[Kleisli[F, A, *]] = + lf.mapK(Kleisli.liftK[F, A]) +}