From 417695fafce51e90927fe00530581e47c9a4a618 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Bone=CC=81r?= Date: Tue, 22 Mar 2011 16:30:49 +0100 Subject: [PATCH] Added SLF4 module with Logging trait and Event Handler --- .../ExecutorBasedEventDrivenDispatcher.scala | 14 ++--- .../src/main/scala/akka/logging/SLF4J.scala | 56 +++++++++++++++++++ project/build/AkkaProject.scala | 47 ++++++++++------ 3 files changed, 92 insertions(+), 25 deletions(-) create mode 100644 akka-slf4j/src/main/scala/akka/logging/SLF4J.scala diff --git a/akka-actor/src/main/scala/akka/dispatch/ExecutorBasedEventDrivenDispatcher.scala b/akka-actor/src/main/scala/akka/dispatch/ExecutorBasedEventDrivenDispatcher.scala index c15a26e00d..f23b759c40 100644 --- a/akka-actor/src/main/scala/akka/dispatch/ExecutorBasedEventDrivenDispatcher.scala +++ b/akka-actor/src/main/scala/akka/dispatch/ExecutorBasedEventDrivenDispatcher.scala @@ -87,7 +87,7 @@ class ExecutorBasedEventDrivenDispatcher( def this(_name: String) = this(_name, Dispatchers.THROUGHPUT, Dispatchers.THROUGHPUT_DEADLINE_TIME_MILLIS, Dispatchers.MAILBOX_TYPE) // Needed for Java API usage - val name = "akka:event-driven:dispatcher:" + _name + val name = "akka:event-driven:dispatcher:" + _name private[akka] val threadFactory = new MonitorableThreadFactory(name) private[akka] val executorService = new AtomicReference[ExecutorService](config.createLazyExecutorService(threadFactory)) @@ -208,20 +208,18 @@ trait ExecutableMailbox extends Runnable { self: MessageQueue => else { //But otherwise, if we are throttled, we need to do some book-keeping var processedMessages = 0 val isDeadlineEnabled = dispatcher.throughputDeadlineTime > 0 - val deadlineNs = if (isDeadlineEnabled) System.nanoTime + TimeUnit.MILLISECONDS.toNanos(dispatcher.throughputDeadlineTime) else 0 + val deadlineNs = if (isDeadlineEnabled) System.nanoTime + TimeUnit.MILLISECONDS.toNanos(dispatcher.throughputDeadlineTime) + else 0 do { nextMessage.invoke - nextMessage = if (self.suspended.locked) { - null //If we are suspended, abort - } - else { //If we aren't suspended, we need to make sure we're not overstepping our boundaries + null // If we are suspended, abort + } else { // If we aren't suspended, we need to make sure we're not overstepping our boundaries processedMessages += 1 if ((processedMessages >= dispatcher.throughput) || (isDeadlineEnabled && System.nanoTime >= deadlineNs)) // If we're throttled, break out null //We reached our boundaries, abort - else - self.dequeue //Dequeue the next message + else self.dequeue //Dequeue the next message } } while (nextMessage ne null) } diff --git a/akka-slf4j/src/main/scala/akka/logging/SLF4J.scala b/akka-slf4j/src/main/scala/akka/logging/SLF4J.scala new file mode 100644 index 0000000000..98579c545e --- /dev/null +++ b/akka-slf4j/src/main/scala/akka/logging/SLF4J.scala @@ -0,0 +1,56 @@ +/** + * Copyright (C) 2009-2011 Scalable Solutions AB + */ + +package akka.logging.slf4j + +import org.slf4j.{Logger => SLFLogger, LoggerFactory => SLFLoggerFactory} + +import akka.actor._ +import Actor._ + +/** + * Base trait for all classes that wants to be able use the SLF4J logging infrastructure. + * + * @author Jonas Bonér + */ +trait Logging { + @transient lazy val log = Logger(this.getClass.getName) +} + +object Logger { + def apply(logger: String) : SLFLogger = SLFLoggerFactory getLogger logger + def apply(clazz: Class[_]): SLFLogger = apply(clazz.getName) + def root : SLFLogger = apply(SLFLogger.ROOT_LOGGER_NAME) +} + +/** + * SLF4J Event Handler. + * + * @author Jonas Bonér + */ +class Slf4jEventHandler extends Actor with Logging { + import EventHandler._ + + self.id = ID + self.dispatcher = EventHandlerDispatcher + + def receive = { + case event @ Error(cause, instance, message) => + log.error("\n\t[{}]\n\t[{}]\n\t[{}]", + Array[AnyRef](instance.getClass.getName, message, stackTraceFor(cause))) + + case event @ Warning(instance, message) => + log.warn("\n\t[{}]\n\t[{}]", instance.getClass.getName, message) + + case event @ Info(instance, message) => + log.info("\n\t[{}]\n\t[{}]", instance.getClass.getName, message) + + case event @ Debug(instance, message) => + log.debug("\n\t[{}]\n\t[{}]", instance.getClass.getName, message) + + case event => log.debug("\n\t[{}]", event.toString) + } +} + + diff --git a/project/build/AkkaProject.scala b/project/build/AkkaProject.scala index e094bb1bf0..64f54407fb 100644 --- a/project/build/AkkaProject.scala +++ b/project/build/AkkaProject.scala @@ -70,16 +70,16 @@ class AkkaParentProject(info: ProjectInfo) extends DefaultProject(info) { // ------------------------------------------------------------------------------------------------------------------- object Repositories { - lazy val LocalMavenRepo = MavenRepository("Local Maven Repo", (Path.userHome / ".m2" / "repository").asURL.toString) - lazy val AkkaRepo = MavenRepository("Akka Repository", "http://akka.io/repository") - lazy val CodehausRepo = MavenRepository("Codehaus Repo", "http://repository.codehaus.org") - lazy val GuiceyFruitRepo = MavenRepository("GuiceyFruit Repo", "http://guiceyfruit.googlecode.com/svn/repo/releases/") - lazy val JBossRepo = MavenRepository("JBoss Repo", "http://repository.jboss.org/nexus/content/groups/public/") - lazy val JavaNetRepo = MavenRepository("java.net Repo", "http://download.java.net/maven/2") - lazy val SonatypeSnapshotRepo = MavenRepository("Sonatype OSS Repo", "http://oss.sonatype.org/content/repositories/releases") - lazy val GlassfishRepo = MavenRepository("Glassfish Repo", "http://download.java.net/maven/glassfish") - lazy val ScalaToolsRelRepo = MavenRepository("Scala Tools Releases Repo", "http://scala-tools.org/repo-releases") - lazy val DatabinderRepo = MavenRepository("Databinder Repo", "http://databinder.net/repo") + lazy val LocalMavenRepo = MavenRepository("Local Maven Repo", (Path.userHome / ".m2" / "repository").asURL.toString) + lazy val AkkaRepo = MavenRepository("Akka Repository", "http://akka.io/repository") + lazy val CodehausRepo = MavenRepository("Codehaus Repo", "http://repository.codehaus.org") + lazy val GuiceyFruitRepo = MavenRepository("GuiceyFruit Repo", "http://guiceyfruit.googlecode.com/svn/repo/releases/") + lazy val JBossRepo = MavenRepository("JBoss Repo", "http://repository.jboss.org/nexus/content/groups/public/") + lazy val JavaNetRepo = MavenRepository("java.net Repo", "http://download.java.net/maven/2") + lazy val SonatypeSnapshotRepo = MavenRepository("Sonatype OSS Repo", "http://oss.sonatype.org/content/repositories/releases") + lazy val GlassfishRepo = MavenRepository("Glassfish Repo", "http://download.java.net/maven/glassfish") + lazy val ScalaToolsRelRepo = MavenRepository("Scala Tools Releases Repo", "http://scala-tools.org/repo-releases") + lazy val DatabinderRepo = MavenRepository("Databinder Repo", "http://databinder.net/repo") lazy val ScalaToolsSnapshotRepo = MavenRepository("Scala-Tools Snapshot Repo", "http://scala-tools.org/repo-snapshots") } @@ -118,6 +118,7 @@ class AkkaParentProject(info: ProjectInfo) extends DefaultProject(info) { lazy val SCALATEST_VERSION = "1.3" lazy val JETTY_VERSION = "7.1.6.v20100715" lazy val JAVAX_SERVLET_VERSION = "3.0" + lazy val SLF4J_VERSION = "1.6.0" // ------------------------------------------------------------------------------------------------------------------- // Dependencies @@ -160,6 +161,9 @@ class AkkaParentProject(info: ProjectInfo) extends DefaultProject(info) { lazy val sjson = "net.debasishg" % "sjson_2.8.1" % "0.9.1" % "compile" //ApacheV2 lazy val sjson_test = "net.debasishg" % "sjson_2.8.1" % "0.9.1" % "test" //ApacheV2 + lazy val slf4j = "org.slf4j" % "slf4j-api" % "1.6.0" + lazy val logback = "ch.qos.logback" % "logback-classic" % "0.9.24" + // Test lazy val commons_coll = "commons-collections" % "commons-collections" % "3.2.1" % "test" //ApacheV2 @@ -177,13 +181,14 @@ class AkkaParentProject(info: ProjectInfo) extends DefaultProject(info) { // ------------------------------------------------------------------------------------------------------------------- lazy val akka_actor = project("akka-actor", "akka-actor", new AkkaActorProject(_)) - lazy val akka_testkit = project("akka-testkit", "akka-testkit", new AkkaTestkitProject(_), akka_actor) lazy val akka_stm = project("akka-stm", "akka-stm", new AkkaStmProject(_), akka_actor) lazy val akka_typed_actor = project("akka-typed-actor", "akka-typed-actor", new AkkaTypedActorProject(_), akka_stm) lazy val akka_remote = project("akka-remote", "akka-remote", new AkkaRemoteProject(_), akka_typed_actor) lazy val akka_http = project("akka-http", "akka-http", new AkkaHttpProject(_), akka_remote) lazy val akka_samples = project("akka-samples", "akka-samples", new AkkaSamplesParentProject(_)) lazy val akka_sbt_plugin = project("akka-sbt-plugin", "akka-sbt-plugin", new AkkaSbtPluginProject(_)) + lazy val akka_testkit = project("akka-testkit", "akka-testkit", new AkkaTestkitProject(_), akka_actor) + lazy val akka_slf4j = project("akka-slf4j", "akka-slf4j", new AkkaSlf4jProject(_), akka_actor) // ------------------------------------------------------------------------------------------------------------------- // Miscellaneous @@ -296,12 +301,6 @@ class AkkaParentProject(info: ProjectInfo) extends DefaultProject(info) { override def testCompileAction = super.testCompileAction dependsOn (akka_testkit.compile) } - // ------------------------------------------------------------------------------------------------------------------- - // akka-testkit subproject - // ------------------------------------------------------------------------------------------------------------------- - - class AkkaTestkitProject(info: ProjectInfo) extends AkkaDefaultProject(info, distPath) - // ------------------------------------------------------------------------------------------------------------------- // akka-stm subproject // ------------------------------------------------------------------------------------------------------------------- @@ -420,6 +419,20 @@ class AkkaParentProject(info: ProjectInfo) extends DefaultProject(info) { } } + // ------------------------------------------------------------------------------------------------------------------- + // akka-testkit subproject + // ------------------------------------------------------------------------------------------------------------------- + + class AkkaTestkitProject(info: ProjectInfo) extends AkkaDefaultProject(info, distPath) + + // ------------------------------------------------------------------------------------------------------------------- + // akka-slf4j subproject + // ------------------------------------------------------------------------------------------------------------------- + + class AkkaSlf4jProject(info: ProjectInfo) extends AkkaDefaultProject(info, distPath) { + val sjson = Dependencies.slf4j + } + // ------------------------------------------------------------------------------------------------------------------- // Helpers // -------------------------------------------------------------------------------------------------------------------