Debug Smart Contracts Faster with Hardhat Network's Built-in Console.log
Debug smart contracts faster using Hardhat Network's built-in console.log() functionality. This guide shows how to add logging to Solidity code, verify it works in tests, and remove it before production deployment.
08 Nov 2025, 07:43 UTC

The Problem: Smart Contract Debugging Without Console Output
When testing smart contracts, you need to see what's happening inside your Solidity code. Traditional debugging methods like external logging or trial-and-error are slow and error-prone. Hardhat Network provides a built-in solution: console.log() statements that work directly in your Solidity code during testing.
Prerequisites for Using Console.log in Hardhat
- Hardhat project initialized with
npx hardhat init - Hardhat version 2.6.0 or later (console.log support added in 2.6.0)
- Tests run via
npx hardhat testornpx hardhat nodewith separate scripts
Step-by-Step: Adding Console Logging to Your Tests
- Add console.log to your Solidity contract:
// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; contract Counter { uint256 public count; function increment() public { count += 1; console.log("Count incremented to:", count); } } - Write a test that calls the function:
const { expect } = require("chai"); const { ethers } = require("hardhat"); describe("Counter", function () { it("Should log count after increment", async function () { const Counter = await ethers.getContractFactory("Counter"); const counter = await Counter.deploy(); await counter.deployed(); await counter.increment(); expect(await counter.count()).to.equal(1); }); }); - Run the test to see console output:
npx hardhat testExpected output will include:
Count incremented to: 1
Understanding Console Output Format
Hardhat formats console.log output as:
[timestamp] [contract_name] [function_name] [log_message]
For example:
[12/03/2026 04:34:47] [Counter] [increment] Count incremented to: 1
Advanced Console Logging Patterns
You can log multiple values and use printf-style formatting:
function transfer(address to, uint256 amount) public {
console.log("Transferring %d tokens to %s", amount, to);
balances[msg.sender] -= amount;
balances[to] += amount;
console.log("New balance for %s: %d", to, balances[to]);
}
Limitations and Important Considerations
| Limitation | Details |
|---|---|
| Not available in production | Console.log statements are stripped during production deployments |
| Gas cost during testing | Console.log adds minimal gas overhead but should be removed for gas optimization |
| Only in Hardhat Network | Works only in Hardhat's local network, not on live Ethereum networks |
| Supported types | Works with uint256, address, bool, string, and bytes types |
Verifying Console Logging Works
To confirm console.log is functioning properly:
- Run your test with verbose output:
npx hardhat test --verbose - Check that console output appears in test results
- Verify the log messages match your expected values
Removing Console.log Before Production
Before deploying to production, remove all console.log statements. You can:
- Manually delete them from your contracts
- Use a Solidity formatter or linter to identify them
- Configure your build process to fail if console.log is present
Alternative Debugging Approaches
If console.log isn't sufficient, consider:
- Hardhat Console: Interactive debugging with
npx hardhat console - Event logging: Emit events in your contracts and listen for them in tests
- Debugger: Use Hardhat's built-in debugger with
npx hardhat test --no-compileand breakpoints
Key Takeaway
Hardhat Network's built-in console.log functionality provides immediate visibility into your smart contract execution during testing. By adding simple logging statements to your Solidity code, you can quickly diagnose issues without complex external tooling. Remember to remove these statements before production deployment, as they add unnecessary overhead and are not supported on live networks.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.