DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Exploring Intercooler.js: Simplify AJAX With HTML Attributes
  • Bridging the Gap: Better Token Standards for Cross-Chain Assets
  • Deploying Smart Contract on Ethereum Blockchain
  • How To Use Ethereum Events Properly: An Solidity Dapp Tutorial

Trending

  • DZone's Article Submission Guidelines
  • How to Submit a Post to DZone
  • Enforcing Architecture With ArchUnit in Java
  • Tired of Spring Overhead? Try Dropwizard for Your Next Java Microservice
  1. DZone
  2. Coding
  3. JavaScript
  4. Easy Smart Contract Debugging With Truffle’s Console.log

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. Let's look at how.

By 
Michael Bogan user avatar
Michael Bogan
DZone Core CORE ·
Jan. 26, 23 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
5.8K Views

Join the DZone community and get the full member experience.

Join For Free

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");


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 and then reinstall it. For the commands used in this post, we’ll use NPM as our package manager.

 $ npm install -g truffle


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
  }


  • 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);
  }
}


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


An even better way to debug this could be to add in the balances[msg.sender] to 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.

Smart contract Solidity Debug (command) JavaScript Event

Published at DZone with permission of Michael Bogan. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Exploring Intercooler.js: Simplify AJAX With HTML Attributes
  • Bridging the Gap: Better Token Standards for Cross-Chain Assets
  • Deploying Smart Contract on Ethereum Blockchain
  • How To Use Ethereum Events Properly: An Solidity Dapp Tutorial

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!