Repository: incubator-s2graph
Updated Branches:
  refs/heads/master e0b2dc0bc -> 488ee0f48


add logging for Management API and HTTP Request/Reponse


Project: http://git-wip-us.apache.org/repos/asf/incubator-s2graph/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-s2graph/commit/d137bb30
Tree: http://git-wip-us.apache.org/repos/asf/incubator-s2graph/tree/d137bb30
Diff: http://git-wip-us.apache.org/repos/asf/incubator-s2graph/diff/d137bb30

Branch: refs/heads/master
Commit: d137bb3056d79cfe5df22baf109b873117d19629
Parents: 653deee
Author: daewon <[email protected]>
Authored: Thu Mar 29 16:36:36 2018 +0900
Committer: daewon <[email protected]>
Committed: Thu Mar 29 16:36:36 2018 +0900

----------------------------------------------------------------------
 project/Common.scala                            |  3 +-
 .../apache/s2graph/graphql/GraphQLServer.scala  | 28 ++++++++-----
 .../org/apache/s2graph/graphql/HttpServer.scala | 32 +++++++--------
 .../graphql/repository/GraphRepository.scala    | 41 ++++++++++++++++----
 4 files changed, 70 insertions(+), 34 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-s2graph/blob/d137bb30/project/Common.scala
----------------------------------------------------------------------
diff --git a/project/Common.scala b/project/Common.scala
index e2323a2..94b3589 100644
--- a/project/Common.scala
+++ b/project/Common.scala
@@ -48,7 +48,8 @@ object Common {
     ExclusionRule("org.slf4j", "jcl-over-slf4j"),
     ExclusionRule("org.slf4j", "log4j-over-slf4j"),
     ExclusionRule("org.slf4j", "slf4j-log4j12"),
-    ExclusionRule("org.slf4j", "jul-to-slf4j")
+    ExclusionRule("org.slf4j", "jul-to-slf4j"),
+    ExclusionRule("org.apache.logging.log4j", "log4j-slf4j-impl")
   )
 
   implicit class LoggingExcluder(moduleId: ModuleID) {

http://git-wip-us.apache.org/repos/asf/incubator-s2graph/blob/d137bb30/s2graphql/src/main/scala/org/apache/s2graph/graphql/GraphQLServer.scala
----------------------------------------------------------------------
diff --git 
a/s2graphql/src/main/scala/org/apache/s2graph/graphql/GraphQLServer.scala 
b/s2graphql/src/main/scala/org/apache/s2graph/graphql/GraphQLServer.scala
index 3365593..e4b43cc 100644
--- a/s2graphql/src/main/scala/org/apache/s2graph/graphql/GraphQLServer.scala
+++ b/s2graphql/src/main/scala/org/apache/s2graph/graphql/GraphQLServer.scala
@@ -29,7 +29,7 @@ import com.typesafe.config.ConfigFactory
 import org.apache.s2graph.core.S2Graph
 import org.apache.s2graph.core.utils.SafeUpdateCache
 import org.apache.s2graph.graphql.repository.GraphRepository
-import org.apache.s2graph.graphql.types._
+import org.slf4j.LoggerFactory
 import sangria.ast.Document
 import sangria.execution._
 import sangria.marshalling.sprayJson._
@@ -42,6 +42,8 @@ import scala.util.{Failure, Success, Try}
 
 object GraphQLServer {
 
+  val logger = LoggerFactory.getLogger(this.getClass)
+
   // Init s2graph
   val numOfThread = Runtime.getRuntime.availableProcessors()
   val threadPool = Executors.newFixedThreadPool(numOfThread * 2)
@@ -69,8 +71,12 @@ object GraphQLServer {
     }
 
     QueryParser.parse(query) match {
-      case Success(queryAst) => complete(executeGraphQLQuery(queryAst, 
operation, vars))
-      case Failure(error) => complete(BadRequest -> 
spray.json.JsObject("error" -> JsString(error.getMessage)))
+      case Success(queryAst) =>
+        logger.info(queryAst.renderCompact)
+        complete(executeGraphQLQuery(queryAst, operation, vars))
+      case Failure(error) =>
+        logger.warn(error.getMessage, error)
+        complete(BadRequest -> spray.json.JsObject("error" -> 
JsString(error.getMessage)))
     }
   }
 
@@ -78,13 +84,13 @@ object GraphQLServer {
     * In development mode(schemaCacheTTL = -1),
     * a new schema is created for each request.
     */
-  println(s"schemaCacheTTL: ${schemaCacheTTL}")
+  logger.info(s"schemaCacheTTL: ${schemaCacheTTL}")
 
   private def createNewSchema(): Schema[GraphRepository, Any] = {
-    println(s"Schema updated: ${System.currentTimeMillis()}")
+    logger.info(s"Schema updated: ${System.currentTimeMillis()}")
     val newSchema = new SchemaDef(s2Repository).S2GraphSchema
-    //    println(SchemaRenderer.renderSchema(newSchema))
-    println("-" * 80)
+    logger.info("-" * 80)
+
     newSchema
   }
 
@@ -100,8 +106,12 @@ object GraphQLServer {
     )
       .map((res: spray.json.JsValue) => OK -> res)
       .recover {
-        case error: QueryAnalysisError => BadRequest -> error.resolveError
-        case error: ErrorWithResolver => InternalServerError -> 
error.resolveError
+        case error: QueryAnalysisError =>
+          logger.warn(error.getMessage, error)
+          BadRequest -> error.resolveError
+        case error: ErrorWithResolver =>
+          logger.error(error.getMessage, error)
+          InternalServerError -> error.resolveError
       }
   }
 }

http://git-wip-us.apache.org/repos/asf/incubator-s2graph/blob/d137bb30/s2graphql/src/main/scala/org/apache/s2graph/graphql/HttpServer.scala
----------------------------------------------------------------------
diff --git 
a/s2graphql/src/main/scala/org/apache/s2graph/graphql/HttpServer.scala 
b/s2graphql/src/main/scala/org/apache/s2graph/graphql/HttpServer.scala
index 9535b0e..7d3771a 100644
--- a/s2graphql/src/main/scala/org/apache/s2graph/graphql/HttpServer.scala
+++ b/s2graphql/src/main/scala/org/apache/s2graph/graphql/HttpServer.scala
@@ -25,38 +25,38 @@ import 
akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
 import akka.http.scaladsl.server.Directives._
 import akka.http.scaladsl.server._
 import akka.stream.ActorMaterializer
+import org.slf4j.LoggerFactory
 
-import scala.Console._
 import scala.concurrent.Await
 import scala.language.postfixOps
 
 object Server extends App {
+  val logger = LoggerFactory.getLogger(this.getClass)
 
-  implicit val actorSystem = ActorSystem("s2graphql-server")
+  implicit val system = ActorSystem("s2graphql-server")
   implicit val materializer = ActorMaterializer()
 
-  import actorSystem.dispatcher
+  import system.dispatcher
 
   import scala.concurrent.duration._
 
-  println("Starting GRAPHQL server...")
-
-  val route: Route =
-    (post & path("graphql")) {
-      entity(as[spray.json.JsValue])(GraphQLServer.endpoint)
-    } ~ {
-      getFromResource("assets/graphiql.html")
-    }
+  val route: Route = (post & path("graphql")) {
+    entity(as[spray.json.JsValue])(GraphQLServer.endpoint)
+  } ~ {
+    getFromResource("assets/graphiql.html")
+  }
 
   val port = sys.props.get("http.port").fold(8000)(_.toInt)
-  Http().bindAndHandle(route, "0.0.0.0", port)
 
+  logger.info(s"Starting GraphQL server... ${port}")
+  Http().bindAndHandle(route, "0.0.0.0", port)
 
   def shutdown(): Unit = {
-    println("Terminating...")
-    actorSystem.terminate()
-    Await.result(actorSystem.whenTerminated, 10 seconds)
+    logger.info("Terminating...")
+
+    system.terminate()
+    Await.result(system.whenTerminated, 30 seconds)
 
-    println("Terminated.")
+    logger.info("Terminated.")
   }
 }

http://git-wip-us.apache.org/repos/asf/incubator-s2graph/blob/d137bb30/s2graphql/src/main/scala/org/apache/s2graph/graphql/repository/GraphRepository.scala
----------------------------------------------------------------------
diff --git 
a/s2graphql/src/main/scala/org/apache/s2graph/graphql/repository/GraphRepository.scala
 
b/s2graphql/src/main/scala/org/apache/s2graph/graphql/repository/GraphRepository.scala
index c934fd5..a29d0fb 100644
--- 
a/s2graphql/src/main/scala/org/apache/s2graph/graphql/repository/GraphRepository.scala
+++ 
b/s2graphql/src/main/scala/org/apache/s2graph/graphql/repository/GraphRepository.scala
@@ -26,12 +26,23 @@ import org.apache.s2graph.core.rest.RequestParser
 import org.apache.s2graph.core.storage.MutateResponse
 import org.apache.s2graph.core.types._
 import org.apache.s2graph.graphql.types.S2Type._
+import org.slf4j.{Logger, LoggerFactory}
 import sangria.schema._
 
 import scala.concurrent._
-import scala.util.{Failure, Try}
+import scala.util.{Failure, Success, Try}
 
 object GraphRepository {
+
+  def withLogTryResponse[A](opName: String, tryObj: Try[A])(implicit logger: 
Logger): Try[A] = {
+    tryObj match {
+      case Success(v) => logger.info(s"${opName} Success:", v)
+      case Failure(e) => logger.warn(s"${opName} Failed:", e)
+    }
+
+    tryObj
+  }
+
 }
 
 /**
@@ -39,6 +50,9 @@ object GraphRepository {
   * @param graph
   */
 class GraphRepository(val graph: S2GraphLike) {
+  implicit val logger = LoggerFactory.getLogger(this.getClass)
+
+  import GraphRepository._
 
   val management = graph.management
   val parser = new RequestParser(graph)
@@ -92,7 +106,7 @@ class GraphRepository(val graph: S2GraphLike) {
   def createService(args: Args): Try[Service] = {
     val serviceName = args.arg[String]("name")
 
-    Service.findByName(serviceName) match {
+    val serviceTry = Service.findByName(serviceName) match {
       case Some(_) => Failure(new RuntimeException(s"Service (${serviceName}) 
already exists"))
       case None =>
         val cluster = 
args.argOpt[String]("cluster").getOrElse(parser.DefaultCluster)
@@ -111,6 +125,8 @@ class GraphRepository(val graph: S2GraphLike) {
 
         serviceTry
     }
+
+    withLogTryResponse("createService", serviceTry)
   }
 
   def createServiceColumn(args: Args): Try[ServiceColumn] = {
@@ -119,9 +135,11 @@ class GraphRepository(val graph: S2GraphLike) {
     val columnType = args.arg[String]("columnType")
     val props = args.argOpt[Vector[Prop]]("props").getOrElse(Vector.empty)
 
-    Try {
+    val tryColumn = Try {
       management.createServiceColumn(serviceName, columnName, columnType, 
props)
     }
+
+    withLogTryResponse("createServiceColumn", tryColumn)
   }
 
   def deleteServiceColumn(args: Args): Try[ServiceColumn] = {
@@ -130,11 +148,13 @@ class GraphRepository(val graph: S2GraphLike) {
     val serviceName = serviceColumnParam.serviceName
     val columnName = serviceColumnParam.columnName
 
-    Management.deleteColumn(serviceName, columnName)
+    val deleteTry = Management.deleteColumn(serviceName, columnName)
+
+    withLogTryResponse("deleteServiceColumn", deleteTry)
   }
 
   def addPropsToLabel(args: Args): Try[Label] = {
-    Try {
+    val addPropToLabelTry = Try {
       val labelName = args.arg[String]("labelName")
       val props = args.arg[Vector[Prop]]("props").toList
 
@@ -144,10 +164,12 @@ class GraphRepository(val graph: S2GraphLike) {
 
       Label.findByName(labelName, false).get
     }
+
+    withLogTryResponse("addPropToLabel", addPropToLabelTry)
   }
 
   def addPropsToServiceColumn(args: Args): Try[ServiceColumn] = {
-    Try {
+    val addPropsToServiceColumnTry = Try {
       val serviceColumnParam = args.arg[ServiceColumnParam]("service")
 
       val serviceName = serviceColumnParam.serviceName
@@ -160,6 +182,8 @@ class GraphRepository(val graph: S2GraphLike) {
       val src = Service.findByName(serviceName)
       ServiceColumn.find(src.get.id.get, columnName, false).get
     }
+
+    withLogTryResponse("addPropsToServiceColumn", addPropsToServiceColumnTry)
   }
 
   def createLabel(args: Args): Try[Label] = {
@@ -205,13 +229,14 @@ class GraphRepository(val graph: S2GraphLike) {
       options
     )
 
-    labelTry
+    withLogTryResponse("createLabel", labelTry)
   }
 
   def deleteLabel(args: Args): Try[Label] = {
     val labelName = args.arg[String]("name")
 
-    Management.deleteLabel(labelName)
+    val deleteLabelTry = Management.deleteLabel(labelName)
+    withLogTryResponse("deleteLabel", deleteLabelTry)
   }
 
   def allServices(): List[Service] = Service.findAll()

Reply via email to