首页 > 解决方案 > Hyperledger Fabric JSON 响应有反斜杠

问题描述

我目前正在测试一个 Hyperledger Fabric 应用程序,但我收到了意外的 JSON 响应。为什么响应中的每个对象之间都有额外的反斜杠?

result, err := json.Marshal(history)
logger.Debug(string(result))
if err != nil {
    message := fmt.Sprintf("unable to marshal the result: %s", err.Error())
    logger.Error(message)
    return shim.Error(message)
}

logger.Info("SimpleChaincode.getHistory exited successfully")
return shim.Success(result)

实际 CLI 输出:

Chaincode invoke successful. result: status:200 payload:"[{\"type\":\"history\",\"key\":\"key\",\"values\":[{\"tx_id\":\"723a398362282d92f7b05b821fc8f835736b6068e5d1b72d105fc86d6e57d64e\",\"value\":\"initial_value\",\"is_delete\":false}]}]" 

预期的 CLI 结果:

Chaincode invoke successful. 
result: status:200 
payload:
[
   {
      "type":"history",
      "key":"key",
      "values":[
         {
            "tx_id":"723a398362282d92f7b05b821fc8f835736b6068e5d1b72d105fc86d6e57d64e",
            "value":"initial_value",
            "is_delete":false
         }
      ]
   }
]

码头工人日志:

2020-08-19 14:40:18.823 UTC [SimpleChaincode] Debug -> DEBU 015 [{"type":"history","key":"key","values":[{"tx_id":"723a398362282d92f7b05b821fc8f835736b6068e5d1b72d105fc86d6e57d64e","value":"initial_value","is_delete":false}]}]
2020-08-19 14:40:18.823 UTC [SimpleChaincode] Info -> INFO 016 SimpleChaincode.getHistory exited successfully

标签: jsongohyperledger-fabrichyperledger-chaincode

解决方案


记录格式

peer 和 orderer 命令的日志格式是通过 FABRIC_LOGGING_FORMAT 环境变量控制的。这个可以设置成格式字符串,比如默认

"%{color}%{time:2006-01-02 15:04:05.000 MST} [%{module}] %{shortfunc} -> %{level:.4s} %{id:03x}%{color:reset} %{message}"

以人类可读的控制台格式打印日志。也可以设置为 json 以 JSON 格式输出日志。

链接:https ://hyperledger-fabric.readthedocs.io/en/release-2.2/logging-control.html#logging-format

您可以更新 core.yaml,也可以在 docker compose 文件中使用“FABRIC_LOGGING_FORMAT”。

下面给出了 core.yaml 的示例:

    # Logging section for the chaincode container
logging:
  # Default level for all loggers within the chaincode container
  level:  info
  # Override default level for the 'shim' logger
  shim:   warning
  # Format for the chaincode container logs
  format: json

您可以在“fabric-samples/config”目录中找到 core.yaml。

链接:https ://github.com/hyperledger/fabric/blob/master/sampleconfig/core.yaml

如果您下载最新的 Fabric 示例,您可以在“fabric-samples/config”目录中找到示例 core.yaml。

下面给出了 docker compose 文件中带有“FABRIC_LOGGING_FORMAT”的示例:您必须使用“-FABRIC_LOGGING_FORMAT=json”编辑 cli 容器的环境

  cli:
container_name: cli
image: hyperledger/fabric-tools:$IMAGE_TAG
tty: true
stdin_open: true
environment:
  - GOPATH=/opt/gopath
  - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
  #- FABRIC_LOGGING_SPEC=DEBUG
  - FABRIC_LOGGING_FORMAT=json
  - FABRIC_LOGGING_SPEC=INFO
  - CORE_PEER_ID=cli
  - CORE_PEER_ADDRESS=peer0.org1.example.com:7051
  - CORE_PEER_LOCALMSPID=Org1MSP
  - CORE_PEER_TLS_ENABLED=true
  - CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.crt
  - CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.key
  - CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
  - CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
command: /bin/bash
volumes:
    - /var/run/:/host/var/run/
    - ./../chaincode/:/opt/gopath/src/github.com/hyperledger/fabric-samples/chaincode
    - ./crypto-config:/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/
    - ./scripts:/opt/gopath/src/github.com/hyperledger/fabric/peer/scripts/
    - ./channel-artifacts:/opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts
depends_on:
  - orderer.example.com
  - peer0.org1.example.com
  - peer1.org1.example.com
  - peer0.org2.example.com
  - peer1.org2.example.com
networks:
  - byfn

推荐阅读