2017-02-20 28 views
0

我有一個應用程序在能夠創建actor系統將使用的配置之前需要調用某個外部系統(以解密敏感信息以使其保持簡短)。Akka(JVM)在ActorSystem之前初始化記錄器

是否可以在我的流程開始時初始化記錄器,以便在與外部系統的通信出錯時監控/調查工具可以訪問日誌?

// the following call will modify the config from a secret id read in the conf 
// and write the value "redis.password" after getting the value from an Azure Keyvault 
val updatedConf = KeyVaultHelper.decryptFromKeyVault(ConfigFactory.load("application"), 
    Map(
    "azure.keyvault.redis.password.secret-id", 
    "redis.password" 
)) 

// now we initialize the actor system with the updated conf 
// and the logging system will be initialized 
val system = ActorSystem("ClusterSystem", updatedConf) 
+0

你可以發佈一些代碼示例?真的很難說,因爲我認爲使用早期初始化器是最悲觀的。 –

+0

我剛剛做了,不知道這是否有幫助? – CanardMoussant

+0

因此,您需要在創建系統之前使用記錄器。我不確定我是否正確理解你的用例,但我會在下面粘貼一些代碼。 –

回答

1

要做到這一點,你可以使用akka-slf4j庫及其推薦的日誌記錄的後端logback一起。有兩件事情需要配置。

logback.xml文件與構成記錄程序:

<!-- src/main/resources/logback.xml --> 
<configuration> 
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> 
    <encoder> 
     <pattern>%date{ISO8601} %-5level %logger{36} %X{sourceThread} - %msg%n</pattern> 
    </encoder> 
    </appender> 
    <root level="debug"> 
    <appender-ref ref="STDOUT" /> 
    </root> 
</configuration> 

的SLF4J記錄器必須從類型安全配置以及被配置:

# src/main/resources/application.conf 
akka { 
    loggers = ["akka.event.slf4j.Slf4jLogger"] 
    loglevel = "DEBUG" 
    logging-filter = "akka.event.slf4j.Slf4jLoggingFilter" 
} 

請參閱吹代碼示例測試配置。

object LoggingTest extends App { 
    // Do all decryption logging here 
    val logger = LoggerFactory.getLogger(getClass) 
    logger.debug("Starting application. Decrypting info from config") 

    // Now you boot the system which will use the same logging config 
    val system = ActorSystem() 
    val actor = system.actorOf(Props[Echo]) 

    actor ! "hello" 
} 

class Echo extends Actor { 
    override def receive: Receive = { 
    case message => 
     context.system.log.debug(s"echo $message") 
    } 
} 

我用以下akka-actor & akka-slf4j 2.4.17和1.1.3 ch.qos.logback

讓我知道你是否還有其他問題。

+0

對不起,沒有透明。我使用logback作爲我的演員系統的記錄器,並且如果我在演員系統存在之前嘗試登錄,我想與我分享它的配置。目前,記錄器由Akka自己初始化,我不確定如何初始化它。我想這實際上是非常簡單的,我想要做的只是確保我創建的logback記錄器會使用相同的配置。 – CanardMoussant

+0

這是可能的如果你使用akka-slf4j。午餐後我會編輯答案:) –

+0

是的,我願意,謝謝! – CanardMoussant

相關問題