When learning how to become a smart contract developer, debugging in smart contracts can be challenging, which is why today popular smart contract development frameworks like Foundry and Hardhat support writing console.log
statements in your code so you can see the values of different variables.
In this article, we will teach how to console log in Solidity, print your variable values and what it is used for.
If you're new to web3 development, you should definitely checkout our list of top solidity smart contract development courses to get yourself up and running.
That said, let's jump straight into it and learn how to console.log your variables in Solidity.
A console.log
statement is an example of a debugging or print
statement, where the value of a variable is entered into Stdout (standard output, aka the terminal).
We can see what print statements look like in other languages.
Assuming we have a file named my_script.py
with the following contents:
Running it in the terminal, we’d see the number 7 logged to the terminal.
Assuming we have a file named my_script.js
with the following contents:
Running it in the terminal, we will see, again, the number 7 logged to the terminal.
This can help us “see” the status of different variables in our codebase. However, in the world of EVM, there isn’t technically a terminal since all our programs are designed to be run in a transaction. Of course, we will often want to debug our transactions, so how do we do this?
Two of the most popular ways are:
Before we could console.log
in Solidity smart contracts, a popular way to "see" the value of a variable would be to emit it as a log:
Then, look at the logs emitted from your transaction. This method was cumbersome because you’d have to define a new event any time you wanted to examine a variable's value. Projects like Hardhat and Foundry added a library called console.sol (or console2.sol
), making console log in Solidity much easier. Let's how.
To console log your Solidity smart contract variables using Foundry, you can (1) import the console.log
library and (2) use the console.log function to print any object, as follow:
Now, if we were to deploy MyContract
in Foundry, we’d be able to see a log being emitted with the value of 7
.
For example, if this was your test:
And you were to run a test with -vvv
:
We’d see the number 7 logged to the terminal.
Logs:
7
Using console.log
in Foundry will work for all primitive types like uint256
and bool
, but it has specific methods for special types of solidity. For example, if you want to console.log a bytes object, you’d have to use:
You can see a list of supported methods in the Foundry documentation.
You can also use console log
to debug your Solidity codebase also when working with Hardhat:
If you were to deploy this contract in a script, you’d see the value 7
output to the terminal.
You’d see something like:
7
In the terminal.
Debugging is a tricky topic, and in this article, we covered how to console.log variables in Solidity smart contract using Foundry and Hardhat, which are the most popular methods.
If you want to learn more advanced debugging techniques, you can view some example repositories from the Cyfrin Updraft curriculum. We teach more advanced ways to debug Solidity smart contracts, including walking opcode-by-opcode through your transactions. Happy building!