我正在尝试生成一个格式良好的特定分类账分录(或分录范围(视图,其中包含所有相关值。
当我运行链代码查询时,我得到如下输出:
user@server:~/fabric-samples/test-network$ peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile $ORDERER_CA -C mychannel -n myFirstChaincode --peerAddresses localhost:7051 --tlsRootCertFiles $PEER1_TLS -c '{"function":"queryOrder","Args":["004"]}'
2020-11-08 16:01:05.166 UTC [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200 payload:"{"actualDimensions":{"DIM1":{},"DIM2":{},"DIM3":{},"DIM4":{}},"customer":"GM","dueDate":"tomorrow","manufacturer":"undefined","manufacturingData":{},"numOfDims":"5","orderStatus":"new","partID":"004","partName":"Test Part","requiredDimensions":{"DIM1":{},"DIM2":{},"DIM3":{},"DIM4":{}}}"
此输出看起来像一个长JSON字符串,其中换行符被替换。
我正在寻找一个人类可读的输出。虽然我可以阅读上面的条目,但相关的数据结构非常简单。如果我查询另一个有几个嵌套对象的条目,它很快就会变得太乱,无法轻松读取。
我曾尝试在链码查询函数中添加类似JSON.stringify(value,null,4)
的内容,但这没有帮助:
2020-11-07 23:40:41.964 UTC [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200 payload:"{n "actualDimensions": {n "DIM1": {},n "DIM2": {},n "DIM3": {},n "DIM4": {}n },n "customer": "GM",n "dueDate": "tomorrow",n "manufacturer": "undefined",n "manufacturingData": {},n "numOfDims": "5",n "orderStatus": "new",n "partID": "004",n "partName": "Test Part",n "requiredDimensions": {n "DIM1": {},n "DIM2": {},n "DIM3": {},n "DIM4": {}n }n}"
看来peer命令可能正在格式化JSON.stringfy…之后的输出
该页面建议您可以使用环境变量修改peer命令所使用的日志记录格式,尽管我在该网页之外没有找到任何示例或详细信息。当我尝试将环境变量添加到cli时:export FABRIC_LOGGING_FORMAT=json
,它没有太大帮助(它添加了更多的"/"而不是换行(:
student@hlfmc:~/fabric-samples/test-network$ peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile $ORDERER_CA -C mychannel -n basic --peerAddresses localhost:7051 --tlsRootCertFiles $PEER1_TLS --peerAddresses localhost:9051 --tlsRootCertFiles $PEER2_TLS -c '{"function":"GetAllAssets","Args":[]}'
{"level":"info","ts":1605042675.2069416,"name":"chaincodeCmd","caller":"chaincode/common.go:160","msg":"Chaincode invoke successful. result: status:200 payload:"[{\"Key\":\"asset1\",\"Record\":{\"ID\":\"asset1\",\"Color\":\"blue\",\"Size\":5,\"Owner\":\"Tomoko\",\"AppraisedValue\":300,\"docType\":\"asset\"}},{\"Key\":\"asset2\",\"Record\":{\"ID\":\"asset2\",\"Color\":\"red\",\"Size\":5,\"Owner\":\"Brad\",\"AppraisedValue\":400,\"docType\":\"asset\"}},{\"Key\":\"asset3\",\"Record\":{\"ID\":\"asset3\",\"Color\":\"green\",\"Size\":10,\"Owner\":\"Jin Soo\",\"AppraisedValue\":500,\"docType\":\"asset\"}},{\"Key\":\"asset4\",\"Record\":{\"ID\":\"asset4\",\"Color\":\"yellow\",\"Size\":10,\"Owner\":\"Max\",\"AppraisedValue\":600,\"docType\":\"asset\"}},{\"Key\":\"asset5\",\"Record\":{\"ID\":\"asset5\",\"Color\":\"black\",\"Size\":15,\"Owner\":\"Adriana\",\"AppraisedValue\":700,\"docType\":\"asset\"}},{\"Key\":\"asset6\",\"Record\":{\"ID\":\"asset6\",\"Color\":\"white\",\"Size\":15,\"Owner\":\"Michel\",\"AppraisedValue\":800,\"docType\":\"asset\"}}]" "}
(上述测试是使用股票"资产转移基本"样本链代码在单独的服务器上运行的。(注意:我还尝试用";格式:json"然后重新运行链代码(在正确地关闭所有内容并重新启动网络之后(。这提供了与以前相同的输出(与原始输出没有变化(。我还尝试在/etc/hyperledger/fabric/core.yaml中编辑对等docker容器的内部core.yaml文件,这似乎也不会影响输出。
我还尝试过从应用程序调用链代码(而不是直接从CLI调用(。生成的输出字符串不再有"/"(但仍然没有换行符/缩进(:
user@server$ node query2.js
Wallet path: /home/user/Project/application/wallet
Transaction has been evaluated, result is: {"actualDimensions":{"DIM1":{},"DIM2":{},"DIM3":{},"DIM4":{}},"customer":"GM","dueDate":"tomorrow","manufacturer":"undefined","manufacturingData":{},"numOfDims":"5","orderStatus":"new","partID":"004","partName":"Test Part","requiredDimensions":{"DIM1":{},"DIM2":{},"DIM3":{},"DIM4":{}}}
如果我尝试在输出字符串上使用JSON.stringify,我会再次得到"/":
user@server$ node query2.js
Wallet path: /home/user/Project/application/wallet
Transaction has been evaluated, result is: "{"actualDimensions":{"DIM1":{},"DIM2":{},"DIM3":{},"DIM4":{}},"customer":"GM","dueDate":"tomorrow","manufacturer":"undefined","manufacturingData":{},"numOfDims":"5","orderStatus":"new","partID":"004","partName":"Test Part","requiredDimensions":{"DIM1":{},"DIM2":{},"DIM3":{},"DIM4":{}}}"
以下是应用程序代码:(第48行是原始代码;第46-47行是第二个版本(
45 ¦ ¦ ¦ const result = await contract.evaluateTransaction('queryOrder','004');
46 ¦ ¦ ¦ const stringResult=result.toString();
47 ¦ ¦ ¦ console.log(`Transaction has been evaluated, result is: ${JSON.stringify(stringResult,null,4)}`);
48 ¦ ¦ ¦ //console.log(`Transaction has been evaluated, result is: ${result.toString()}`);
我看到这篇文章也有类似的问题,但它并没有为漂亮的格式提供任何解决方案。
目前对此有什么解决方案/建议吗?
我很乐意使用JSON格式或任何其他具有空格/换行符并使当前输出更具可读性的格式。
您可以更新core.yaml,也可以使用"FABRIC_ LOGGING_FORMAT";在你的docker compose文件中。
下面给出了一个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
你可以在";织物样品/config";目录
链接:https://github.com/hyperledger/fabric/blob/master/sampleconfig/core.yaml
如果你下载最新的织物样品,你可以在";织物样品/config";目录
例如;FABRIC_ LOGGING_FORMAT";在您的docker中,compose文件如下所示:您必须使用"-"来编辑cli容器的环境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