การตอบสนอง 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
คำตอบ
รูปแบบการบันทึก
รูปแบบการบันทึกของคำสั่งเพียร์และออร์เดอร์ถูกควบคุมผ่านตัวแปรสภาวะแวดล้อม 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 หรือใช้ "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
คุณสามารถค้นหา core.yaml ในไดเร็กทอรี "fabric-samples / config"
ลิงค์: https://github.com/hyperledger/fabric/blob/master/sampleconfig/core.yaml
หากคุณดาวน์โหลดตัวอย่างผ้าล่าสุดคุณสามารถค้นหา core.yaml ตัวอย่างได้ที่ไดเร็กทอรี "fabric-samples / config"
ตัวอย่างที่มี "FABRIC_LOGGING_FORMAT" ในไฟล์นักเทียบท่าของคุณมีดังต่อไปนี้: คุณต้องแก้ไขสภาพแวดล้อมของ cli container ด้วย "- 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/[email protected]/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