Hyperledger Fabric JSON响应包含反斜线 [英] Hyperledger Fabric JSON response has backslashes

查看:77
本文介绍了Hyperledger Fabric JSON响应包含反斜线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm currently testing a Hyperledger Fabric Application, but I get an unexpected JSON response. Why are there extra backslashes between every object in the response?

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
         }
      ]
   }
]

Docker日志:

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

推荐答案

记录格式

peer和orderer命令的日志记录格式是通过FABRIC_LOGGING_FORMAT环境变量控制的.可以将其设置为格式字符串,例如默认值

The logging format of the peer and orderer commands is controlled via the FABRIC_LOGGING_FORMAT environment variable. This can be set to a format string, such as the default

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

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

to print the logs in a human-readable console format. It can be also set to json to output logs in JSON format.

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

您可以更新core.yaml,也可以使用"FABRIC_LOGGING_FORMAT"在您的docker compose文件中.

You can update core.yaml or you can use "FABRIC_LOGGING_FORMAT" in your docker compose file.

以下是core.yaml的示例:

An example with core.yaml is given below:

    # 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.目录.

You can find core.yaml into "fabric-samples/config" directory.

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

如果您下载最新的面料样本,则可以在以下位置找到样本core.yaml:"fabric-samples/config".目录.

If you download latest fabric samples, you can find sample core.yaml at "fabric-samples/config" directory.

带有"FABRIC_LOGGING_FORMAT"的示例;在您的docker compose文件中给出如下: 您必须使用-FABRIC_LOGGING_FORMAT = json""

An example with "FABRIC_LOGGING_FORMAT" in your docker compose file is given below: You have to edit the environment of cli container with "- FABRIC_LOGGING_FORMAT=json"

  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

这篇关于Hyperledger Fabric JSON响应包含反斜线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆