# How can I log data to the console from my contract?

**URL:** <https://forum.fuel.network/t/how-can-i-log-data-to-the-console-from-my-contract/1508>\
**Category:** Sway\
**Created:** [January 17, 2023, 8:52pm UTC](https://forum.fuel.network/t/how-can-i-log-data-to-the-console-from-my-contract/1508 "2023-01-17T20:52:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sarah](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/sarah/32/13_2.png) [@sarah](https://forum.fuel.network/u/sarah)\
**Post date:** [January 17, 2023, 8:52pm UTC](https://forum.fuel.network/t/how-can-i-log-data-to-the-console-from-my-contract/1508/1 "2023-01-17T20:52:43Z")

</div>

What is the equivalent of console.log() in Sway?

There are two main ways to be able to log data in a contract to the console.

1. You can return the data in the function, and print the value returned to the console.

2. Use the [`log` method](https://fuellabs.github.io/sway/master/book/basics/comments_and_logging.html) from the standard library. When a function gets called that uses the `log` method, a `log` or `logData` receipt will be appended to the transaction receipts array. Each call to `log` appends a `receipt` to the list of receipts.

```rust
fn test_function(){
	let x: u64 = 64;
	log(x);
	
	let boolean = true;
	log(boolean);
	
	let string = "fuel";
	log(string);
}

```

In a Rust test you can print the log values either from the receipts array, or with the `get_logs` or `get_logs_with_type` methods. Due to possible performance hits, it is not recommended to use `get_logs()` outside of a debugging scenario.

```rust
let response = instance.methods().test_function().call().await.unwrap();

println!("FIRST LOG: {:?}", response.receipts[1].ra());

let log_string = response.get_logs_with_type::<SizedAsciiString<4>>();
println!("LOG STRING: {:?}", log_string);

println!("GET LOGS: {:?}", response.get_logs());

```

This will print the following to the console:

```auto
FIRST LOG: Some(64)
LOG STRING: Ok([SizedAsciiString { data: "fuel" }])
GET LOGS: Ok(["64", "true", "SizedAsciiString { data: \"fuel\" }"]))

```

You can learn more about working with logs in the Rust SDK here: [Logs - The Fuel Rust SDK](https://fuellabs.github.io/fuels-rs/master/calling-contracts/logs.html)

---

<div class="post-metadata">

**Author:** ![diyahir](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/diyahir/32/1075_2.png) [@diyahir](https://forum.fuel.network/u/diyahir)\
**Post date:** [March 13, 2023, 9:41pm UTC](https://forum.fuel.network/t/how-can-i-log-data-to-the-console-from-my-contract/1508/2 "2023-03-13T21:41:58Z")

</div>

Something like this can also be useful to print everything:

```auto
response.receipts.iter().for_each(|r| match r.ra() {
        Some(r) => println!("{:?}", r),
        _ => (),
    });

```

---

<div class="post-metadata">

**Author:** ![sarah](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/sarah/32/13_2.png) [@sarah](https://forum.fuel.network/u/sarah)\
**Post date:** [May 30, 2023, 2:31pm UTC](https://forum.fuel.network/t/how-can-i-log-data-to-the-console-from-my-contract/1508/3 "2023-05-30T14:31:54Z")

</div>


