Skip to content

Commit

Permalink
The Companion trait did not work as expected
Browse files Browse the repository at this point in the history
  • Loading branch information
hamnis committed Apr 5, 2022
1 parent 9d2421d commit 7167e54
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,7 @@ package org.typelevel.log4cats

import scala.annotation.implicitNotFound

@implicitNotFound("""|
|Implicit not found for LoggerFactory[${F}].
|
|Information can be found here: https://log4cats.github.io/logging-capability.html
|""")
@implicitNotFound("""Implicit not found for LoggerFactory[${F}].
Information can be found here: https://log4cats.github.io/logging-capability.html
""")
trait LoggerFactory[F[_]] extends LoggerFactoryGen[F, SelfAwareStructuredLogger[F]]
object LoggerFactory extends LoggerFactoryCompanion

private[log4cats] trait LoggerFactoryCompanion {
def getLogger[F[_]](implicit
lf: LoggerFactory[F],
name: LoggerName
): SelfAwareStructuredLogger[F] =
lf.getLogger
def getLoggerFromName[F[_]](name: String)(implicit
lf: LoggerFactory[F]
): SelfAwareStructuredLogger[F] =
lf.getLoggerFromName(name)

def getLoggerFromClass[F[_]](clazz: Class[_])(implicit
lf: LoggerFactory[F]
): SelfAwareStructuredLogger[F] =
lf.getLoggerFromClass(clazz)

def create[F[_]](implicit
lf: LoggerFactory[F],
name: LoggerName
): F[SelfAwareStructuredLogger[F]] =
lf.create
def fromName[F[_]](name: String)(implicit lf: LoggerFactory[F]): F[SelfAwareStructuredLogger[F]] =
lf.fromName(name)
def fromClass[F[_]](clazz: Class[_])(implicit
lf: LoggerFactory[F]
): F[SelfAwareStructuredLogger[F]] =
lf.fromClass(clazz)
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
* limitations under the License.
*/

package org.typelevel.log4cats.slf4j
package org.typelevel.log4cats
package slf4j

import cats.effect.Sync
import org.typelevel.log4cats.{LoggerFactory, LoggerFactoryCompanion, SelfAwareStructuredLogger}
import org.slf4j.{Logger => JLogger}

trait Slf4jFactory[F[_]] extends LoggerFactory[F] {
Expand All @@ -26,7 +26,7 @@ trait Slf4jFactory[F[_]] extends LoggerFactory[F] {
def fromSlf4j(logger: JLogger): F[SelfAwareStructuredLogger[F]]
}

object Slf4jFactory extends LoggerFactoryCompanion {
object Slf4jFactory {
def apply[F[_]: Slf4jFactory]: Slf4jFactory[F] = implicitly

implicit def forSync[F[_]: Sync]: Slf4jFactory[F] = new Slf4jFactory[F] {
Expand All @@ -51,4 +51,31 @@ object Slf4jFactory extends LoggerFactoryCompanion {
lf: Slf4jFactory[F]
): F[SelfAwareStructuredLogger[F]] =
lf.fromSlf4j(logger)

def getLogger[F[_]](implicit
lf: Slf4jFactory[F],
name: LoggerName
): SelfAwareStructuredLogger[F] =
lf.getLogger
def getLoggerFromName[F[_]](name: String)(implicit
lf: Slf4jFactory[F]
): SelfAwareStructuredLogger[F] =
lf.getLoggerFromName(name)

def getLoggerFromClass[F[_]](clazz: Class[_])(implicit
lf: Slf4jFactory[F]
): SelfAwareStructuredLogger[F] =
lf.getLoggerFromClass(clazz)

def create[F[_]](implicit
lf: Slf4jFactory[F],
name: LoggerName
): F[SelfAwareStructuredLogger[F]] =
lf.create
def fromName[F[_]](name: String)(implicit lf: Slf4jFactory[F]): F[SelfAwareStructuredLogger[F]] =
lf.fromName(name)
def fromClass[F[_]](clazz: Class[_])(implicit
lf: Slf4jFactory[F]
): F[SelfAwareStructuredLogger[F]] =
lf.fromClass(clazz)
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class Slf4jLoggerOuterClassMacroTest {

object LoggingBaseline {
val t = new Throwable
def logger[F[_]: Sync]: SelfAwareStructuredLogger[F] = Slf4jLogger.getLogger[F]
def logger[F[_]: Sync]: SelfAwareStructuredLogger[F] = Slf4jFactory.getLogger[F]

val traceM = logger[IO].trace("")
val traceTM = logger[IO].trace(t)("")
Expand Down

0 comments on commit 7167e54

Please sign in to comment.