logging
authorRoman Shevchenko <roman.shevchenko@jetbrains.com>
Fri, 23 Sep 2016 13:26:04 +0000 (16:26 +0300)
committerRoman Shevchenko <roman.shevchenko@jetbrains.com>
Fri, 23 Sep 2016 14:17:24 +0000 (17:17 +0300)
update-server-mock/build.gradle
update-server-mock/log4j.xml [new file with mode: 0644]
update-server-mock/src/main/java/com/intellij/updater/mock/Server.kt
update-server-mock/src/main/java/com/intellij/updater/mock/main.kt

index ef1f5e31bc8203136b42d50e0913e016eeec0b24..692861f2fc029035993793f6b9e49089e970708b 100644 (file)
@@ -10,6 +10,8 @@ apply plugin: 'kotlin'
 repositories { jcenter() }
 dependencies {
   compile "org.jetbrains.kotlin:kotlin-stdlib:1.0.+"
+  compile "org.apache.logging.log4j:log4j-api:2.+"
+  runtime "org.apache.logging.log4j:log4j-core:2.+"
   testCompile "junit:junit:4.+"
   testCompile "org.assertj:assertj-core:3.+"
 }
diff --git a/update-server-mock/log4j.xml b/update-server-mock/log4j.xml
new file mode 100644 (file)
index 0000000..2e4483a
--- /dev/null
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<Configuration>
+
+  <Appenders>
+    <Console name="STDOUT">
+      <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %logger{36} - %msg%n"/>
+    </Console>
+  </Appenders>
+
+  <Loggers>
+    <Root level="info">
+      <AppenderRef ref="STDOUT"/>
+    </Root>
+  </Loggers>
+
+</Configuration>
\ No newline at end of file
index 930efcc258923c0f9178647b516bc663cddd8cca..d2b0c1cfb236774d48d1e9d22ae06ec6a6e099e5 100644 (file)
@@ -16,6 +16,7 @@
 package com.intellij.updater.mock
 
 import com.sun.net.httpserver.HttpServer
+import org.apache.logging.log4j.LogManager
 import java.net.HttpURLConnection.*
 import java.net.InetSocketAddress
 import java.net.URI
@@ -24,6 +25,7 @@ import java.time.format.DateTimeFormatter
 
 class Server(private val port: Int, private val generator: Generator) {
   private val server = HttpServer.create()
+  private val log = LogManager.getLogger(Server::class.java)
   private val buildFormat = "([A-Z]+)-([0-9.]+)".toRegex()
   private val tsFormat = DateTimeFormatter.ofPattern("dd/MMM/yyyy:kk:mm:ss ZZ")
 
@@ -35,18 +37,23 @@ class Server(private val port: Int, private val generator: Generator) {
         process(ex.requestMethod, ex.requestURI)
       }
       catch(e: Exception) {
-        e.printStackTrace()
+        log.error(e)
         Response(HTTP_INTERNAL_ERROR, "Internal error")
       }
 
-      val contentType = if (response.type.startsWith("text/")) response.type + "; charset=utf-8" else response.type
-      ex.responseHeaders.add("Content-Type", contentType)
-      ex.sendResponseHeaders(response.code, response.bytes.size.toLong())
-      ex.responseBody.write(response.bytes)
-      ex.close()
+      try {
+        val contentType = if (response.type.startsWith("text/")) "${response.type}; charset=utf-8" else response.type
+        ex.responseHeaders.add("Content-Type", contentType)
+        ex.sendResponseHeaders(response.code, response.bytes.size.toLong())
+        ex.responseBody.write(response.bytes)
+        ex.close()
 
-      println("${ex.remoteAddress.address.hostAddress} - - [${tsFormat.format(ZonedDateTime.now())}]" +
-        " \"${ex.requestMethod} ${ex.requestURI}\" ${ex.responseCode} ${response.bytes.size}")
+        log.info("${ex.remoteAddress.address.hostAddress} - - [${tsFormat.format(ZonedDateTime.now())}]" +
+          " \"${ex.requestMethod} ${ex.requestURI}\" ${ex.responseCode} ${response.bytes.size}")
+      }
+      catch(e: Exception) {
+        log.error(e)
+      }
     }
 
     server.start()
index 4fb8d90b308480367f660ca81c3400aa70560d93..a2cc4997ebbe3d9453d461fcd72e34ea0df94c9e 100644 (file)
  */
 package com.intellij.updater.mock
 
+import org.apache.logging.log4j.LogManager
+
 fun main(args: Array<String>) {
   if (args.size != 1) {
     println("usage: java -jar update-server-mock.jar <port>")
     System.exit(1)
   }
 
-  val port = args[0].toInt()
-  val generator = Generator()
-  Server(port, generator).start()
+  val log = LogManager.getLogger(Server::class.java)
+  val port = args[0]
+  try {
+    log.info("starting the server on port '$port' ...")
+    val generator = Generator()
+    Server(port.toInt(), generator).start()
+    log.info("ready")
+  }
+  catch(e: Exception) {
+    log.error("failed to start the server", e)
+    System.exit(2)
+  }
 }
\ No newline at end of file