Debugging Solidity Contracts with Hardhat's console.log
Learn how to use Hardhat's built‑in console.log to debug Solidity contracts directly from the terminal, with a minimal setup, example contract and test, and notes on gas overhead and network limits.
13 Jan 2026, 18:11 UTC

Why you need a quick way to see contract values
When a Solidity function behaves unexpectedly during testing, the usual options are to add events, inspect transaction receipts, or rely on external debuggers. All of these require extra steps or external tooling. Hardhat provides a built‑in console.log that works like JavaScript’s console and prints directly to the terminal when a transaction runs on the Hardhat network. This lets you inspect variables instantly without changing your contract’s ABI or deploying to a testnet.
Setting up a minimal Hardhat project
- Open a terminal in an empty folder and run:
# Initialize a Node.js project (no special permissions needed) npm init -y # Install Hardhat as a dev dependency npm install --save-dev hardhat # Start the interactive setup and choose “Create a basic sample project” npx hardhat - Accept the defaults; Hardhat will create
hardhat.config.js,contracts/,test/, andscripts/folders.
Adding console.log to a contract
Create a simple contract that logs a value when a function is called.
- Add a new file
contracts/Logger.sol:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "hardhat/console.sol";
contract Logger {
uint256 public counter;
function increment() public {
counter += 1;
// Log the new counter value
console.log("Counter after increment:", counter);
}
}
- Write a test that calls
incrementand verify the log appears in the test output. Createtest/Logger.test.js:
const { expect } = require("chai");
describe("Logger contract", function () {
it("should log the counter value", async function () {
const Logger = await ethers.getContractFactory("Logger");
const logger = await Logger.deploy();
await logger.deployed();
// The transaction will trigger console.log inside increment
await logger.increment();
// Optional: check that the state changed as expected
expect(await logger.counter).to.equal(1);
});
});
Running the test and observing the log
From the project root, execute:
npx hardhat test
You will see the usual Mocha test output followed by a line similar to:
Logger contract ✔ should log the counter value (xx ms) Counter after increment: 1The line beginning with “Counter after increment:” comes from the
console.logcall inside the contract. No additional configuration is required; Hardhat’s network intercepts the opcode and forwards the data to the console.Trade‑offs and limitations
- Gas overhead: Each
console.logadds roughly 200‑300 gas because Hardhat inserts extra opcodes to capture the data. In tight loops this can become significant and may cause out‑of‑gas errors. - Network scope: The feature only works when contracts are executed on Hardhat’s built‑in network (e.g., during
hardhat test,hardhat node, or scripts that explicitly useethers.providerpointing tohardhat). If you deploy to a live network or a fork without enabling the Hardhat network provider, the logs are stripped out. - Type support: Primitive types (uint, int, address, bool, string) log directly. For structs, arrays, or mappings you must manually extract the fields you want to log.
To verify that the logs are indeed coming from Hardhat and not from an external source, run the same test against a different network (e.g., npx hardhat test --network localhost pointing to a regular Ethereum node). The console.log line will disappear, confirming the behavior is network‑specific.
Actionable next steps
- Add
console.logto any contract function where you need quick visibility during development. - Keep an eye on gas usage; remove or conditionally compile out logs before deploying to production or testnets.
- If you need to log complex data, create a helper function that returns a string representation (e.g., using
abi.encodePacked) and log that string. - Remember that logs are stripped in production builds, so they are safe to leave in the source as long as you are aware of the gas cost.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.