Merge pull request #1870 from akka/wip-3609-log-hostport-patriknw

+act #3609 Include host and port in log source
This commit is contained in:
Patrik Nordwall 2013-12-13 00:22:28 -08:00
commit 254e77a8da
3 changed files with 80 additions and 8 deletions

View file

@ -10,6 +10,7 @@ import scala.annotation.tailrec
import scala.reflect.BeanProperty
import scala.util.control.NoStackTrace
import java.util.regex.Pattern
import akka.event.LoggingAdapter
/**
* INTERNAL API
@ -279,7 +280,15 @@ object Status {
* }}}
*/
trait ActorLogging { this: Actor
val log = akka.event.Logging(context.system, this)
private var _log: LoggingAdapter = _
def log: LoggingAdapter = {
// only used in Actor, i.e. thread safe
if (_log eq null)
_log = akka.event.Logging(context.system, this)
_log
}
}
/**

View file

@ -4,7 +4,6 @@
package akka.event
import language.existentials
import akka.actor._
import akka.{ ConfigurationException, AkkaException }
import akka.actor.ActorSystem.Settings
@ -16,6 +15,7 @@ import scala.collection.immutable
import scala.concurrent.duration._
import scala.concurrent.Await
import scala.util.control.NoStackTrace
import scala.util.control.NonFatal
/**
* This trait brings log level handling to the EventStream: it reads the log
@ -270,11 +270,18 @@ object LogSource {
}
implicit val fromActor: LogSource[Actor] = new LogSource[Actor] {
def genString(a: Actor) = a.self.path.toString
def genString(a: Actor) = fromActorRef.genString(a.self)
override def genString(a: Actor, system: ActorSystem) = fromActorRef.genString(a.self, system)
}
implicit val fromActorRef: LogSource[ActorRef] = new LogSource[ActorRef] {
def genString(a: ActorRef) = a.path.toString
override def genString(a: ActorRef, system: ActorSystem) = try {
a.path.toStringWithAddress(system.asInstanceOf[ExtendedActorSystem].provider.getDefaultAddress)
} catch {
// it can fail if the ActorSystem (remoting) is not completely started yet
case NonFatal(_) a.path.toString
}
}
// this one unfortunately does not work as implicit, because existential types have some weird behavior
@ -710,11 +717,11 @@ object Logging {
private val date = new Date()
private val dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss.SSS")
private val errorFormat = "[ERROR] [%s] [%s] [%s] %s%s".intern
private val errorFormatWithoutCause = "[ERROR] [%s] [%s] [%s] %s".intern
private val warningFormat = "[WARN] [%s] [%s] [%s] %s".intern
private val infoFormat = "[INFO] [%s] [%s] [%s] %s".intern
private val debugFormat = "[DEBUG] [%s] [%s] [%s] %s".intern
private val errorFormat = "[ERROR] [%s] [%s] [%s] %s%s"
private val errorFormatWithoutCause = "[ERROR] [%s] [%s] [%s] %s"
private val warningFormat = "[WARN] [%s] [%s] [%s] %s"
private val infoFormat = "[INFO] [%s] [%s] [%s] %s"
private val debugFormat = "[DEBUG] [%s] [%s] [%s] %s"
def timestamp(event: LogEvent): String = synchronized {
date.setTime(event.timestamp)

View file

@ -0,0 +1,56 @@
/**
* Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
*/
package akka.remote
import scala.concurrent.duration._
import akka.testkit.AkkaSpec
import akka.actor.Actor
import akka.actor.ActorLogging
import akka.actor.Props
import akka.event.Logging
import akka.testkit.ImplicitSender
import akka.testkit.TestProbe
import akka.actor.Deploy
import akka.event.Logging.Info
import akka.actor.ExtendedActorSystem
object LogSourceSpec {
class Reporter extends Actor with ActorLogging {
def receive = {
case s: String
log.info(s)
}
}
}
@org.junit.runner.RunWith(classOf[org.scalatest.junit.JUnitRunner])
class LogSourceSpec extends AkkaSpec(
"""
akka.loglevel = INFO
akka.actor.provider = "akka.remote.RemoteActorRefProvider"
akka.remote.netty.tcp.port = 0
""") {
import LogSourceSpec._
val reporter = system.actorOf(Props[Reporter], "reporter")
val logProbe = TestProbe()
system.eventStream.subscribe(system.actorOf(Props(new Actor {
def receive = {
case i @ Info(_, _, msg: String) if msg contains "hello" logProbe.ref ! i
case _
}
}).withDeploy(Deploy.local), "logSniffer"), classOf[Logging.Info])
"Log events" must {
"must include host and port for local LogSource" in {
reporter ! "hello"
val info = logProbe.expectMsgType[Info]
info.message must be("hello")
val defaultAddress = system.asInstanceOf[ExtendedActorSystem].provider.getDefaultAddress
info.logSource must include(defaultAddress.toString)
}
}
}