We can use console.log() to print out debugging info in Foundry just like in JavaScript. Import the following library:
import"forge-std/console.sol";
Use console.log() in contract:
// SPDX-License-Identifier: MITpragmasolidity ^0.8.17;import"forge-std/console.sol";contract Counter {uintpublic count;// Function to get the current countfunctionget() publicviewreturns (uint) {return count; }// Function to increment count by 1functioninc() public { console.log("Calling inc(). Now count = ", count); count +=1; }// Function to decrement count by 1functiondec() public {// This function will fail if count = 0 count -=1; }}
Verbosity at least -vv must be used to print out console.log() messages:
forgetest--match-pathtest/Counter.t.sol-vv
Of course you can also use console.log() in test file. For example, create a test file Console.t.sol: