From d00181c9e350dd538b86676cd3f6f209228abfcd Mon Sep 17 00:00:00 2001 From: Arman Bilge Date: Tue, 23 Aug 2022 18:13:28 +0000 Subject: [PATCH 1/6] Deprecate implicit use of `LoggerFactory` --- .../typelevel/log4cats/LoggerFactoryGen.scala | 6 ++++++ .../typelevel/log4cats/slf4j/Slf4jFactory.scala | 16 ++++++++++++++++ .../org/typelevel/log4cats/slf4j/package.scala | 16 +++------------- .../slf4j/Slf4jLoggerMacroCompilationTests.scala | 2 +- 4 files changed, 26 insertions(+), 14 deletions(-) diff --git a/core/shared/src/main/scala/org/typelevel/log4cats/LoggerFactoryGen.scala b/core/shared/src/main/scala/org/typelevel/log4cats/LoggerFactoryGen.scala index 0c45eb00..8237bbb7 100644 --- a/core/shared/src/main/scala/org/typelevel/log4cats/LoggerFactoryGen.scala +++ b/core/shared/src/main/scala/org/typelevel/log4cats/LoggerFactoryGen.scala @@ -27,16 +27,22 @@ trait LoggerFactoryGen[F[_]] { } private[log4cats] trait LoggerFactoryGenCompanion { + @deprecated("LoggerFactories should be passed explicitly, not implicitly", "2.5.0") def getLogger[F[_]](implicit lf: LoggerFactoryGen[F], name: LoggerName): lf.LoggerType = lf.getLogger + @deprecated("LoggerFactories should be passed explicitly, not implicitly", "2.5.0") def getLoggerFromName[F[_]](name: String)(implicit lf: LoggerFactoryGen[F]): lf.LoggerType = lf.getLoggerFromName(name) + @deprecated("LoggerFactories should be passed explicitly, not implicitly", "2.5.0") def getLoggerFromClass[F[_]](clazz: Class[_])(implicit lf: LoggerFactoryGen[F]): lf.LoggerType = lf.getLoggerFromClass(clazz) + @deprecated("LoggerFactories should be passed explicitly, not implicitly", "2.5.0") def create[F[_]](implicit lf: LoggerFactoryGen[F], name: LoggerName): F[lf.LoggerType] = lf.create + @deprecated("LoggerFactories should be passed explicitly, not implicitly", "2.5.0") def fromName[F[_]](name: String)(implicit lf: LoggerFactoryGen[F]): F[lf.LoggerType] = lf.fromName(name) + @deprecated("LoggerFactories should be passed explicitly, not implicitly", "2.5.0") def fromClass[F[_]](clazz: Class[_])(implicit lf: LoggerFactoryGen[F]): F[lf.LoggerType] = lf.fromClass(clazz) } diff --git a/slf4j/src/main/scala/org/typelevel/log4cats/slf4j/Slf4jFactory.scala b/slf4j/src/main/scala/org/typelevel/log4cats/slf4j/Slf4jFactory.scala index b210e9a3..5eaa8965 100644 --- a/slf4j/src/main/scala/org/typelevel/log4cats/slf4j/Slf4jFactory.scala +++ b/slf4j/src/main/scala/org/typelevel/log4cats/slf4j/Slf4jFactory.scala @@ -17,6 +17,7 @@ package org.typelevel.log4cats package slf4j +import cats.effect.kernel.Sync import org.slf4j.{Logger => JLogger} trait Slf4jFactory[F[_]] extends LoggerFactory[F] { @@ -26,8 +27,23 @@ trait Slf4jFactory[F[_]] extends LoggerFactory[F] { } object Slf4jFactory extends LoggerFactoryGenCompanion { + @deprecated("LoggerFactories should be passed explicitly, not implicitly", "2.5.0") def apply[F[_]: Slf4jFactory]: Slf4jFactory[F] = implicitly + def create[F[_]: Sync]: Slf4jFactory[F] = new Slf4jFactory[F] { + override def getLoggerFromName(name: String): SelfAwareStructuredLogger[F] = + Slf4jLogger.getLoggerFromName(name) + + override def getLoggerFromSlf4j(logger: JLogger): SelfAwareStructuredLogger[F] = + Slf4jLogger.getLoggerFromSlf4j(logger) + + override def fromName(name: String): F[SelfAwareStructuredLogger[F]] = + Slf4jLogger.fromName(name) + + override def fromSlf4j(logger: JLogger): F[SelfAwareStructuredLogger[F]] = + Slf4jLogger.fromSlf4j(logger) + } + def getLoggerFromSlf4j[F[_]](logger: JLogger)(implicit lf: Slf4jFactory[F] ): SelfAwareStructuredLogger[F] = lf.getLoggerFromSlf4j(logger) diff --git a/slf4j/src/main/scala/org/typelevel/log4cats/slf4j/package.scala b/slf4j/src/main/scala/org/typelevel/log4cats/slf4j/package.scala index 95cfd9f1..cd44b882 100644 --- a/slf4j/src/main/scala/org/typelevel/log4cats/slf4j/package.scala +++ b/slf4j/src/main/scala/org/typelevel/log4cats/slf4j/package.scala @@ -17,20 +17,10 @@ package org.typelevel.log4cats import cats.effect.Sync -import org.slf4j.{Logger => JLogger} package object slf4j { - implicit def loggerFactoryforSync[F[_]: Sync]: Slf4jFactory[F] = new Slf4jFactory[F] { - override def getLoggerFromName(name: String): SelfAwareStructuredLogger[F] = - Slf4jLogger.getLoggerFromName(name) - override def getLoggerFromSlf4j(logger: JLogger): SelfAwareStructuredLogger[F] = - Slf4jLogger.getLoggerFromSlf4j(logger) - - override def fromName(name: String): F[SelfAwareStructuredLogger[F]] = - Slf4jLogger.fromName(name) - - override def fromSlf4j(logger: JLogger): F[SelfAwareStructuredLogger[F]] = - Slf4jLogger.fromSlf4j(logger) - } + @deprecated("LoggerFactories should be passed explicitly, not implicitly", "2.5.0") + implicit def loggerFactoryforSync[F[_]](implicit F: Sync[F]): Slf4jFactory[F] = + Slf4jFactory.create(F) } diff --git a/slf4j/src/test/scala/org/typelevel/log4cats/slf4j/Slf4jLoggerMacroCompilationTests.scala b/slf4j/src/test/scala/org/typelevel/log4cats/slf4j/Slf4jLoggerMacroCompilationTests.scala index ce3c1ab7..38226ab1 100644 --- a/slf4j/src/test/scala/org/typelevel/log4cats/slf4j/Slf4jLoggerMacroCompilationTests.scala +++ b/slf4j/src/test/scala/org/typelevel/log4cats/slf4j/Slf4jLoggerMacroCompilationTests.scala @@ -48,7 +48,7 @@ class Slf4jLoggerOuterClassMacroTest { object LoggingBaseline { val t = new Throwable - def logger[F[_]: Sync]: SelfAwareStructuredLogger[F] = Slf4jFactory.getLogger[F] + def logger[F[_]: Sync]: SelfAwareStructuredLogger[F] = Slf4jFactory.create[F].getLogger val traceM = logger[IO].trace("") val traceTM = logger[IO].trace(t)("") From c5150353cec46acb9c7c046ad3d8a660ef8d2fd7 Mon Sep 17 00:00:00 2001 From: Arman Bilge Date: Tue, 23 Aug 2022 19:40:34 +0000 Subject: [PATCH 2/6] Revert "Deprecate implicit use of `LoggerFactory`" This reverts commit d00181c9e350dd538b86676cd3f6f209228abfcd. --- .../typelevel/log4cats/LoggerFactoryGen.scala | 6 ------ .../typelevel/log4cats/slf4j/Slf4jFactory.scala | 16 ---------------- .../org/typelevel/log4cats/slf4j/package.scala | 16 +++++++++++++--- .../slf4j/Slf4jLoggerMacroCompilationTests.scala | 2 +- 4 files changed, 14 insertions(+), 26 deletions(-) diff --git a/core/shared/src/main/scala/org/typelevel/log4cats/LoggerFactoryGen.scala b/core/shared/src/main/scala/org/typelevel/log4cats/LoggerFactoryGen.scala index 8237bbb7..0c45eb00 100644 --- a/core/shared/src/main/scala/org/typelevel/log4cats/LoggerFactoryGen.scala +++ b/core/shared/src/main/scala/org/typelevel/log4cats/LoggerFactoryGen.scala @@ -27,22 +27,16 @@ trait LoggerFactoryGen[F[_]] { } private[log4cats] trait LoggerFactoryGenCompanion { - @deprecated("LoggerFactories should be passed explicitly, not implicitly", "2.5.0") def getLogger[F[_]](implicit lf: LoggerFactoryGen[F], name: LoggerName): lf.LoggerType = lf.getLogger - @deprecated("LoggerFactories should be passed explicitly, not implicitly", "2.5.0") def getLoggerFromName[F[_]](name: String)(implicit lf: LoggerFactoryGen[F]): lf.LoggerType = lf.getLoggerFromName(name) - @deprecated("LoggerFactories should be passed explicitly, not implicitly", "2.5.0") def getLoggerFromClass[F[_]](clazz: Class[_])(implicit lf: LoggerFactoryGen[F]): lf.LoggerType = lf.getLoggerFromClass(clazz) - @deprecated("LoggerFactories should be passed explicitly, not implicitly", "2.5.0") def create[F[_]](implicit lf: LoggerFactoryGen[F], name: LoggerName): F[lf.LoggerType] = lf.create - @deprecated("LoggerFactories should be passed explicitly, not implicitly", "2.5.0") def fromName[F[_]](name: String)(implicit lf: LoggerFactoryGen[F]): F[lf.LoggerType] = lf.fromName(name) - @deprecated("LoggerFactories should be passed explicitly, not implicitly", "2.5.0") def fromClass[F[_]](clazz: Class[_])(implicit lf: LoggerFactoryGen[F]): F[lf.LoggerType] = lf.fromClass(clazz) } diff --git a/slf4j/src/main/scala/org/typelevel/log4cats/slf4j/Slf4jFactory.scala b/slf4j/src/main/scala/org/typelevel/log4cats/slf4j/Slf4jFactory.scala index 5eaa8965..b210e9a3 100644 --- a/slf4j/src/main/scala/org/typelevel/log4cats/slf4j/Slf4jFactory.scala +++ b/slf4j/src/main/scala/org/typelevel/log4cats/slf4j/Slf4jFactory.scala @@ -17,7 +17,6 @@ package org.typelevel.log4cats package slf4j -import cats.effect.kernel.Sync import org.slf4j.{Logger => JLogger} trait Slf4jFactory[F[_]] extends LoggerFactory[F] { @@ -27,23 +26,8 @@ trait Slf4jFactory[F[_]] extends LoggerFactory[F] { } object Slf4jFactory extends LoggerFactoryGenCompanion { - @deprecated("LoggerFactories should be passed explicitly, not implicitly", "2.5.0") def apply[F[_]: Slf4jFactory]: Slf4jFactory[F] = implicitly - def create[F[_]: Sync]: Slf4jFactory[F] = new Slf4jFactory[F] { - override def getLoggerFromName(name: String): SelfAwareStructuredLogger[F] = - Slf4jLogger.getLoggerFromName(name) - - override def getLoggerFromSlf4j(logger: JLogger): SelfAwareStructuredLogger[F] = - Slf4jLogger.getLoggerFromSlf4j(logger) - - override def fromName(name: String): F[SelfAwareStructuredLogger[F]] = - Slf4jLogger.fromName(name) - - override def fromSlf4j(logger: JLogger): F[SelfAwareStructuredLogger[F]] = - Slf4jLogger.fromSlf4j(logger) - } - def getLoggerFromSlf4j[F[_]](logger: JLogger)(implicit lf: Slf4jFactory[F] ): SelfAwareStructuredLogger[F] = lf.getLoggerFromSlf4j(logger) diff --git a/slf4j/src/main/scala/org/typelevel/log4cats/slf4j/package.scala b/slf4j/src/main/scala/org/typelevel/log4cats/slf4j/package.scala index cd44b882..95cfd9f1 100644 --- a/slf4j/src/main/scala/org/typelevel/log4cats/slf4j/package.scala +++ b/slf4j/src/main/scala/org/typelevel/log4cats/slf4j/package.scala @@ -17,10 +17,20 @@ package org.typelevel.log4cats import cats.effect.Sync +import org.slf4j.{Logger => JLogger} package object slf4j { + implicit def loggerFactoryforSync[F[_]: Sync]: Slf4jFactory[F] = new Slf4jFactory[F] { + override def getLoggerFromName(name: String): SelfAwareStructuredLogger[F] = + Slf4jLogger.getLoggerFromName(name) - @deprecated("LoggerFactories should be passed explicitly, not implicitly", "2.5.0") - implicit def loggerFactoryforSync[F[_]](implicit F: Sync[F]): Slf4jFactory[F] = - Slf4jFactory.create(F) + override def getLoggerFromSlf4j(logger: JLogger): SelfAwareStructuredLogger[F] = + Slf4jLogger.getLoggerFromSlf4j(logger) + + override def fromName(name: String): F[SelfAwareStructuredLogger[F]] = + Slf4jLogger.fromName(name) + + override def fromSlf4j(logger: JLogger): F[SelfAwareStructuredLogger[F]] = + Slf4jLogger.fromSlf4j(logger) + } } diff --git a/slf4j/src/test/scala/org/typelevel/log4cats/slf4j/Slf4jLoggerMacroCompilationTests.scala b/slf4j/src/test/scala/org/typelevel/log4cats/slf4j/Slf4jLoggerMacroCompilationTests.scala index 38226ab1..ce3c1ab7 100644 --- a/slf4j/src/test/scala/org/typelevel/log4cats/slf4j/Slf4jLoggerMacroCompilationTests.scala +++ b/slf4j/src/test/scala/org/typelevel/log4cats/slf4j/Slf4jLoggerMacroCompilationTests.scala @@ -48,7 +48,7 @@ class Slf4jLoggerOuterClassMacroTest { object LoggingBaseline { val t = new Throwable - def logger[F[_]: Sync]: SelfAwareStructuredLogger[F] = Slf4jFactory.create[F].getLogger + def logger[F[_]: Sync]: SelfAwareStructuredLogger[F] = Slf4jFactory.getLogger[F] val traceM = logger[IO].trace("") val traceTM = logger[IO].trace(t)("") From 64ce5e591387c95c341438661c0386de92c59d54 Mon Sep 17 00:00:00 2001 From: Arman Bilge Date: Tue, 23 Aug 2022 19:49:06 +0000 Subject: [PATCH 3/6] Only deprecated `loggerFactoryforSync` --- .../typelevel/log4cats/slf4j/Slf4jFactory.scala | 15 +++++++++++++++ .../org/typelevel/log4cats/slf4j/package.scala | 16 +++------------- .../slf4j/Slf4jLoggerMacroCompilationTests.scala | 2 +- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/slf4j/src/main/scala/org/typelevel/log4cats/slf4j/Slf4jFactory.scala b/slf4j/src/main/scala/org/typelevel/log4cats/slf4j/Slf4jFactory.scala index b210e9a3..440fa942 100644 --- a/slf4j/src/main/scala/org/typelevel/log4cats/slf4j/Slf4jFactory.scala +++ b/slf4j/src/main/scala/org/typelevel/log4cats/slf4j/Slf4jFactory.scala @@ -17,6 +17,7 @@ package org.typelevel.log4cats package slf4j +import cats.effect.kernel.Sync import org.slf4j.{Logger => JLogger} trait Slf4jFactory[F[_]] extends LoggerFactory[F] { @@ -28,6 +29,20 @@ trait Slf4jFactory[F[_]] extends LoggerFactory[F] { object Slf4jFactory extends LoggerFactoryGenCompanion { def apply[F[_]: Slf4jFactory]: Slf4jFactory[F] = implicitly + def create[F[_]: Sync]: Slf4jFactory[F] = new Slf4jFactory[F] { + override def getLoggerFromName(name: String): SelfAwareStructuredLogger[F] = + Slf4jLogger.getLoggerFromName(name) + + override def getLoggerFromSlf4j(logger: JLogger): SelfAwareStructuredLogger[F] = + Slf4jLogger.getLoggerFromSlf4j(logger) + + override def fromName(name: String): F[SelfAwareStructuredLogger[F]] = + Slf4jLogger.fromName(name) + + override def fromSlf4j(logger: JLogger): F[SelfAwareStructuredLogger[F]] = + Slf4jLogger.fromSlf4j(logger) + } + def getLoggerFromSlf4j[F[_]](logger: JLogger)(implicit lf: Slf4jFactory[F] ): SelfAwareStructuredLogger[F] = lf.getLoggerFromSlf4j(logger) diff --git a/slf4j/src/main/scala/org/typelevel/log4cats/slf4j/package.scala b/slf4j/src/main/scala/org/typelevel/log4cats/slf4j/package.scala index 95cfd9f1..74c4ec24 100644 --- a/slf4j/src/main/scala/org/typelevel/log4cats/slf4j/package.scala +++ b/slf4j/src/main/scala/org/typelevel/log4cats/slf4j/package.scala @@ -17,20 +17,10 @@ package org.typelevel.log4cats import cats.effect.Sync -import org.slf4j.{Logger => JLogger} package object slf4j { - implicit def loggerFactoryforSync[F[_]: Sync]: Slf4jFactory[F] = new Slf4jFactory[F] { - override def getLoggerFromName(name: String): SelfAwareStructuredLogger[F] = - Slf4jLogger.getLoggerFromName(name) - override def getLoggerFromSlf4j(logger: JLogger): SelfAwareStructuredLogger[F] = - Slf4jLogger.getLoggerFromSlf4j(logger) - - override def fromName(name: String): F[SelfAwareStructuredLogger[F]] = - Slf4jLogger.fromName(name) - - override def fromSlf4j(logger: JLogger): F[SelfAwareStructuredLogger[F]] = - Slf4jLogger.fromSlf4j(logger) - } + @deprecated("Use Slf4jFactory.create[F] explicitly", "2.5.0") + implicit def loggerFactoryforSync[F[_]: Sync]: Slf4jFactory[F] = + Slf4jFactory.create[F] } diff --git a/slf4j/src/test/scala/org/typelevel/log4cats/slf4j/Slf4jLoggerMacroCompilationTests.scala b/slf4j/src/test/scala/org/typelevel/log4cats/slf4j/Slf4jLoggerMacroCompilationTests.scala index ce3c1ab7..38226ab1 100644 --- a/slf4j/src/test/scala/org/typelevel/log4cats/slf4j/Slf4jLoggerMacroCompilationTests.scala +++ b/slf4j/src/test/scala/org/typelevel/log4cats/slf4j/Slf4jLoggerMacroCompilationTests.scala @@ -48,7 +48,7 @@ class Slf4jLoggerOuterClassMacroTest { object LoggingBaseline { val t = new Throwable - def logger[F[_]: Sync]: SelfAwareStructuredLogger[F] = Slf4jFactory.getLogger[F] + def logger[F[_]: Sync]: SelfAwareStructuredLogger[F] = Slf4jFactory.create[F].getLogger val traceM = logger[IO].trace("") val traceTM = logger[IO].trace(t)("") From fe1883a16d34227e90aad2ba177b4be702b11ce3 Mon Sep 17 00:00:00 2001 From: Arman Bilge Date: Tue, 23 Aug 2022 20:53:01 +0000 Subject: [PATCH 4/6] Update docs --- docs/index.md | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/docs/index.md b/docs/index.md index 4a11a92a..2f43cbcb 100644 --- a/docs/index.md +++ b/docs/index.md @@ -100,29 +100,17 @@ This brings several advantages: If you are unsure how to create a new `LoggerFactory[F]` instance, then you can look at the `log4cats-slf4j`, or `log4cats-noop` modules for concrete implementations. -The quickest fix might be to import needed implicits: -```scala mdoc:silent -// assumes dependency on log4cats-slf4j module -import org.typelevel.log4cats._ -import org.typelevel.log4cats.slf4j._ - -val logger: SelfAwareStructuredLogger[IO] = LoggerFactory[IO].getLogger - -// or -def anyFSyncLogger[F[_]: Sync]: SelfAwareStructuredLogger[F] = Slf4jFactory[F].getLogger -``` - -Alternatively, a mutually exclusive solution is to explicitly create your -`LoggerFactory[F]` instance and pass them around implicitly: +The quickest fix might to create an instance of a `Slf4jFactory` and pass it around implicitly: ```scala mdoc:reset:silent import cats.effect.IO import cats.Monad import cats.syntax.all._ import org.typelevel.log4cats._ +// assumes dependency on log4cats-slf4j module import org.typelevel.log4cats.slf4j.Slf4jFactory // create our LoggerFactory -implicit val logging: LoggerFactory[IO] = Slf4jFactory[IO] +implicit val logging: LoggerFactory[IO] = Slf4jFactory.create[IO] // we summon LoggerFactory instance, and create logger val logger: SelfAwareStructuredLogger[IO] = LoggerFactory[IO].getLogger From 6c60b665d37d5fe4c4d9016679b0ca2368ddfe6c Mon Sep 17 00:00:00 2001 From: Arman Bilge Date: Tue, 23 Aug 2022 15:11:44 -0700 Subject: [PATCH 5/6] Fix typo --- docs/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/index.md b/docs/index.md index 2f43cbcb..a401fd87 100644 --- a/docs/index.md +++ b/docs/index.md @@ -100,7 +100,7 @@ This brings several advantages: If you are unsure how to create a new `LoggerFactory[F]` instance, then you can look at the `log4cats-slf4j`, or `log4cats-noop` modules for concrete implementations. -The quickest fix might to create an instance of a `Slf4jFactory` and pass it around implicitly: +The quickest fix might be to create an instance of a `Slf4jFactory` and pass it around implicitly: ```scala mdoc:reset:silent import cats.effect.IO import cats.Monad From 93aa9ba9ba59273f40de4c72ea886754448ca16b Mon Sep 17 00:00:00 2001 From: Arman Bilge Date: Mon, 19 Sep 2022 13:18:32 -0700 Subject: [PATCH 6/6] bump deprecation version --- slf4j/src/main/scala/org/typelevel/log4cats/slf4j/package.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/slf4j/src/main/scala/org/typelevel/log4cats/slf4j/package.scala b/slf4j/src/main/scala/org/typelevel/log4cats/slf4j/package.scala index 74c4ec24..0414d1a3 100644 --- a/slf4j/src/main/scala/org/typelevel/log4cats/slf4j/package.scala +++ b/slf4j/src/main/scala/org/typelevel/log4cats/slf4j/package.scala @@ -20,7 +20,7 @@ import cats.effect.Sync package object slf4j { - @deprecated("Use Slf4jFactory.create[F] explicitly", "2.5.0") + @deprecated("Use Slf4jFactory.create[F] explicitly", "2.6.0") implicit def loggerFactoryforSync[F[_]: Sync]: Slf4jFactory[F] = Slf4jFactory.create[F] }