DEV Community

Michael Bogan
Michael Bogan

Posted on

Easy Smart Contract Debugging with Truffle's Console.log

If you’re a Solidity developer, you’ll be excited to hear that Truffle now supports console logging in Solidity smart contracts. While Truffle has long been a leader in smart contract development tooling—providing an easy-to-use environment for creating, testing, and debugging smart contracts—a directly integrated console.log was a feature it still needed.

But no more! Developers can now easily log messages and debug their smart contracts, all within the familiar Truffle (Ganache) environment. Let’s look at how.

What Is Console.log?

Console.log is a very popular feature in JavaScript and is widely used by developers to easily output logging messages and extract details directly from code. In the context of web3 and smart contract development, console.log plays a similar role, allowing developers to print out Solidity variables and other information from their smart contracts.

For example, you can use console.log to display the value of a variable or the output of a function call within your smart contract. This can be extremely useful when debugging or testing your smart contract.

console.log("Console Logging: The Smart Contract Developer's Best Friend");
Enter fullscreen mode Exit fullscreen mode

How To Use Console Logging in Truffle

Making use of console.log is quite straightforward. First, you’ll have to ensure you have an up-to-date Truffle version running on your computer. If you have any issues, you might want to uninstall the package entirely then reinstall it. For the commands used in this post, we’ll use NPM as our package manager.

$ npm install -g truffle
Enter fullscreen mode Exit fullscreen mode

After a successful installation, I suggest that you modify the truffle configuration file (i.e. truffle-config.js) as follows:

module.exports = {
  . . .
  solidityLog: {
    displayPrefix: ' :', // defaults to ""
    preventConsoleLogMigration: true, // defaults to false
  }
Enter fullscreen mode Exit fullscreen mode
  • displayPrefix: decorates the outputs from console.log to differentiate it from other contents displayed by the CLI.
  • preventConsoleLogMigration: screens contract deployments from going through when on a test or mainnet. You can opt out of this if you wish to deploy your contract with the console.log included. However, if you choose to do this, keep in mind that console.log has unpredictable behavior when it comes to gas usage.

Now you’re ready to try it out! Import the contract.sol contract into your Solidity code as usual. Now you’re ready to use the console.log() command as you would in JavaScript.

This includes using string substitutions like %s and %f.

pragma solidity ^0.8.9;
import "truffle/console.sol";
contract BookStore {
  //...

  function transfer(address to, uint256 amount) external {
    console.log("Transferring %s tokens to %s", amount, to);

    require(balances[msg.sender] >= amount, "Not enough tokens");

    balances[msg.sender] -= amount;
    balances[to] += amount;

    emit Transfer(amount, to, msg.sender);
  }
}
Enter fullscreen mode Exit fullscreen mode

The above transfer function shows console.log in action. Imagine a call to the transfer function failing with the “Not enough tokens” error. The console.log line, in this case, will show the number of tokens the call is trying to transfer. This allows the developer to see the address and amount of tokens being transferred. The message will look like this.

...
Transferring 10 tokens to 0x377bbcae5327695b32a1784e0e13bedc8e078c9c
Enter fullscreen mode Exit fullscreen mode

An even better way to debug this could be to add in the balances[msg.sender] into the console.log statement or print it out on a separate line. That way, the sender’s balance is visible in the console, too. You get the point!

You can also leave logs in test and mainnets; this way, you’ll have a nice way to observe your smart contract. And it's worth mentioning that tools like Tenderly will integrate the scrapping of logs, which can be useful when debugging and testing smart contracts in a production environment.

Finally, when using console logging, it's important to follow all the good usage rules you already know, such as using clear and descriptive log messages. This will make it easier to understand the output and identify any issues that may arise.

Other Debugging Tools in Truffle

While console logging is a powerful tool for debugging smart contracts, keep in mind that Truffle offers other debugging tools as well. Truffle has a powerful built-in debugger CLI tool that can be used to step through the execution of a smart contract and inspect the state of variables at different points in the execution. Additionally, events are a nice way to log messages and track the behavior of a smart contract.

That said, it's worth noting that using the debugger for something as simple as variable output can be overkill. Similarly, event logging only works when the transaction succeeds, which can be a limitation in certain situations.

The bottom line is that the console.log feature—in combination with the other debugging tools in Truffle—can provide a better developer experience, thanks to its simplicity and ease of use. It gives developers the ability to quickly and easily log messages and monitor the behavior of their smart contracts, while the other debugging tools can be used for more advanced debugging and troubleshooting.

Try It Out

Truffle's new console logging feature is a valuable addition to smart contract development. It’s easy to use and can streamline the debugging and testing process. The ability to log messages and track the behavior of smart contracts in real time can reduce inefficiencies and headaches. It’s a great tool to have in your toolbox.

Oldest comments (0)