Skip to content

Commit

Permalink
feat: use logback
Browse files Browse the repository at this point in the history
  • Loading branch information
tkqubo committed Feb 8, 2016
1 parent e459ca5 commit 095a09e
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 6 deletions.
3 changes: 1 addition & 2 deletions project/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ object Build extends sbt.Build {
"org.specs2" %% "specs2-matcher-extra" % specs2Version % "test",
"org.specs2" %% "specs2-mock" % specs2Version % "test",
"org.scalatest" % "scalatest_2.11" % "2.2.1" % "test",
"org.slf4j" % "slf4j-nop" % slf4jVersion,
"org.slf4j" % "slf4j-api" % slf4jVersion,
"ch.qos.logback" % "logback-classic" % "1.1.2" % "runtime,test",
"com.github.scala-incubator.io" %% "scala-io-file" % "0.4.3",
"io.strongtyped" %% "active-slick" % "0.3.3",
"mysql" % "mysql-connector-java" % "5.1.34",
Expand Down
32 changes: 32 additions & 0 deletions src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
Default logback configuration provided for import, equivalent to the programmatic
initialization performed by Boot
-->

<configuration>
<property name="CONSOLE_LOG_PATTERN" value="[%highlight(%-5level)] [%thread] - %msg%n"/>
<property name="CONSOLE_DETAIL_LOG_PATTERN" value="[%highlight(%-5level)] [%thread] %logger{36} - %msg%n%caller{1}%n"/>
<property name="FILE_LOG_PATTERN" value="[%date{ISO8601}] [%-5level] [%thread] - %msg%n"/>
<property name="FILE_DETAIL_LOG_PATTERN" value="[%date{ISO8601}] [%-5level] [%thread] %logger{36} - %msg%n%caller{1}%n"/>

<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>${LOG_FILE_PATH:-log/application-${environment}.log}</file>
<append>true</append>
<encoder>
<pattern>${FILE_LOG_PATTERN}</pattern>
<charset>utf-8</charset>
</encoder>
</appender>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
<charset>utf-8</charset>
</encoder>
</appender>

<root level="INFO">
<appender-ref ref="CONSOLE"/>
</root>
</configuration>
6 changes: 4 additions & 2 deletions src/main/scala/com/github/qubo/seed/Boot.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import akka.util.Timeout
import com.github.qubo.seed.routes.ApiRouterActor
import com.github.qubo.seed.utils.Config
import Config.App
import org.slf4j.LoggerFactory
import spray.can.Http

import scala.concurrent.ExecutionContext
import scala.concurrent.duration._


object Boot extends App {
protected val logger = LoggerFactory.getLogger(getClass)

// we need an ActorSystem to host our application in
implicit val system = ActorSystem(App.systemName)
Expand All @@ -28,9 +30,9 @@ object Boot extends App {
.mapTo[Http.Event]
.map {
case Http.Bound(address) =>
println(s"REST interface bound to $address")
logger.info(s"REST interface bound to $address")
case Http.CommandFailed(cmd) =>
println("REST interface could not bind to " + s"${App.interface}:${App.port}, ${cmd.failureMessage}")
logger.error(s"REST interface could not bind to ${App.interface}:${App.port}, ${cmd.failureMessage}")
system.shutdown()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import spray.http.{AllOrigins, StatusCodes}
import spray.httpx.SprayJsonSupport
import spray.json.DefaultJsonProtocol
import spray.routing.{ExceptionHandler, HttpService, Route}
import scalaz._, Scalaz._

import scala.concurrent.ExecutionContext
import scala.reflect.runtime.universe.{Type, typeOf}
Expand Down Expand Up @@ -43,7 +44,13 @@ class ApiRouterActor

private def swaggerRoute: Route =
(path("swagger.json") & get & complete) {
new SwaggerDefinition(SwaggerDefinitionConfig(types = apiTypes)).prettyJson
new SwaggerDefinition(
SwaggerDefinitionConfig(
types = apiTypes,
host = Config.App.interface,
port = Config.App.port.some
)
).prettyJson
}

private def swaggerUiRoute: Route =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.github.qubo.seed.swagger

import java.util

import com.github.qubo.seed.utils.ObjectHelper._
import io.swagger.jaxrs.Reader
import io.swagger.jaxrs.config.ReaderConfig
import io.swagger.models.Swagger
Expand All @@ -27,6 +28,7 @@ class SwaggerDefinition(config: SwaggerDefinitionConfig) {
lazy val swagger: Swagger = reader.read(
config.types
.map(getClassNameForType)
.tap(name => logger.info("loading class to swagger: {}", name))
.map(Class.forName).toSet
)

Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/com/github/qubo/seed/utils/Config.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ object Config {
private val config = ConfigFactory.parseResources(s"$environment.conf")

object App {
val appConf = config.getConfig("app")
private val appConf = config.getConfig("app")
val systemName = appConf.getString("systemName")
val interface = appConf.getString("interface")
val port = appConf.getInt("port")
Expand Down
11 changes: 11 additions & 0 deletions src/main/scala/com/github/qubo/seed/utils/ObjectHelper.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.github.qubo.seed.utils

object ObjectHelper {
implicit class RichSeq[+A](seq: Seq[A]) {
def tap(function: A => Unit): Seq[A] =
seq.map { item =>
function(item)
item
}
}
}

0 comments on commit 095a09e

Please sign in to comment.