简体   繁体   English

在 Hyperledger Fabric 智能合约中,我如何打开日志记录?

[英]In a Hyperledger Fabric Smart Contract how do I turn on logging?

I've written a Hyperledger Fabric Smart Contracts in Java and NodeJS.我在 Java 和 NodeJS 中编写了 Hyperledger Fabric 智能合约。 How do I turn on different levels of logging?如何打开不同级别的日志记录?

The Fabric Smart Contract classes themselves log at the INFO level, how do I change that when the contract is deployed and running? Fabric 智能合约类本身记录在 INFO 级别,在部署和运行合约时如何更改它?

EDIT: Included Node.js (for JavaScript and Typescript) as well as Java.编辑:包括 Node.js(用于 JavaScript 和打字稿)以及 Java。

The solution to this is to implement a simple 'helper' transaction.解决方案是实现一个简单的“助手”事务。 As an example, you could create another class as follows.例如,您可以创建另一个 class,如下所示。

In Java:在 Java 中:

@Contract(name = "ContractSupport")
public class ContractSupport implements ContractInterface {

    /**
     * Required Default Constructor.
     */
    public ContractSupport() {

    }

    /**
     * Sets the log level.
     * 
     * The setLogLevel method has the required parsing to manage the levels.
     *
     * @param ctx   Transactional Context
     * @param level string id
     */
    @Transaction(intent = TYPE.EVALUATE)
    public void setLogLevel(final Context ctx, final String level) {
        Logging.setLogLevel(level);

    }
}

Note that this has the @Contract() annotation so it's callable from the Client SDKs as any other transaction function.请注意,这有@Contract()注释,因此它可以从客户端 SDK 调用,就像任何其他事务 function 一样。 Remember to make sure the name in the annotation is different from your other contracts, and don't use the @Default() annotation - that should be used by your own code.请记住确保注释中的名称与您的其他合同不同,并且不要使用@Default()注释 - 您自己的代码应该使用该注释。

The strings that are accepted by the Logging.setLogLevel method and how they map to java.util.logging levels are Logging.setLogLevel方法接受的字符串以及它们如何从 map 到java.util.logging级别是

  • CRITICAL, ERROR maps to Level.SEVERE CRITICAL, ERROR映射到Level.SEVERE
  • WARNING maps to Level.WARNING WARNING映射到Level.WARNING
  • INFO maps to Level.INFO INFO映射到Level.INFO
  • NOTICE maps to Level.CONFIG NOTICE映射到Level.CONFIG
  • DEBUG maps to Level.FINEST DEBUG映射到Level.FINEST

Anything else is mapped to INFO.其他任何内容都映射到 INFO。

All the loggers that start org.hyperledger will be updated.所有启动org.hyperledger的记录器都会被更新。

In Node.js (this example is in Typescript)在 Node.js 中(此示例在 Typescript 中)

@Info({ title: 'ContractSupport', description: 'Utilties to support the contract' })
export class ContractSupport extends Contract {

    @Transaction(false)
    public async setLogLevel(ctx: Context, loglevel: string): Promise<void> {
        const logger = ctx.logger.setLevel(loglevel);
    }

}

This is using the @Info annotation to specify the contract title and the super class of Contract is used.这是使用@Info注释来指定合约标题,并使用Contract的超级 class。 Remember to add this contract class to the list of exports in the index.js/index.ts otherwise it won't be picked up.请记住将此合约 class 添加到 index.js/index.ts 中的导出列表中,否则将不会被拾取。

The mapping here maps to the levels used by Winston这里的映射映射到 Winston 使用的关卡

  • CRITICIAL maps to fatal关键映射到致命
  • ERROR maps to to error ERROR映射到错误
  • WARNING maps to to warn WARNING映射到警告
  • DEBUG maps to to debug DEBUG映射到调试
  • INFO maps to to info INFO映射到信息

INFO is the default if something else is supplied.如果提供了其他内容,则 INFO 是默认值。

Do I submit or evaluate?我提交或评估? In the example, I've marked this as evaluate.在示例中,我将此标记为评估。 Remember this annotation is a recommendation, it is ultimately how the client application submits the transaction that determines if it sent for ordering.请记住,此注释是一个建议,最终由客户端应用程序提交事务的方式决定它是否发送以进行排序。

As this is not affecting the ledger, but the actually running chaincode you can use either evaluate or submit.因为这不会影响分类帐,但您可以使用评估或提交实际运行的链代码。 The important thing is to think about which chaincode instances will run the code.重要的是要考虑哪些链码实例将运行代码。

Evaluate will send to one peer and therefore one chaincode will enable logging.评估将发送给一个对等点,因此一个链码将启用日志记录。 Submit will send to potentially other peers in other organizations.提交将发送给其他组织中的潜在其他同行。 Do you want to enable additional logging across all of those?您想在所有这些中启用额外的日志记录吗?

It's probably best to use evaluate, and then return the level to INFO when you've got the diagnostics you need.最好使用评估,然后在获得所需诊断后将级别返回到 INFO。

Why do I have to code this myself?为什么我必须自己编写代码? Valid question, there are 2 reasons.有效的问题,有两个原因。

  1. You may wish to map your own logging level changes here for your own Smart Contract code.您可能希望在此处为您自己的智能合约代码更改您自己的日志记录级别。
  2. For the other transaction functions you're likely to want to implement some validation of who is submitting the logging change.对于其他事务功能,您可能希望对谁提交日志更改进行一些验证。 You might even want to update a specific key in the ledger to record the change.您甚至可能想要更新分类帐中的特定键以记录更改。

Where do the logs go?日志 go 在哪里?

They go to the stdout of the chaincode process, which will usually be captured by the environment, eg Kubernetes.它们 go 到链码过程的标准输出,通常会被环境捕获,例如 Kubernetes。

When developing chaincode, dev-mode is recommended.开发链码时,推荐使用 dev-mode。 You can check this from the link below.您可以从下面的链接中查看。

hyperledger/fabric/peer-devmode hyperledger/fabric/peer-devmode

At this time, you can easily modify the log level by changing the CORE_CHAINCODE_LOGLEVEL option when starting chaincode.此时,您可以在启动链码时通过更改CORE_CHAINCODE_LOGLEVEL选项轻松修改日志级别。

cd examples/chaincode/go/chaincode_example02
go build
CORE_CHAINCODE_LOGLEVEL=debug CORE_PEER_ADDRESS=127.0.0.1:7052 CORE_CHAINCODE_ID_NAME=mycc:0 ./chaincode_example02

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM