Skip to content

Commit

Permalink
Use akka-logging instead of slf4j directly (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
okapies authored and doriordan committed Dec 30, 2017
1 parent 06af70c commit a2f5abe
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 33 deletions.
3 changes: 1 addition & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"))
)

Expand Down
3 changes: 0 additions & 3 deletions client/src/main/scala/skuber/api/Watch.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
22 changes: 10 additions & 12 deletions client/src/main/scala/skuber/api/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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._
Expand All @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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}")
}
Expand Down Expand Up @@ -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 =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -45,7 +42,6 @@ object PlayJsonSupportForAkkaHttp extends PlayJsonSupportForAkkaHttp {
JsError.toJson(error).toString()
}

override val log: Logger = LoggerFactory.getLogger("skuber.api")
}

/**
Expand All @@ -54,8 +50,6 @@ object PlayJsonSupportForAkkaHttp extends PlayJsonSupportForAkkaHttp {
trait PlayJsonSupportForAkkaHttp {
import PlayJsonSupportForAkkaHttp._

val log: Logger

def unmarshallerContentTypes: Seq[ContentTypeRange] =
List(`application/json`)

Expand Down Expand Up @@ -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
*
Expand All @@ -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)
}
2 changes: 2 additions & 0 deletions examples/application.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
akka {
loggers = ["akka.event.slf4j.Slf4jLogger"]
# loglevel = "DEBUG"
logging-filter = "akka.event.slf4j.Slf4jLoggingFilter"
actor {
debug {
# receive = on
Expand Down

0 comments on commit a2f5abe

Please sign in to comment.