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
}
]
}
]
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
답변
로깅 형식
피어 및 순서 지정자 명령의 로깅 형식은 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
core.yaml은 "fabric-samples / config"디렉토리에서 찾을 수 있습니다.
링크: https://github.com/hyperledger/fabric/blob/master/sampleconfig/core.yaml
최신 패브릭 샘플을 다운로드하는 경우 "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/[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