2014-09-03 24 views
2

我已經編寫了一個自定義記錄器將日誌消息傳遞到遠程系統。我有第二個記錄器,使用作爲後端打印到控制檯和日誌文件。Akka自定義記錄器不會收到一些消息

我注意到的奇怪之處是有些消息沒有被我的自定義記錄器接收,但slf4j只收到它們。

例如通過記錄到日誌中的監督戰略的一部分,一個失敗的兒童演員中產生的錯誤信息:

[DEBUG][akka://Test/system/remote-deployment-watcher]: started ([email protected]) 
[DEBUG][akka://Test/system]: now supervising Actor[akka://Test/system/deadLetterListener#-286980524] 
started ([email protected]) 
now supervising Actor[akka://Test/system/deadLetterListener#-286980524] 
[DEBUG][akka://Test/system/deadLetterListener]: started ([email protected]) 
started ([email protected]) 
started ([email protected]) 
[DEBUG][akka://Test/system/remote-watcher]: started ([email protected]) 
now supervising TestActor[akka://Test/user/$$a] 
[DEBUG][akka://Test/user]: now supervising TestActor[akka://Test/user/$$a] 
started ([email protected]) 
now supervising Actor[akka://Test/user/$$a/refresher#-138594901] 
[DEBUG][akka://Test/user/$$a]: started ([email protected]) 
[DEBUG][akka://Test/user/$$a]: now supervising Actor[akka://Test/user/$$a/refresher#-138594901] 
akka.tcp://[email protected]:2552/user/$$a: im an error 
[ERROR][akka.tcp://[email protected]:2552/user/$$a]: im an error 
stopped 
[ERROR][akka://Test/user/$$a/refresher]: exception during creation 
akka.actor.ActorInitializationException: exception during creation 
    at akka.actor.ActorInitializationException$.apply(Actor.scala:164) ~[akka-actor_2.10-2.3.5.jar:na] 
    ... 

出於調試的目的我的自定義記錄打印接收到的信息到控制檯。帶有前綴日誌級別的消息由slf4j生成,而包含消息的之間的消息由我的自定義記錄器生成。

正如你可以看到我的自定義記錄器接收調試消息就好了,甚至錯誤消息「IM錯誤」我手動生成收到罰款,但沒有異常生成的自動消息。

我是否必須訂閱記錄器才能接收其他事件?或者這裏有什麼問題?

我的記錄:application.conf的

class MonitoringLogger extends Actor { 

    val mon = context.actorOf(Props[MonitorEndpoint], "MonitorEndpoint") 

    def receive = { 
    case InitializeLogger(bus) => sender ! LoggerInitialized 
    case Error(cause, logSource, logClass, message: String) => 
     println(message) 
     mon ! format(logSource, message, Console.RED) 
    case Warning(logSource, logClass, message: String) => mon ! format(logSource, message, Console.YELLOW) 
    case Info(logSource, logClass, message: String) => mon ! format(logSource, message, Console.GREEN) 
    case Debug(logSource, logClass, message: String) => 
     println(message) 
     mon ! format(logSource, message, Console.GREEN) 
    } 

    def format(src: String, msg: String, code: String) = s"${Console.BLUE}R> $code[${src.split('/').last}] ${Console.RESET}$msg" 
} 

Interresting部分:

akka { 
    loggers = ["akka.event.slf4j.Slf4jLogger", "package.MonitoringLogger"] 
    loglevel = "DEBUG" 
    log-dead-letters-during-shutdown = off 
    log-dead-letters = 10 
    actor.debug { 
    lifecycle = on 
    } 
} 
+0

您可以分享自定義記錄器的代碼嗎? – cmbaxter 2014-09-03 11:07:17

+0

@cmbaxter我當然可以。 – mgttlinger 2014-09-03 11:15:23

+1

我的猜測是你有一個未處理的案例。嘗試添加一個'case other =>',然後打印另一個'other'以查看是否有另一種消息類型。 – cmbaxter 2014-09-03 11:33:17

回答

1

cmbaxter已導致我到我的錯誤。我比賽反對case class Error被定義爲:

case class Error(
    cause: Throwable, 
    logSource: String, 
    logClass: Class[_], 
    message: Any = "") extends LogEvent 

注意,message具有類型Any但我對陣String。我自己的日誌消息得到匹配,因爲我使用了字符串消息。該異常的自動生成事件的message設置爲null,因此它不符合匹配。