corda 合同的应用程序日志记录

Mah*_*ind 1 corda

能否请您给我一些有关如何在corda 合同中添加应用程序日志的指示。

在corda 文档中有一个关于日志的页面,但它没有提供一个正确的例子。 https://docs.corda.net/node-administration.html?highlight=logs

您还可以帮助了解如何在 shell 控制台上打印调试语句。这些信息将非常有用。

谢谢你 。

Joe*_*oel 5

您可以按如下方式在流中执行日志记录:

@InitiatingFlow
@StartableByRPC
public static class Initiator extends FlowLogic<Void> {
    @Suspendable
    @Override public Void call() {
        getLogger().info("Logging within a flow.");

        return null;
    }
}
Run Code Online (Sandbox Code Playgroud)

在 Kotlin 中,这变成了:

@InitiatingFlow
@StartableByRPC
class Initiator : FlowLogic<Unit>() {
    @Suspendable
    override fun call() {
        logger.info("Logging within a flow.")
    }
}
Run Code Online (Sandbox Code Playgroud)

合约内的日志记录只能用于调试目的。在生产中,合约验证将在禁止日志记录的 SGX 飞地内进行。您在合同中执行日志记录如下:

public class TemplateContract implements Contract {    
    @Override
    public void verify(LedgerTransaction tx) {
        LoggerFactory.getLogger(TemplateContract.class).info("Logging within a contract.");
    }
}
Run Code Online (Sandbox Code Playgroud)

在 Kotlin 中,您还可以使用loggerFor扩展函数在合约中执行日志记录:

open class TemplateContract : Contract {
    override fun verify(tx: LedgerTransaction) {
        loggerFor<TemplateContract>().info("Logging within a contract.")
    }
}
Run Code Online (Sandbox Code Playgroud)

您无法登录到节点外壳,因为外壳实际上是一个通过 RPC 与节点通信的远程进程。