Skip to content

Commit

Permalink
Merge pull request #653 from alexcardell/main
Browse files Browse the repository at this point in the history
Add LoggerFactory.mapK
  • Loading branch information
danicheg authored May 19, 2022
2 parents 909f712 + 1ed694d commit 4c8fc23
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

package org.typelevel.log4cats

import cats.Functor
import cats.syntax.functor._
import cats.~>

import scala.annotation.implicitNotFound

@implicitNotFound("""
Expand All @@ -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)
}
}
}
Original file line number Diff line number Diff line change
@@ -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])
}

0 comments on commit 4c8fc23

Please sign in to comment.