Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add logging #97

Merged
merged 1 commit into from
May 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ rust-version = "1.56.0"
version = "0.3.0"

[dependencies]
log = "0.4.17"
uuid = { version = "0.8", features = ["v4"], optional = true }
borsh = "0.9"
serde = "1.0.127"
Expand All @@ -30,6 +31,7 @@ near-jsonrpc-primitives = "0.12.0"

[dev-dependencies]
tokio = { version = "1.1", features = ["rt", "macros"] }
env_logger = "0.9.0"

[features]
default = ["auth"]
Expand Down
2 changes: 2 additions & 0 deletions examples/access_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ fn indent(indentation: usize, s: String) -> String {

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::init();

let client = utils::select_network()?;

let account_id = utils::input("Enter the Account ID whose keys we're listing: ")?.parse()?;
Expand Down
2 changes: 2 additions & 0 deletions examples/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ async fn authorized() -> Result<(), Box<dyn std::error::Error>> {

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::init();

unauthorized().await?;

authorized().await?;
Expand Down
2 changes: 2 additions & 0 deletions examples/contract_change_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ mod utils;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::init();

let client = JsonRpcClient::connect("https://rpc.testnet.near.org");

let signer_account_id = utils::input("Enter the signer Account ID: ")?.parse()?;
Expand Down
2 changes: 2 additions & 0 deletions examples/contract_change_method_commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ mod utils;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::init();

let client = JsonRpcClient::connect("https://rpc.testnet.near.org");

let signer_account_id = utils::input("Enter the signer Account ID: ")?.parse()?;
Expand Down
2 changes: 2 additions & 0 deletions examples/contract_view_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ mod utils;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::init();

let client = utils::select_network()?;

let contract_id: AccountId =
Expand Down
2 changes: 2 additions & 0 deletions examples/contract_view_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ pub struct AccountStatus {

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::init();

let client = JsonRpcClient::connect("https://rpc.testnet.near.org");

let account_id = utils::input("Enter the account to view: ")?;
Expand Down
2 changes: 2 additions & 0 deletions examples/create_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ async fn get_current_nonce(

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::init();

let client = utils::select_network()?;

let signer_account_id = loop {
Expand Down
2 changes: 2 additions & 0 deletions examples/query_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ pub fn specify_block_reference() -> std::io::Result<near_primitives::types::Bloc

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::init();

let client = utils::select_network()?;

// tolerate only 3 retries
Expand Down
2 changes: 2 additions & 0 deletions examples/view_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ mod utils;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::init();

let client = utils::select_network()?;

let account_id: AccountId = utils::input("Enter an Account ID to lookup: ")?.parse()?;
Expand Down
9 changes: 9 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ pub struct JsonRpcClientConnector {
impl JsonRpcClientConnector {
/// Return a JsonRpcClient that connects to the specified server.
pub fn connect<U: AsUrl>(&self, server_addr: U) -> JsonRpcClient {
log::info!("returned a new JSONRPC client handle");

JsonRpcClient {
inner: Arc::new(JsonRpcInnerClient {
server_addr: server_addr.to_string(),
Expand Down Expand Up @@ -207,6 +209,8 @@ impl JsonRpcClient {
))
})?;

log::info!("request payload: {:#}", request_payload);

let request_payload = serde_json::to_vec(&request_payload).map_err(|err| {
JsonRpcError::TransportError(RpcTransportError::SendError(
JsonRpcTransportSendError::PayloadSerializeError(err.into()),
Expand Down Expand Up @@ -250,6 +254,10 @@ impl JsonRpcClient {
})?;
let response_payload = serde_json::from_slice::<serde_json::Value>(&response_payload);

if let Ok(ref response_payload) = response_payload {
log::info!("response payload: {:#}", response_payload);
}

let response_message = near_jsonrpc_primitives::message::decoded_to_parsed(
response_payload.and_then(serde_json::from_value),
)
Expand Down Expand Up @@ -342,6 +350,7 @@ impl JsonRpcClient {
reqwest::header::HeaderValue::from_static("application/json"),
);

log::info!("initialized a new JSONRPC client connector");
JsonRpcClientConnector {
client: reqwest::Client::builder()
.default_headers(headers)
Expand Down