From ce082aa7ee0e211d7ef9eb6eb31ca338d3336823 Mon Sep 17 00:00:00 2001 From: Henry Date: Fri, 26 Jan 2024 11:35:28 +0100 Subject: [PATCH] Remove dependency on logback-classic and scala-logging where unnecessary (#3471) Co-authored-by: Henry Genet --- build.sbt | 68 ++++++++++--------- .../sttp/tapir/examples/BooksExample.scala | 4 +- .../tapir/examples/BooksPicklerExample.scala | 4 +- .../examples/client/Http4sClientExample.scala | 4 +- .../custom_types/BooksExampleSemiauto.scala | 4 +- .../sttp/tapir/examples/logging/Logging.scala | 15 ++++ .../observability/DatadogMetricsExample.scala | 4 +- .../OpenTelemetryMetricsExample.scala | 4 +- .../PrometheusMetricsExample.scala | 4 +- .../streaming/ProxyHttp4sFs2Server.scala | 4 +- .../examples/GrpcSimpleBooksExample.scala | 11 +-- .../PekkoGrpcSimpleBooksExample.scala | 12 ++-- .../grpc/protobuf/ProtoSchemaGenerator.scala | 8 ++- project/Versions.scala | 2 + .../netty/cats/NettyCatsServerOptions.scala | 5 +- .../netty/loom/NettyIdServerOptions.scala | 4 +- .../netty/NettyFutureServerOptions.scala | 4 +- .../server/netty/internal/NettyDefaults.scala | 10 +-- .../netty/internal/NettyServerHandler.scala | 11 ++- .../netty/zio/NettyZioServerOptions.scala | 12 ++-- .../tapir/server/nima/NimaServerOptions.scala | 18 ++--- .../tapir/server/tests/CreateServerTest.scala | 8 ++- .../runtime/AwsLambdaRuntimeInvocation.scala | 8 ++- 23 files changed, 137 insertions(+), 91 deletions(-) create mode 100644 examples/src/main/scala/sttp/tapir/examples/logging/Logging.scala diff --git a/build.sbt b/build.sbt index 37ecb6c2b1..2ead560770 100644 --- a/build.sbt +++ b/build.sbt @@ -154,10 +154,8 @@ val scalaTestPlusScalaCheck = { Def.setting("org.scalatestplus" %%% s"scalacheck-$scalaCheckSuffix" % Versions.scalaTestPlusScalaCheck) } -lazy val loggerDependencies = Seq( - "ch.qos.logback" % "logback-classic" % "1.4.14", - "com.typesafe.scala-logging" %% "scala-logging" % "3.9.5" -) +lazy val logback = "ch.qos.logback" % "logback-classic" % Versions.logback +lazy val slf4j = "org.slf4j" % "slf4j-api" % Versions.slf4j lazy val rawAllAggregates = core.projectRefs ++ testing.projectRefs ++ @@ -372,10 +370,11 @@ lazy val clientTestServer = (projectMatrix in file("client/testserver")) .settings( name := "testing-server", publish / skip := true, - libraryDependencies ++= loggerDependencies ++ Seq( + libraryDependencies ++= Seq( "org.http4s" %% "http4s-dsl" % Versions.http4s, "org.http4s" %% "http4s-blaze-server" % Versions.http4sBlazeServer, - "org.http4s" %% "http4s-circe" % Versions.http4s + "org.http4s" %% "http4s-circe" % Versions.http4s, + logback ), // the test server needs to be started before running any client tests reStart / mainClass := Some("sttp.tapir.client.tests.HttpServer"), @@ -467,7 +466,10 @@ lazy val testing: ProjectMatrix = (projectMatrix in file("testing")) .settings(commonSettings) .settings( name := "tapir-testing", - libraryDependencies ++= Seq(scalaTest.value % Test) ++ loggerDependencies + libraryDependencies ++= Seq( + scalaTest.value % Test, + logback % Test + ) ) .jvmPlatform(scalaVersions = scala2And3Versions) .jsPlatform(scalaVersions = scala2And3Versions, settings = commonJsSettings) @@ -482,8 +484,9 @@ lazy val tests: ProjectMatrix = (projectMatrix in file("tests")) "io.circe" %%% "circe-generic" % Versions.circe, "com.softwaremill.common" %%% "tagging" % "2.3.4", scalaTest.value, - "org.typelevel" %%% "cats-effect" % Versions.catsEffect - ) ++ loggerDependencies + "org.typelevel" %%% "cats-effect" % Versions.catsEffect, + logback + ) ) .jvmPlatform(scalaVersions = scala2And3Versions) .jsPlatform( @@ -515,8 +518,9 @@ lazy val perfTests: ProjectMatrix = (projectMatrix in file("perf-tests")) "org.http4s" %% "http4s-core" % Versions.http4s, "org.http4s" %% "http4s-dsl" % Versions.http4s, "org.http4s" %% "http4s-blaze-server" % Versions.http4sBlazeServer, - "org.typelevel" %%% "cats-effect" % Versions.catsEffect - ) ++ loggerDependencies, + "org.typelevel" %%% "cats-effect" % Versions.catsEffect, + logback + ), publishArtifact := false ) .settings(Gatling / scalaSource := sourceDirectory.value / "test" / "scala") @@ -933,10 +937,12 @@ lazy val protobuf: ProjectMatrix = (projectMatrix in file("grpc/protobuf")) .settings(commonSettings) .settings( name := "tapir-grpc-protobuf", - libraryDependencies ++= loggerDependencies ++ Seq( + libraryDependencies ++= Seq( + slf4j, scalaTest.value % Test, scalaCheck.value % Test, - scalaTestPlusScalaCheck.value % Test + scalaTestPlusScalaCheck.value % Test, + logback % Test ) ) .jvmPlatform(scalaVersions = scala2Versions) @@ -961,7 +967,8 @@ lazy val grpcExamples: ProjectMatrix = (projectMatrix in file("grpc/examples")) .settings( name := "tapir-grpc-examples", libraryDependencies ++= Seq( - "com.typesafe.akka" %% "akka-discovery" % "2.6.20" + "com.typesafe.akka" %% "akka-discovery" % "2.6.20", + slf4j ), fork := true ) @@ -978,7 +985,8 @@ lazy val pekkoGrpcExamples: ProjectMatrix = (projectMatrix in file("grpc/pekko-e .settings( name := "tapir-pekko-grpc-examples", libraryDependencies ++= Seq( - "org.apache.pekko" %% "pekko-discovery" % "1.0.2" + "org.apache.pekko" %% "pekko-discovery" % "1.0.2", + slf4j ), fork := true ) @@ -1398,7 +1406,7 @@ lazy val jdkhttpServer: ProjectMatrix = (projectMatrix in file("server/jdkhttp-s name := "tapir-jdkhttp-server", libraryDependencies ++= Seq( "org.apache.httpcomponents" % "httpmime" % "4.5.14" - ) ++ loggerDependencies + ) ) .jvmPlatform(scalaVersions = List(scala2_13, scala3)) .dependsOn(serverCore, serverTests % Test) @@ -1409,9 +1417,9 @@ lazy val nettyServer: ProjectMatrix = (projectMatrix in file("server/netty-serve name := "tapir-netty-server", libraryDependencies ++= Seq( "io.netty" % "netty-all" % Versions.nettyAll, - "org.playframework.netty" % "netty-reactive-streams-http" % Versions.nettyReactiveStreams - ) - ++ loggerDependencies, + "org.playframework.netty" % "netty-reactive-streams-http" % Versions.nettyReactiveStreams, + slf4j + ), // needed because of https://github.com/coursier/coursier/issues/2016 useCoursier := false ) @@ -1419,7 +1427,7 @@ lazy val nettyServer: ProjectMatrix = (projectMatrix in file("server/netty-serve .dependsOn(serverCore, serverTests % Test) lazy val nettyServerLoom: ProjectMatrix = - ProjectMatrix("nettyServerLoom", file(s"server/netty-server/loom")) + ProjectMatrix("nettyServerLoom", file("server/netty-server/loom")) .settings(commonJvmSettings) .settings( name := "tapir-netty-server-loom", @@ -1433,7 +1441,8 @@ lazy val nettyServerCats: ProjectMatrix = nettyServerProject("cats", catsEffect) .settings( libraryDependencies ++= Seq( "com.softwaremill.sttp.shared" %% "fs2" % Versions.sttpShared, - "co.fs2" %% "fs2-reactive-streams" % Versions.fs2 + "co.fs2" %% "fs2-reactive-streams" % Versions.fs2, + slf4j ) ) @@ -1450,7 +1459,6 @@ def nettyServerProject(proj: String, dependency: ProjectMatrix): ProjectMatrix = .settings(commonJvmSettings) .settings( name := s"tapir-netty-server-$proj", - libraryDependencies ++= loggerDependencies, // needed because of https://github.com/coursier/coursier/issues/2016 useCoursier := false ) @@ -1464,7 +1472,7 @@ lazy val nimaServer: ProjectMatrix = (projectMatrix in file("server/nima-server" libraryDependencies ++= Seq( "io.helidon.webserver" % "helidon-webserver" % Versions.helidon, "io.helidon.logging" % "helidon-logging-slf4j" % Versions.helidon - ) ++ loggerDependencies + ) ) .jvmPlatform(scalaVersions = scala2_13And3Versions) .dependsOn(serverCore, serverTests % Test) @@ -1518,7 +1526,6 @@ lazy val awsLambdaCore: ProjectMatrix = (projectMatrix in file("serverless/aws/l .settings(commonJvmSettings) .settings( name := "tapir-aws-lambda-core", - libraryDependencies ++= loggerDependencies ) .jvmPlatform(scalaVersions = scala2And3Versions) .jsPlatform(scalaVersions = scala2Versions) @@ -1528,7 +1535,6 @@ lazy val awsLambdaZio: ProjectMatrix = (projectMatrix in file("serverless/aws/la .settings(commonJvmSettings) .settings( name := "tapir-aws-lambda-zio", - libraryDependencies ++= loggerDependencies, libraryDependencies ++= Seq( "com.amazonaws" % "aws-lambda-java-runtime-interface-client" % Versions.awsLambdaInterface ) @@ -1599,10 +1605,10 @@ lazy val awsLambdaCatsEffect: ProjectMatrix = (projectMatrix in file("serverless .settings(commonJvmSettings) .settings( name := "tapir-aws-lambda", - libraryDependencies ++= loggerDependencies, libraryDependencies ++= Seq( "com.softwaremill.sttp.client3" %% "fs2" % Versions.sttp, - "com.amazonaws" % "aws-lambda-java-runtime-interface-client" % Versions.awsLambdaInterface + "com.amazonaws" % "aws-lambda-java-runtime-interface-client" % Versions.awsLambdaInterface, + slf4j ) ) .jvmPlatform(scalaVersions = scala2And3Versions) @@ -2019,9 +2025,9 @@ lazy val examples2: ProjectMatrix = (projectMatrix in file("examples2")) "io.opentelemetry" % "opentelemetry-sdk" % Versions.openTelemetry, "io.opentelemetry" % "opentelemetry-sdk-metrics" % Versions.openTelemetry, "io.opentelemetry" % "opentelemetry-exporter-otlp" % Versions.openTelemetry, - scalaTest.value + scalaTest.value, + logback ), - libraryDependencies ++= loggerDependencies, publishArtifact := false, Compile / run / fork := true ) @@ -2077,9 +2083,9 @@ lazy val examples: ProjectMatrix = (projectMatrix in file("examples")) "io.opentelemetry" % "opentelemetry-sdk" % Versions.openTelemetry, "io.opentelemetry" % "opentelemetry-sdk-metrics" % Versions.openTelemetry, "io.opentelemetry" % "opentelemetry-exporter-otlp" % Versions.openTelemetry, - scalaTest.value + scalaTest.value, + logback ), - libraryDependencies ++= loggerDependencies, publishArtifact := false ) .jvmPlatform(scalaVersions = examplesScalaVersions) diff --git a/examples/src/main/scala/sttp/tapir/examples/BooksExample.scala b/examples/src/main/scala/sttp/tapir/examples/BooksExample.scala index f1b1bd2fc2..9a588d0683 100644 --- a/examples/src/main/scala/sttp/tapir/examples/BooksExample.scala +++ b/examples/src/main/scala/sttp/tapir/examples/BooksExample.scala @@ -1,9 +1,9 @@ package sttp.tapir.examples -import com.typesafe.scalalogging.StrictLogging +import sttp.tapir.examples.logging.Logging import sttp.tapir.generic.auto.* -object BooksExample extends App with StrictLogging { +object BooksExample extends App with Logging { type Limit = Option[Int] type AuthToken = String diff --git a/examples/src/main/scala/sttp/tapir/examples/BooksPicklerExample.scala b/examples/src/main/scala/sttp/tapir/examples/BooksPicklerExample.scala index 3e83d71cf8..b2cdc29ed9 100644 --- a/examples/src/main/scala/sttp/tapir/examples/BooksPicklerExample.scala +++ b/examples/src/main/scala/sttp/tapir/examples/BooksPicklerExample.scala @@ -1,12 +1,12 @@ package sttp.tapir.examples -import com.typesafe.scalalogging.StrictLogging +import sttp.tapir.examples.logging.Logging import sttp.tapir.server.netty.{NettyFutureServer, NettyFutureServerBinding} import scala.concurrent.Await import scala.concurrent.duration.Duration -object BooksPicklerExample extends App with StrictLogging { +object BooksPicklerExample extends App with Logging { type Limit = Option[Int] type AuthToken = String diff --git a/examples/src/main/scala/sttp/tapir/examples/client/Http4sClientExample.scala b/examples/src/main/scala/sttp/tapir/examples/client/Http4sClientExample.scala index 2f4393b712..096886adc5 100644 --- a/examples/src/main/scala/sttp/tapir/examples/client/Http4sClientExample.scala +++ b/examples/src/main/scala/sttp/tapir/examples/client/Http4sClientExample.scala @@ -1,14 +1,14 @@ package sttp.tapir.examples.client import cats.effect.{ExitCode, IO, IOApp} -import com.typesafe.scalalogging.StrictLogging +import sttp.tapir.examples.logging.Logging import io.circe.generic.auto.* import sttp.tapir.* import sttp.tapir.client.http4s.Http4sClientInterpreter import sttp.tapir.generic.auto.* import sttp.tapir.json.circe.* -object Http4sClientExample extends IOApp with StrictLogging { +object Http4sClientExample extends IOApp with Logging { case class User(id: Int, name: String) diff --git a/examples/src/main/scala/sttp/tapir/examples/custom_types/BooksExampleSemiauto.scala b/examples/src/main/scala/sttp/tapir/examples/custom_types/BooksExampleSemiauto.scala index baed59ee22..909a3c6de9 100644 --- a/examples/src/main/scala/sttp/tapir/examples/custom_types/BooksExampleSemiauto.scala +++ b/examples/src/main/scala/sttp/tapir/examples/custom_types/BooksExampleSemiauto.scala @@ -1,9 +1,9 @@ package sttp.tapir.examples.custom_types -import com.typesafe.scalalogging.StrictLogging +import sttp.tapir.examples.logging.Logging import sttp.tapir.Schema -object BooksExampleSemiauto extends App with StrictLogging { +object BooksExampleSemiauto extends App with Logging { type Limit = Option[Int] type AuthToken = String diff --git a/examples/src/main/scala/sttp/tapir/examples/logging/Logging.scala b/examples/src/main/scala/sttp/tapir/examples/logging/Logging.scala new file mode 100644 index 0000000000..af18df84a6 --- /dev/null +++ b/examples/src/main/scala/sttp/tapir/examples/logging/Logging.scala @@ -0,0 +1,15 @@ +package sttp.tapir.examples.logging + +import org.slf4j.{Logger, LoggerFactory} + +/** + * Defines a [[org.slf4j.Logger]] instance `logger` named according to the class + * into which this trait is mixed. + * + * In a real-life project, you might rather want to use a macros-based SLF4J wrapper + * or logging backend. + */ +trait Logging { + + protected val logger: Logger = LoggerFactory.getLogger(getClass.getName) +} diff --git a/examples/src/main/scala/sttp/tapir/examples/observability/DatadogMetricsExample.scala b/examples/src/main/scala/sttp/tapir/examples/observability/DatadogMetricsExample.scala index 7a28791c67..89fff38076 100644 --- a/examples/src/main/scala/sttp/tapir/examples/observability/DatadogMetricsExample.scala +++ b/examples/src/main/scala/sttp/tapir/examples/observability/DatadogMetricsExample.scala @@ -1,7 +1,7 @@ package sttp.tapir.examples.observability import com.timgroup.statsd.NonBlockingStatsDClientBuilder -import com.typesafe.scalalogging.StrictLogging +import sttp.tapir.examples.logging.Logging import io.circe.generic.auto.* import sttp.tapir.* import sttp.tapir.generic.auto.* @@ -15,7 +15,7 @@ import scala.concurrent.duration.* import scala.concurrent.{Await, Future} import scala.io.StdIn -object DatadogMetricsExample extends App with StrictLogging { +object DatadogMetricsExample extends App with Logging { case class Person(name: String) diff --git a/examples/src/main/scala/sttp/tapir/examples/observability/OpenTelemetryMetricsExample.scala b/examples/src/main/scala/sttp/tapir/examples/observability/OpenTelemetryMetricsExample.scala index d1b32805a5..aec1dc1a35 100644 --- a/examples/src/main/scala/sttp/tapir/examples/observability/OpenTelemetryMetricsExample.scala +++ b/examples/src/main/scala/sttp/tapir/examples/observability/OpenTelemetryMetricsExample.scala @@ -1,6 +1,6 @@ package sttp.tapir.examples.observability -import com.typesafe.scalalogging.StrictLogging +import sttp.tapir.examples.logging.Logging import io.circe.generic.auto.* import io.opentelemetry.api.OpenTelemetry import io.opentelemetry.exporter.otlp.metrics.OtlpGrpcMetricExporter @@ -57,7 +57,7 @@ import scala.io.StdIn * ... * }}} */ -object OpenTelemetryMetricsExample extends App with StrictLogging { +object OpenTelemetryMetricsExample extends App with Logging { case class Person(name: String) diff --git a/examples/src/main/scala/sttp/tapir/examples/observability/PrometheusMetricsExample.scala b/examples/src/main/scala/sttp/tapir/examples/observability/PrometheusMetricsExample.scala index f7239b67db..e5933a4a39 100644 --- a/examples/src/main/scala/sttp/tapir/examples/observability/PrometheusMetricsExample.scala +++ b/examples/src/main/scala/sttp/tapir/examples/observability/PrometheusMetricsExample.scala @@ -1,6 +1,6 @@ package sttp.tapir.examples.observability -import com.typesafe.scalalogging.StrictLogging +import sttp.tapir.examples.logging.Logging import io.circe.generic.auto.* import sttp.tapir.* import sttp.tapir.generic.auto.* @@ -14,7 +14,7 @@ import scala.concurrent.duration.* import scala.concurrent.{Await, Future} import scala.io.StdIn -object PrometheusMetricsExample extends App with StrictLogging { +object PrometheusMetricsExample extends App with Logging { case class Person(name: String) diff --git a/examples/src/main/scala/sttp/tapir/examples/streaming/ProxyHttp4sFs2Server.scala b/examples/src/main/scala/sttp/tapir/examples/streaming/ProxyHttp4sFs2Server.scala index 2cba2b12e8..91f38362f2 100644 --- a/examples/src/main/scala/sttp/tapir/examples/streaming/ProxyHttp4sFs2Server.scala +++ b/examples/src/main/scala/sttp/tapir/examples/streaming/ProxyHttp4sFs2Server.scala @@ -1,7 +1,7 @@ package sttp.tapir.examples.streaming import cats.effect.{ExitCode, IO, IOApp} -import com.typesafe.scalalogging.StrictLogging +import sttp.tapir.examples.logging.Logging import fs2.Stream import org.http4s.HttpRoutes import org.http4s.blaze.server.BlazeServerBuilder @@ -14,7 +14,7 @@ import sttp.tapir.* import sttp.tapir.server.http4s.Http4sServerInterpreter /** Proxies requests from /proxy to https://httpbin.org/anything */ -object ProxyHttp4sFs2Server extends IOApp with StrictLogging { +object ProxyHttp4sFs2Server extends IOApp with Logging { val proxyEndpoint: PublicEndpoint[ (Method, List[String], QueryParams, List[Header], Stream[IO, Byte]), Unit, diff --git a/grpc/examples/src/main/scala/sttp/tapir/grpc/examples/GrpcSimpleBooksExample.scala b/grpc/examples/src/main/scala/sttp/tapir/grpc/examples/GrpcSimpleBooksExample.scala index fb118ade6b..d14b839799 100644 --- a/grpc/examples/src/main/scala/sttp/tapir/grpc/examples/GrpcSimpleBooksExample.scala +++ b/grpc/examples/src/main/scala/sttp/tapir/grpc/examples/GrpcSimpleBooksExample.scala @@ -5,7 +5,7 @@ import akka.grpc.GrpcClientSettings import akka.http.scaladsl.Http import cats.implicits._ import com.typesafe.config.ConfigFactory -import com.typesafe.scalalogging.StrictLogging +import org.slf4j.{Logger, LoggerFactory} import sttp.tapir._ import sttp.tapir.grpc.protobuf._ import sttp.tapir.grpc.protobuf.pbdirect._ @@ -21,6 +21,9 @@ import sttp.tapir.grpc.examples.grpc_simple_books_example.gen.{ import java.util.concurrent.atomic.AtomicLong import scala.concurrent.{Await, ExecutionContext, Future} +trait Logging { + protected val logger: Logger = LoggerFactory.getLogger(getClass.getName) +} case class SimpleBook(id: Long, title: String, description: String) case class AddBookMsg(title: String, description: String) @@ -35,7 +38,7 @@ object Endpoints { val endpoints = List(addBook) } -object SimpleBooksExampleServer extends StrictLogging { +object SimpleBooksExampleServer extends Logging { import Endpoints._ @@ -59,7 +62,7 @@ object SimpleBooksExampleServer extends StrictLogging { } } -class ExampleGrpcServer(system: ActorSystem) extends StrictLogging { +class ExampleGrpcServer(system: ActorSystem) extends Logging { def run(): Future[Http.ServerBinding] = { // Akka boot up code implicit val sys: ActorSystem = system @@ -84,7 +87,7 @@ object SimpleBookExampleProtoGenerator extends App { ) } -object SimpleBookExampleClient extends App with StrictLogging { +object SimpleBookExampleClient extends App with Logging { import scala.concurrent.duration._ diff --git a/grpc/pekko-examples/src/main/scala/sttp/tapir/grpc/examples/PekkoGrpcSimpleBooksExample.scala b/grpc/pekko-examples/src/main/scala/sttp/tapir/grpc/examples/PekkoGrpcSimpleBooksExample.scala index 3021adf857..f424174510 100644 --- a/grpc/pekko-examples/src/main/scala/sttp/tapir/grpc/examples/PekkoGrpcSimpleBooksExample.scala +++ b/grpc/pekko-examples/src/main/scala/sttp/tapir/grpc/examples/PekkoGrpcSimpleBooksExample.scala @@ -5,7 +5,7 @@ import org.apache.pekko.grpc.GrpcClientSettings import org.apache.pekko.http.scaladsl.Http import cats.implicits._ import com.typesafe.config.ConfigFactory -import com.typesafe.scalalogging.StrictLogging +import org.slf4j.{Logger, LoggerFactory} import sttp.tapir._ import sttp.tapir.grpc.protobuf._ import sttp.tapir.grpc.protobuf.pbdirect._ @@ -21,6 +21,10 @@ import sttp.tapir.grpc.examples.grpc_simple_books_example.gen.{ import java.util.concurrent.atomic.AtomicLong import scala.concurrent.{Await, ExecutionContext, Future} +trait Logging { + protected val logger: Logger = LoggerFactory.getLogger(getClass.getName) +} + case class SimpleBook(id: Long, title: String, description: String) case class AddBookMsg(title: String, description: String) @@ -35,7 +39,7 @@ object Endpoints { val endpoints = List(addBook) } -object SimpleBooksExampleServer extends StrictLogging { +object SimpleBooksExampleServer extends Logging { import Endpoints._ @@ -59,7 +63,7 @@ object SimpleBooksExampleServer extends StrictLogging { } } -class ExampleGrpcServer(system: ActorSystem) extends StrictLogging { +class ExampleGrpcServer(system: ActorSystem) extends Logging { def run(): Future[Http.ServerBinding] = { // Pekko boot up code implicit val sys: ActorSystem = system @@ -84,7 +88,7 @@ object SimpleBookExampleProtoGenerator extends App { ) } -object SimpleBookExampleClient extends App with StrictLogging { +object SimpleBookExampleClient extends App with Logging { import scala.concurrent.duration._ diff --git a/grpc/protobuf/src/main/scala/sttp/tapir/grpc/protobuf/ProtoSchemaGenerator.scala b/grpc/protobuf/src/main/scala/sttp/tapir/grpc/protobuf/ProtoSchemaGenerator.scala index 4754e5946e..c8069693c3 100644 --- a/grpc/protobuf/src/main/scala/sttp/tapir/grpc/protobuf/ProtoSchemaGenerator.scala +++ b/grpc/protobuf/src/main/scala/sttp/tapir/grpc/protobuf/ProtoSchemaGenerator.scala @@ -4,13 +4,15 @@ import java.nio.file.Files import java.nio.file.Paths import java.nio.charset.StandardCharsets import java.nio.file.StandardOpenOption -import com.typesafe.scalalogging.StrictLogging +import org.slf4j.LoggerFactory import sttp.tapir.AnyEndpoint import sttp.tapir.grpc.protobuf.model._ -object ProtoSchemaGenerator extends StrictLogging { +object ProtoSchemaGenerator { + + private val logger = LoggerFactory.getLogger(getClass.getName) def renderToFile(path: String, packageName: PackageName, endpoints: Iterable[AnyEndpoint]): Unit = { - logger.info(s"Generating proto file") + logger.info("Generating proto file") val renderer: ProtoRenderer = new ProtoRenderer() val interpreter = new ProtobufInterpreter(new EndpointToProtobufMessage(), new EndpointToProtobufService()) diff --git a/project/Versions.scala b/project/Versions.scala index 1043a52edb..f75d8eee54 100644 --- a/project/Versions.scala +++ b/project/Versions.scala @@ -61,4 +61,6 @@ object Versions { val mockServer = "5.15.0" val dogstatsdClient = "4.3.0" val nettyAll = "4.1.106.Final" + val logback = "1.4.14" + val slf4j = "2.0.11" } diff --git a/server/netty-server/cats/src/main/scala/sttp/tapir/server/netty/cats/NettyCatsServerOptions.scala b/server/netty-server/cats/src/main/scala/sttp/tapir/server/netty/cats/NettyCatsServerOptions.scala index 8a399bfeff..ef6c24e820 100644 --- a/server/netty-server/cats/src/main/scala/sttp/tapir/server/netty/cats/NettyCatsServerOptions.scala +++ b/server/netty-server/cats/src/main/scala/sttp/tapir/server/netty/cats/NettyCatsServerOptions.scala @@ -2,10 +2,11 @@ package sttp.tapir.server.netty.cats import cats.effect.std.Dispatcher import cats.effect.{Async, Sync} -import com.typesafe.scalalogging.Logger +import org.slf4j.LoggerFactory import sttp.tapir.model.ServerRequest import sttp.tapir.server.interceptor.log.DefaultServerLog import sttp.tapir.server.interceptor.{CustomiseInterceptors, Interceptor} +import sttp.tapir.server.netty.NettyFutureServerOptions.getClass import sttp.tapir.server.netty.internal.NettyDefaults import sttp.tapir.{Defaults, TapirFile} @@ -46,7 +47,7 @@ object NettyCatsServerOptions { createOptions = (ci: CustomiseInterceptors[F, NettyCatsServerOptions[F]]) => default(ci.interceptors, dispatcher) ).serverLog(defaultServerLog) - private val log = Logger[NettyCatsServerInterpreter[cats.Id]] + private val log = LoggerFactory.getLogger(classOf[NettyCatsServerInterpreter[cats.Id]].getName) def defaultServerLog[F[_]: Async]: DefaultServerLog[F] = DefaultServerLog( doLogWhenReceived = debugLog(_, None), diff --git a/server/netty-server/loom/src/main/scala/sttp/tapir/server/netty/loom/NettyIdServerOptions.scala b/server/netty-server/loom/src/main/scala/sttp/tapir/server/netty/loom/NettyIdServerOptions.scala index 5a48c90e58..4d7659570c 100644 --- a/server/netty-server/loom/src/main/scala/sttp/tapir/server/netty/loom/NettyIdServerOptions.scala +++ b/server/netty-server/loom/src/main/scala/sttp/tapir/server/netty/loom/NettyIdServerOptions.scala @@ -1,6 +1,6 @@ package sttp.tapir.server.netty.loom -import com.typesafe.scalalogging.Logger +import org.slf4j.LoggerFactory import sttp.tapir.model.ServerRequest import sttp.tapir.server.interceptor.log.{DefaultServerLog, ServerLog} import sttp.tapir.server.netty.internal.NettyDefaults @@ -40,7 +40,7 @@ object NettyIdServerOptions { ).serverLog(defaultServerLog) } - private val log = Logger[NettyIdServerInterpreter] + private val log = LoggerFactory.getLogger(getClass.getName) lazy val defaultServerLog: ServerLog[Id] = { DefaultServerLog[Id]( diff --git a/server/netty-server/src/main/scala/sttp/tapir/server/netty/NettyFutureServerOptions.scala b/server/netty-server/src/main/scala/sttp/tapir/server/netty/NettyFutureServerOptions.scala index d33b48e2db..fe9160cc6c 100644 --- a/server/netty-server/src/main/scala/sttp/tapir/server/netty/NettyFutureServerOptions.scala +++ b/server/netty-server/src/main/scala/sttp/tapir/server/netty/NettyFutureServerOptions.scala @@ -1,6 +1,6 @@ package sttp.tapir.server.netty -import com.typesafe.scalalogging.Logger +import org.slf4j.LoggerFactory import sttp.tapir.model.ServerRequest import sttp.tapir.server.interceptor.log.DefaultServerLog import sttp.tapir.server.interceptor.{CustomiseInterceptors, Interceptor} @@ -46,7 +46,7 @@ object NettyFutureServerOptions { ).serverLog(defaultServerLog) } - private val log = Logger[NettyFutureServerInterpreter] + private val log = LoggerFactory.getLogger(getClass.getName) lazy val defaultServerLog: DefaultServerLog[Future] = { DefaultServerLog( diff --git a/server/netty-server/src/main/scala/sttp/tapir/server/netty/internal/NettyDefaults.scala b/server/netty-server/src/main/scala/sttp/tapir/server/netty/internal/NettyDefaults.scala index 5cd2640b2e..4f12013158 100644 --- a/server/netty-server/src/main/scala/sttp/tapir/server/netty/internal/NettyDefaults.scala +++ b/server/netty-server/src/main/scala/sttp/tapir/server/netty/internal/NettyDefaults.scala @@ -1,11 +1,13 @@ package sttp.tapir.server.netty.internal -import com.typesafe.scalalogging.Logger +import org.slf4j.Logger object NettyDefaults { def debugLog(log: Logger, msg: String, exOpt: Option[Throwable]): Unit = - exOpt match { - case None => log.debug(msg) - case Some(ex) => log.debug(s"$msg; exception: {}", ex) + if (log.isDebugEnabled) { + exOpt match { + case None => log.debug(msg) + case Some(ex) => log.debug(msg, ex) + } } } diff --git a/server/netty-server/src/main/scala/sttp/tapir/server/netty/internal/NettyServerHandler.scala b/server/netty-server/src/main/scala/sttp/tapir/server/netty/internal/NettyServerHandler.scala index c7e32baf9b..eac79911e5 100644 --- a/server/netty-server/src/main/scala/sttp/tapir/server/netty/internal/NettyServerHandler.scala +++ b/server/netty-server/src/main/scala/sttp/tapir/server/netty/internal/NettyServerHandler.scala @@ -1,6 +1,5 @@ package sttp.tapir.server.netty.internal -import com.typesafe.scalalogging.Logger import io.netty.buffer.{ByteBuf, Unpooled} import io.netty.channel._ import io.netty.channel.group.ChannelGroup @@ -8,6 +7,7 @@ import io.netty.handler.codec.http._ import io.netty.handler.stream.{ChunkedFile, ChunkedStream} import org.playframework.netty.http.{DefaultStreamedHttpResponse, StreamedHttpRequest} import org.reactivestreams.Publisher +import org.slf4j.{Logger, LoggerFactory} import sttp.monad.MonadError import sttp.monad.syntax._ import sttp.tapir.server.model.ServerResponse @@ -60,7 +60,7 @@ class NettyServerHandler[F[_]]( // if the connection gets closed. private[this] val pendingResponses = MutableQueue.empty[() => Future[Unit]] - private val logger = Logger[NettyServerHandler[F]] + private val logger = LoggerFactory.getLogger(getClass.getName) override def handlerAdded(ctx: ChannelHandlerContext): Unit = if (ctx.channel.isActive) { @@ -79,7 +79,12 @@ class NettyServerHandler[F[_]]( // When the channel closes we want to cancel any pending dispatches. // Since the listener will be executed from the channels EventLoop everything is thread safe. val _ = ctx.channel.closeFuture.addListener { (_: ChannelFuture) => - logger.debug(s"Http channel to ${ctx.channel.remoteAddress} closed. Cancelling ${pendingResponses.length} responses.") + if (logger.isDebugEnabled) { + logger.debug("Http channel to {} closed. Cancelling {} responses.", + ctx.channel.remoteAddress, + pendingResponses.length + ) + } pendingResponses.foreach(_.apply()) } } diff --git a/server/netty-server/zio/src/main/scala/sttp/tapir/server/netty/zio/NettyZioServerOptions.scala b/server/netty-server/zio/src/main/scala/sttp/tapir/server/netty/zio/NettyZioServerOptions.scala index 4f6593c700..2b25e067ef 100644 --- a/server/netty-server/zio/src/main/scala/sttp/tapir/server/netty/zio/NettyZioServerOptions.scala +++ b/server/netty-server/zio/src/main/scala/sttp/tapir/server/netty/zio/NettyZioServerOptions.scala @@ -1,12 +1,11 @@ package sttp.tapir.server.netty.zio -import com.typesafe.scalalogging.Logger import sttp.tapir.model.ServerRequest import sttp.tapir.server.interceptor.log.DefaultServerLog import sttp.tapir.server.interceptor.{CustomiseInterceptors, Interceptor} import sttp.tapir.server.netty.internal.NettyDefaults import sttp.tapir.{Defaults, TapirFile} -import zio.{RIO, ZIO} +import zio.{Cause, RIO, ZIO} /** Options configuring the [[NettyZioServerInterpreter]], which is being used by [[NettyZioServer]] to interpret tapir's * [[sttp.tapir.server.ServerEndpoint]]s so that they can be served using a Netty server. Contains the interceptors stack and functions for @@ -40,17 +39,18 @@ object NettyZioServerOptions { createOptions = (ci: CustomiseInterceptors[RIO[R, *], NettyZioServerOptions[R]]) => default(ci.interceptors) ).serverLog(defaultServerLog[R]) - private val log: Logger = Logger[NettyZioServerInterpreter[Any]] - def defaultServerLog[R]: DefaultServerLog[RIO[R, *]] = DefaultServerLog( doLogWhenReceived = debugLog(_, None), doLogWhenHandled = debugLog[R], doLogAllDecodeFailures = debugLog[R], - doLogExceptions = (msg: String, ex: Throwable) => ZIO.succeed { log.warn(msg, ex) }, + doLogExceptions = (msg: String, ex: Throwable) => ZIO.logErrorCause(msg, Cause.die(ex)), noLog = ZIO.unit ) private def debugLog[R](msg: String, exOpt: Option[Throwable]): RIO[R, Unit] = - ZIO.succeed(NettyDefaults.debugLog(log, msg, exOpt)) + exOpt match { + case None => ZIO.logDebug(msg) + case Some(ex) => ZIO.logDebugCause(msg, Cause.fail(ex)) + } } diff --git a/server/nima-server/src/main/scala/sttp/tapir/server/nima/NimaServerOptions.scala b/server/nima-server/src/main/scala/sttp/tapir/server/nima/NimaServerOptions.scala index 64ef7f0092..70f6a85f6d 100644 --- a/server/nima-server/src/main/scala/sttp/tapir/server/nima/NimaServerOptions.scala +++ b/server/nima-server/src/main/scala/sttp/tapir/server/nima/NimaServerOptions.scala @@ -1,12 +1,11 @@ package sttp.tapir.server.nima +import org.slf4j.LoggerFactory import sttp.tapir.{Defaults, TapirFile} import sttp.tapir.model.ServerRequest import sttp.tapir.server.interceptor.log.{DefaultServerLog, ServerLog} import sttp.tapir.server.interceptor.{CustomiseInterceptors, Interceptor} -import java.util.logging.{Level, Logger} - case class NimaServerOptions( interceptors: List[Interceptor[Id]], createFile: ServerRequest => TapirFile, @@ -27,19 +26,22 @@ object NimaServerOptions { createOptions = (ci: CustomiseInterceptors[Id, NimaServerOptions]) => default(ci.interceptors) ).serverLog(defaultServerLog) - private val log = Logger.getLogger(classOf[NimaServerInterpreter].getName) + private val log = LoggerFactory.getLogger(classOf[NimaServerInterpreter].getName) lazy val defaultServerLog: ServerLog[Id] = DefaultServerLog[Id]( doLogWhenReceived = debugLog(_, None), doLogWhenHandled = debugLog, doLogAllDecodeFailures = debugLog, - doLogExceptions = (msg: String, ex: Throwable) => log.log(Level.SEVERE, msg, ex), + doLogExceptions = (msg: String, ex: Throwable) => log.error(msg, ex), noLog = () ) - private def debugLog(msg: String, exOpt: Option[Throwable]): Unit = exOpt match { - case Some(e) => log.log(Level.FINE, msg, e) - case None => log.log(Level.FINE, msg) - } + private def debugLog(msg: String, exOpt: Option[Throwable]): Unit = + if (log.isDebugEnabled) { + exOpt match { + case Some(e) => log.debug(msg, e) + case None => log.debug(msg) + } + } } diff --git a/server/tests/src/main/scala/sttp/tapir/server/tests/CreateServerTest.scala b/server/tests/src/main/scala/sttp/tapir/server/tests/CreateServerTest.scala index b717469d51..2cbcfcb546 100644 --- a/server/tests/src/main/scala/sttp/tapir/server/tests/CreateServerTest.scala +++ b/server/tests/src/main/scala/sttp/tapir/server/tests/CreateServerTest.scala @@ -4,7 +4,6 @@ import cats.data.NonEmptyList import cats.effect.unsafe.implicits.global import cats.effect.{IO, Resource} import cats.implicits._ -import com.typesafe.scalalogging.StrictLogging import org.scalatest.Assertion import sttp.capabilities.WebSockets import sttp.capabilities.fs2.Fs2Streams @@ -15,6 +14,8 @@ import sttp.tapir.server.ServerEndpoint import sttp.tapir.server.interceptor.CustomiseInterceptors import sttp.tapir.tests._ import org.scalactic.anyvals.FiniteDouble +import org.slf4j.LoggerFactory + import scala.concurrent.duration.FiniteDuration trait CreateServerTest[F[_], +R, OPTIONS, ROUTE] { @@ -58,8 +59,9 @@ trait CreateServerTest[F[_], +R, OPTIONS, ROUTE] { class DefaultCreateServerTest[F[_], +R, OPTIONS, ROUTE]( backend: SttpBackend[IO, Fs2Streams[IO] with WebSockets], interpreter: TestServerInterpreter[F, R, OPTIONS, ROUTE] -) extends CreateServerTest[F, R, OPTIONS, ROUTE] - with StrictLogging { +) extends CreateServerTest[F, R, OPTIONS, ROUTE] { + + private val logger = LoggerFactory.getLogger(getClass.getName) override def testServer[I, E, O]( e: PublicEndpoint[I, E, O, R], diff --git a/serverless/aws/lambda-cats-effect/src/main/scala/sttp/tapir/serverless/aws/lambda/runtime/AwsLambdaRuntimeInvocation.scala b/serverless/aws/lambda-cats-effect/src/main/scala/sttp/tapir/serverless/aws/lambda/runtime/AwsLambdaRuntimeInvocation.scala index 0919cc62c9..ce17c0b455 100644 --- a/serverless/aws/lambda-cats-effect/src/main/scala/sttp/tapir/serverless/aws/lambda/runtime/AwsLambdaRuntimeInvocation.scala +++ b/serverless/aws/lambda-cats-effect/src/main/scala/sttp/tapir/serverless/aws/lambda/runtime/AwsLambdaRuntimeInvocation.scala @@ -2,7 +2,7 @@ package sttp.tapir.serverless.aws.lambda.runtime import cats.effect.{Resource, Sync} import cats.syntax.either._ -import com.typesafe.scalalogging.StrictLogging +import org.slf4j.LoggerFactory import io.circe.Printer import io.circe.generic.auto._ import io.circe.parser.decode @@ -16,7 +16,9 @@ import sttp.tapir.serverless.aws.lambda.{AwsRequest, AwsResponse, Route} import scala.concurrent.duration.DurationInt // loosely based on https://github.com/carpe/scalambda/blob/master/native/src/main/scala/io/carpe/scalambda/native/ScalambdaIO.scala -object AwsLambdaRuntimeInvocation extends StrictLogging { +object AwsLambdaRuntimeInvocation { + + private val logger = LoggerFactory.getLogger(getClass.getName) /** Handles the next, single lambda invocation, read from api at `awsRuntimeApiHost` using `backend`, with the given `route`. */ def handleNext[F[_]: Sync]( @@ -34,7 +36,7 @@ object AwsLambdaRuntimeInvocation extends StrictLogging { val nextEventRequest = basicRequest.get(uri"$runtimeApiInvocationUri/next").response(asStringAlways).readTimeout(0.seconds) val pollEvent: F[RequestEvent] = { - logger.info(s"Fetching request event") + logger.info("Fetching request event") backend .use(nextEventRequest.send(_)) .flatMap { response =>