What's the Solidity statement to print data to the console?

  • Sometimes it would be really helpful to be able to see string representations of variable values in the Geth console, like of argument values in functions. Can I somehow print them to the Geth console?

  • You are most likely looking for Events. Not only do they help with debugging, but they are useful in normal production code.

    Events are declared like functions, like so:

    event VoteCast(address voter, uint votes, bool inFavor);

    Then somewhere (for example, in a vote counting function):

    function vote(bool inFavor) {
        var votes = shares[msg.sender];
        // ...
        emit VoteCast(msg.sender, votes, inFavor);

    In javascript, a contract object has an event method, which can be used to read events when they happen. In fact, it can read events that happened in the past.

    var voteCast = someContract.voteCast();
    voteCast.watch(function(err, result) {/* some callback */});
    // Alternately, to get the events all at once.
    voteCast.get(function(err, result) /* some other callback* /)

    Events have a number of quirks, which are too many to get into detail here. Nonetheless, they're an integral part of high-level dapp work.

  • Print doesn't exist in Solidity. Use the logX statement as indicated in the manual instead.

    The log statements are really low level. One is better off using specific events.

    Agree they're low-level but can be useful especially for temporary debugging to quickly see a value in IDEs that support log statements and pretty-print their parameters.

  • Since you mentioned geth console, you can try console.log() for debugging purposes:

    console.log ("Your debug message" + debug.object);

    This is possible because geth supports web3.js

    this is js way , not solidity

    @MaxLXJ `print` is not supported by Solidity as it supports Events-driven paradigm, but since it leverages JavaScript syntax it is quite handy to use `console.log` for debug purposes. But this is not recommended for Production/Live usage as Solidity written contract will be distributed across nodes, and using Events will be ideal.

License under CC-BY-SA with attribution

Content dated before 7/24/2021 11:53 AM