From a2f5abec1521108b9f4d64430b190d4f315760e4 Mon Sep 17 00:00:00 2001 From: Yuta Okamoto Date: Sat, 30 Dec 2017 19:41:44 +0900 Subject: [PATCH] Use akka-logging instead of slf4j directly (#97) --- build.sbt | 3 +-- client/src/main/scala/skuber/api/Watch.scala | 3 --- .../src/main/scala/skuber/api/package.scala | 22 +++++++++---------- .../json/PlayJsonSupportForAkkaHttp.scala | 17 +------------- examples/application.conf | 2 ++ 5 files changed, 14 insertions(+), 33 deletions(-) diff --git a/build.sbt b/build.sbt index f0d0193d..b36e9a9f 100644 --- a/build.sbt +++ b/build.sbt @@ -7,7 +7,6 @@ val specs2 = "org.specs2" %% "specs2-core" % "3.9.5" val snakeYaml = "org.yaml" % "snakeyaml" % "1.16" val commonsIO = "commons-io" % "commons-io" % "2.5" val commonsCodec = "commons-codec" % "commons-codec" % "1.10" -val sl4j = "org.slf4j" % "slf4j-api" % "1.7.25" // the client API request/response handing uses Akka Http // This also brings in the transitive dependencies on Akka actors and streams @@ -55,7 +54,7 @@ lazy val commonSettings = Seq( lazy val skuberSettings = Seq( name := "skuber", - libraryDependencies ++= Seq(akkaHttp, playJson, snakeYaml, commonsIO, commonsCodec, sl4j, scalaCheck % Test,specs2 % Test). + libraryDependencies ++= Seq(akkaHttp, playJson, snakeYaml, commonsIO, commonsCodec, scalaCheck % Test,specs2 % Test). map(_.exclude("commons-logging","commons-logging")) ) diff --git a/client/src/main/scala/skuber/api/Watch.scala b/client/src/main/scala/skuber/api/Watch.scala index 878360b9..7edcd041 100644 --- a/client/src/main/scala/skuber/api/Watch.scala +++ b/client/src/main/scala/skuber/api/Watch.scala @@ -14,7 +14,6 @@ import play.api.libs.json.{Format, JsError, JsSuccess, Json} import scala.concurrent.{ExecutionContext, ExecutionContextExecutor, Future} import scala.concurrent.ExecutionContext.Implicits.global -import org.slf4j.LoggerFactory import scala.language.postfixOps @@ -25,8 +24,6 @@ import scala.language.postfixOps */ object Watch { - val log = LoggerFactory.getLogger("skuber.api") - /** * Get a source of events on a specific Kubernetes resource * @param context the applicable request context diff --git a/client/src/main/scala/skuber/api/package.scala b/client/src/main/scala/skuber/api/package.scala index 335e35a8..843da02b 100644 --- a/client/src/main/scala/skuber/api/package.scala +++ b/client/src/main/scala/skuber/api/package.scala @@ -4,6 +4,7 @@ import java.net.URL import java.util.UUID import akka.actor.ActorSystem +import akka.event.Logging import akka.http.scaladsl.Http import akka.http.scaladsl.ConnectionContext import akka.http.scaladsl.model._ @@ -17,11 +18,10 @@ import play.api.libs.json.{Format, Reads} import skuber._ import skuber.api.security.{HTTPRequestAuth, TLS} import skuber.json.format._ -import scala.util.{Success, Failure} + +import scala.util.{Failure, Success} import skuber.json.format.apiobj._ import skuber.json.PlayJsonSupportForAkkaHttp._ -import org.slf4j.Logger -import org.slf4j.LoggerFactory import scala.sys.SystemProperties @@ -30,7 +30,6 @@ import scala.sys.SystemProperties */ package object client { - val log: Logger = LoggerFactory.getLogger("skuber.api") val sysProps = new SystemProperties // Certificates and keys can be specified in configuration either as paths to files or embedded PEM data @@ -124,6 +123,8 @@ package object client { val closeHook: Option[() => Unit]) (implicit val actorSystem: ActorSystem, val actorMaterializer: ActorMaterializer) { + val log = Logging.getLogger(actorSystem, "skuber.api") + implicit val dispatcher = actorSystem.dispatcher private var isClosed = false @@ -137,7 +138,7 @@ package object client { val responseFut = requestInvoker(request) responseFut onComplete { case Success(response) => logInfo(logConfig.logResponseBasic,s"received response with HTTP status ${response.status.intValue()}") - case Failure(ex) => logWarn("HTTP request resulted in an unexpected exception",ex) + case Failure(ex) => logError("HTTP request resulted in an unexpected exception",ex) } responseFut } @@ -211,11 +212,6 @@ package object client { log.error(s"[ ${lc.output} - $msg ]") } - private[skuber] def logWarn(msg: String, ex: Throwable)(implicit lc: LoggingContext) = - { - log.warn(s"[ ${lc.output} - $msg ]", ex) - } - private[skuber] def logError(msg: String, ex: Throwable)(implicit lc: LoggingContext) = { log.error(s"[ ${lc.output} - $msg ]", ex) @@ -387,7 +383,7 @@ package object client { val queryOpt = maybeLabelSelector map { ls => Uri.Query("labelSelector" -> ls.toString) } - if (log.isDebugEnabled()) { + if (log.isDebugEnabled) { val lsInfo = maybeLabelSelector map { ls => s" with label selector '${ls.toString}'" } getOrElse "" logDebug(s"[List request: resources of kind '${rd.spec.names.kind}'${lsInfo}") } @@ -613,8 +609,10 @@ package object client { def init(k8sContext: Context, logConfig: LoggingConfig, closeHook: Option[() => Unit]=None)( implicit actorSystem: ActorSystem, actorMaterializer: ActorMaterializer) : RequestContext = { - if (logConfig.logConfiguration) + if (logConfig.logConfiguration) { + val log = Logging.getLogger(actorSystem, "skuber.api") log.info("Using following context for connecting to Kubernetes cluster: {}", k8sContext) + } val sslContext = TLS.establishSSLContext(k8sContext) val theRequestAuth = HTTPRequestAuth.establishRequestAuth(k8sContext) sslContext foreach { ssl => diff --git a/client/src/main/scala/skuber/json/PlayJsonSupportForAkkaHttp.scala b/client/src/main/scala/skuber/json/PlayJsonSupportForAkkaHttp.scala index c1abcfc2..8bd75544 100644 --- a/client/src/main/scala/skuber/json/PlayJsonSupportForAkkaHttp.scala +++ b/client/src/main/scala/skuber/json/PlayJsonSupportForAkkaHttp.scala @@ -32,9 +32,6 @@ import akka.util.ByteString import play.api.libs.json.{ JsError, JsValue, Json, Reads, Writes } import scala.collection.immutable.Seq -import org.slf4j.Logger -import org.slf4j.LoggerFactory - /** * Automatic to and from JSON marshalling/unmarshalling using an in-scope *play-json* protocol. */ @@ -45,7 +42,6 @@ object PlayJsonSupportForAkkaHttp extends PlayJsonSupportForAkkaHttp { JsError.toJson(error).toString() } - override val log: Logger = LoggerFactory.getLogger("skuber.api") } /** @@ -54,8 +50,6 @@ object PlayJsonSupportForAkkaHttp extends PlayJsonSupportForAkkaHttp { trait PlayJsonSupportForAkkaHttp { import PlayJsonSupportForAkkaHttp._ - val log: Logger - def unmarshallerContentTypes: Seq[ContentTypeRange] = List(`application/json`) @@ -85,19 +79,10 @@ trait PlayJsonSupportForAkkaHttp { ) } jsonStringUnmarshaller.map { data => - if (log.isDebugEnabled) { - log.debug(s"[Skuber reading: $data]") - } read(Json.parse(data)) } } - val logMarshalled: String => String = { marshalled => - if(log.isDebugEnabled) { - log.debug(s"[Skuber writing; $marshalled") - } - marshalled - } /** * `A` => HTTP entity * @@ -108,5 +93,5 @@ trait PlayJsonSupportForAkkaHttp { implicit writes: Writes[A], printer: JsValue => String = Json.prettyPrint ): ToEntityMarshaller[A] = - jsonStringMarshaller.compose(logMarshalled).compose(printer).compose(writes.writes) + jsonStringMarshaller.compose(printer).compose(writes.writes) } \ No newline at end of file diff --git a/examples/application.conf b/examples/application.conf index 396baf78..45788f55 100644 --- a/examples/application.conf +++ b/examples/application.conf @@ -1,5 +1,7 @@ akka { + loggers = ["akka.event.slf4j.Slf4jLogger"] # loglevel = "DEBUG" + logging-filter = "akka.event.slf4j.Slf4jLoggingFilter" actor { debug { # receive = on