You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iota.apache.org by to...@apache.org on 2016/07/21 23:37:57 UTC

[06/10] incubator-iota git commit: Event capturing for logs logged outside an akka actor

Event capturing for logs logged outside an akka actor


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

Branch: refs/heads/master
Commit: e0cad7914b5c9e97cf1baff86279298ed64db2e1
Parents: 90ffcf3
Author: Barbara Gomes <ba...@gmail.com>
Authored: Wed Jul 20 18:03:44 2016 -0700
Committer: Barbara Gomes <ba...@gmail.com>
Committed: Wed Jul 20 18:03:44 2016 -0700

----------------------------------------------------------------------
 fey-core/NOTICE                                 | 34 +++++++++
 fey-core/src/test/resources/logback-test.xml    | 25 +++++++
 .../test/resources/test-fey-configuration.conf  |  2 +-
 .../org/apache/iota/fey/InMemoryAppender.scala  | 67 ++++++++++++++++++
 .../scala/org/apache/iota/fey/LoggingTest.scala | 74 ++++++++++++++++++++
 5 files changed, 201 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-iota/blob/e0cad791/fey-core/NOTICE
----------------------------------------------------------------------
diff --git a/fey-core/NOTICE b/fey-core/NOTICE
new file mode 100644
index 0000000..1fe1627
--- /dev/null
+++ b/fey-core/NOTICE
@@ -0,0 +1,34 @@
+========================================================================
+NOTICE files
+========================================================================
+
+The following NOTICEs are pertain to software distributed with this project.
+
+
+// ------------------------------------------------------------------
+// NOTICE file corresponding to the section 4d of The Apache License,
+// Version 2.0, in this case for
+// ------------------------------------------------------------------
+
+
+                            Scala-tests-Utils
+                            =================
+
+Source code: https://github.com/Dwolla/scala-test-utils
+
+The MIT License (MIT)
+Copyright � 2016 Dwolla
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+and associated documentation files (the "Software"), to deal in the Software without restriction,
+including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
+and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
+OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-iota/blob/e0cad791/fey-core/src/test/resources/logback-test.xml
----------------------------------------------------------------------
diff --git a/fey-core/src/test/resources/logback-test.xml b/fey-core/src/test/resources/logback-test.xml
new file mode 100644
index 0000000..73cb12f
--- /dev/null
+++ b/fey-core/src/test/resources/logback-test.xml
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+
+<!--Licensed to the Apache Software Foundation (ASF) under one or more -->
+<!--contributor license agreements.  See the NOTICE file distributed with -->
+<!--this work for additional information regarding copyright ownership. -->
+<!--The ASF licenses this file to You under the Apache License, Version 2.0 -->
+<!--(the "License"); you may not use this file except in compliance with -->
+<!--the License.  You may obtain a copy of the License at -->
+<!-- -->
+<!--   http://www.apache.org/licenses/LICENSE-2.0 -->
+<!-- -->
+<!--Unless required by applicable law or agreed to in writing, software -->
+<!--distributed under the License is distributed on an "AS IS" BASIS, -->
+<!--WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -->
+<!--See the License for the specific language governing permissions and -->
+<!--limitations under the License. -->
+
+<configuration>
+    <appender name="inMemory" class="org.apache.iota.fey.InMemoryAppender"/>
+
+    <root level="info">
+        <appender-ref ref="inMemory"/>
+    </root>
+</configuration>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-iota/blob/e0cad791/fey-core/src/test/resources/test-fey-configuration.conf
----------------------------------------------------------------------
diff --git a/fey-core/src/test/resources/test-fey-configuration.conf b/fey-core/src/test/resources/test-fey-configuration.conf
index d575eda..cf5e47b 100644
--- a/fey-core/src/test/resources/test-fey-configuration.conf
+++ b/fey-core/src/test/resources/test-fey-configuration.conf
@@ -28,7 +28,7 @@ fey-global-configuration {
     downloaded-repository = "/tmp/fey/test/jars/dynamic"
     force-pull = false
   }
-  log-level = "ERROR"
+  log-level = "DEBUG"
   log-appender = "STDOUT"
   auto-scale{
     messages-per-resize = 500

http://git-wip-us.apache.org/repos/asf/incubator-iota/blob/e0cad791/fey-core/src/test/scala/org/apache/iota/fey/InMemoryAppender.scala
----------------------------------------------------------------------
diff --git a/fey-core/src/test/scala/org/apache/iota/fey/InMemoryAppender.scala b/fey-core/src/test/scala/org/apache/iota/fey/InMemoryAppender.scala
new file mode 100644
index 0000000..6733210
--- /dev/null
+++ b/fey-core/src/test/scala/org/apache/iota/fey/InMemoryAppender.scala
@@ -0,0 +1,67 @@
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*
+ * Source: https://github.com/Dwolla/scala-test-utils
+ * The MIT License (MIT)
+ * Copyright � 2016 Dwolla
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
+ * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+package org.apache.iota.fey
+
+import ch.qos.logback.classic.Level
+import ch.qos.logback.classic.spi.ILoggingEvent
+import ch.qos.logback.core.AppenderBase
+
+import scala.collection.mutable
+import scala.collection.mutable.ListBuffer
+
+class InMemoryAppender extends AppenderBase[ILoggingEvent] {
+  private val events: mutable.Map[Thread, ListBuffer[ILoggingEvent]] = new mutable.HashMap[Thread, ListBuffer[ILoggingEvent]]
+
+  override def append(e: ILoggingEvent): Unit =
+    events.getOrElseUpdate(Thread.currentThread(), new ListBuffer[ILoggingEvent]).append(e)
+
+  def reset(): Unit = events.get(Thread.currentThread()).foreach(l => l.clear())
+
+  def containsAtLevel(msg: String, logLevel: Level): Boolean =
+    eventsForCurrentThread.filter(e => e.getLevel == logLevel)
+      .map(e => e.getFormattedMessage).contains(msg)
+
+  def contains(msg: String): Boolean =
+    eventsForCurrentThread.map(e => e.getFormattedMessage).contains(msg)
+
+  def dumpLogs(): Unit =
+    eventsForCurrentThread.foreach(e => println(s"${Thread.currentThread().getName}: ${e.getFormattedMessage}"))
+
+  private def eventsForCurrentThread = events.getOrElse(Thread.currentThread(), new ListBuffer[ILoggingEvent])
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-iota/blob/e0cad791/fey-core/src/test/scala/org/apache/iota/fey/LoggingTest.scala
----------------------------------------------------------------------
diff --git a/fey-core/src/test/scala/org/apache/iota/fey/LoggingTest.scala b/fey-core/src/test/scala/org/apache/iota/fey/LoggingTest.scala
new file mode 100644
index 0000000..ab03973
--- /dev/null
+++ b/fey-core/src/test/scala/org/apache/iota/fey/LoggingTest.scala
@@ -0,0 +1,74 @@
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*
+ * Source: https://github.com/Dwolla/scala-test-utils
+ * The MIT License (MIT)
+ * Copyright � 2016 Dwolla
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
+ * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+package org.apache.iota.fey
+
+import ch.qos.logback.classic.{Level, Logger}
+import org.scalatest.matchers.{MatchResult, Matcher}
+import org.slf4j.helpers.SubstituteLogger
+import org.slf4j.{LoggerFactory, Logger => SLF4JLogger}
+
+trait LoggingTest{
+
+  protected val logAppenderName = "inMemory"
+  private val appender = findInMemoryAppender(logAppenderName)
+
+  def beLoggedAt(logLevel: Level): Matcher[String] = new Matcher[String] {
+    def apply(left: String) = {
+      val containsAtLevel = appender.containsAtLevel(left, logLevel)
+      MatchResult(containsAtLevel,
+        s" '$left' was not found at log level",
+        s" '$left' was found at log level")
+    }
+  }
+
+  def resetCapturedLogs(): Unit = appender.reset()
+
+  def dumpCapturedLogsToSysOut(): Unit = appender.dumpLogs()
+
+  private def findInMemoryAppender(s: String): InMemoryAppender = {
+    LoggerFactory.getLogger(SLF4JLogger.ROOT_LOGGER_NAME) match {
+      case logger: Logger => logger.getAppender(s) match {
+        case inMemoryAppender: InMemoryAppender => inMemoryAppender
+        case _ => throw new IllegalStateException(s"Is the InMemoryAppender registered with logback in its configuration file with the name $s?")
+      }
+      case sub: SubstituteLogger => throw new IllegalStateException("SLF4J is probably still initializing. Is LoggingTest part of the outermost class wrapping your tests?")
+      case _ => throw new IllegalStateException("Are you using LogBack logging?")
+    }
+  }
+}
\ No newline at end of file