Back to blogs
Written by
Patrick Collins
Published on
April 30, 2024

2 Ways to Console Log in Solidity Smart Contracts

Learn how to console log variables in Solidity smart contracts using Foundry and Hardhat and debug your smart contracts without using events.

Table of Contents

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.

What is console.log 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.

Example: Console.log using Python

my_variable = 7
print(my_variable)

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.

Example: Console.log in Javascript / nodejs

Assuming we have a file named my_script.js with the following contents:

const my_variable = 7
console.log(my_variable)

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:

  1. Step through debuggers (not covered in this article but you can learn how to debug smart contracts on Cyfrin Updraft)
  2. console.logging

How to read variables in an EVM transaction before console logging

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:

event Log(uint256);
uint256 my_variable = 7
emit Log(my_variable);

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.

How to Console log smart contract variables using Foundry

Image of how to console log in solidity using foundry

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:

pragma solidity ^0.8.0;
import {console} from "forge-std/console.sol";

contract MyContract{
	constructor() {
	  uin256 my_variable = 7;
	  console.log(my_variable);
	}
}

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:

pragma solidity ^0.8.0;
import {Test} from "forge-std/Test.sol";
import {MyContrat} from "../src/MyContract.sol";

contract MyTest is Test{
  function setUp() public {
    MyContract myContract = new MyContract();
  }
}

And you were to run a test with -vvv :

forge test -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:

console.logBytes(bytes memory b)


You can see a list of supported methods in the Foundry documentation.

How to Console log in Solidity using Hardhat

Image of how to console log in solidity using hardhat

You can also use console log to debug your Solidity codebase also when working with Hardhat:


pragma solidity ^0.8.0;

import {console} from "hardhat/console.sol";

contract MyContract {
  constructor() {
      uint256 my_value = 7;
      console.log(my_value);
  }
}

If you were to deploy this contract in a script, you’d see the value 7 output to the terminal.

npx hardhat test

You’d see something like:

7

In the terminal.

Conclusion: How to console log in Solidity

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!

Secure your protocol today

Join some of the biggest protocols and companies in creating a better internet. Our security researchers will help you throughout the whole process.
Stay on the bleeding edge of security
Carefully crafted, short smart contract security tips and news freshly delivered every week.