- Similar to Hardhat's console functions.
- You can use it in calls and transactions. It works with view functions, but not in pure ones.
- It always works, regardless of the call or transaction failing or being successful.
- To use it you need to import forge-std/console.sol.
- You can call console.log with up to 4 parameters in any order of following types:
- There's also the single parameter API for the types above, and additionally bytes, bytes1... up to bytes32:
console.logString(string memory s)
console.logBytes(bytes memory b)
- console.log implements the same formatting options that can be found in Hardhat's console.log.
console.log("Changing owner from %s to %s", currentOwner, newOwner)
- console.log is implemented in standard Solidity and it is compatible Anvil and Hardhat Networks.
- console.log calls can run in other networks, like mainnet, kovan, ropsten, etc. They do nothing in those networks, but do spend a minimal amount of gas.
console.log() method prints a formatted string using the first argument as a printf-like format string which can contain zero or more format specifiers. Each specifier is replaced with the converted value from the corresponding argument. Supported specifiers are:
%s: String will be used to convert all values to a human-readable string.
bytesvalues are converted to their
0xhex encoded values.
%d: Number will be used to convert all values to a human-readable string. This is identical to
%i: Works the same way as
%%: single percent sign ('%'). This does not consume an argument.
<string>The formatted string
If a specifier does not have a corresponding argument, it is not replaced:
console.log("%s:%s", "foo"); // Returns: "foo:%s"
Values that are not part of the format string are formatted using as a human-readable string representation.
If there are more arguments passed to the console.log() method than the number of specifiers, the extra arguments are concatenated to the returned string, separated by spaces:
console.log("%s:%s", "foo", "bar", "baz"); // Returns: "foo:bar baz"
If only one argument is passed to console.log(), it is returned as it is without any formatting:
console.log("%% %s"); // Returns: "%% %s"
The String format specifier (
%s) should be used in most cases unless specific functionality is needed from other format specifiers.