/truffle-logger-example

Console.log example for Solidity inside Truffle projects

Primary LanguageJavaScript

Console.log for Solidity with Truffle

This is an experimental feature for Truffle that introduces a console.log-equivalent in your Solidity smart contracts.

Please leave your feedback in this issue: trufflesuite#3

Example

Simply import the Console library:

import "truffle/Console.sol";

And just log out your variables with Console.log like this!

Console.log(myBool);
Console.log(myInt);
Console.log(myUint);
Console.log(myString);
Console.log(myBytes32);
Console.log(myAddress);

You can also pass in a label for your variable so it's easier to track:

Console.log("This is myBool", myBool);
Console.log("This is myInt", myInt);
Console.log("This is myUint", myUint);
Console.log("This is myString", myString);
Console.log("This is myBytes32", myBytes32);
Console.log("This is myAddress", myAddress);

Running this example project

Clone this project, and then run the following steps:

  1. Spawn a Truffle develop console with the Console.log-enhanced version of Truffle:

    npx truffle@truffleLogger develop
  2. Run tests at the prompt:

    truffle(develop)> test
    
  3. Note the output with respect to what is logged in the smart contract.

For reference, take a look at the following files:

Running inside an existing project

This is what you need to do to use the logger inside your existing Truffle projects:

  1. Import the Console library in your contracts:

    import "truffle/Console.sol";
  2. Call the logging function:

    Console.log(myVariable);
  3. Spawn a Truffle develop console with the Console.log-enhanced version of Truffle:

    npx truffle@truffleLogger develop
  4. Run tests at the prompt:

    truffle(develop)> test