Solidity learning - how to print Log logs in smart contracts

In the process of writing contracts, some mistakes are often encountered. At this time, if you want to view some data during the running of the contract, you can use the following methods:

Create an Event in the contract and name it Log

Call the event emit Log(...) where you want to print the log, and you can view the data in the running process

As shown below:

Click deploy, and you can view the printed data on the console

Can also be used in methods ( however: cannot use view decoration )

Call consoleLog to print out the log

 

The following is the complete code of the demo

// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0; // 版本号,使用大于0.8.0的版本编译器编译

contract TestLog {

    // 创建一个Event,起名为Log
    event Log(string);
    event Log(uint256);

    constructor() {
        emit Log(123456);
        emit Log("abcdef");
    }

    function consoleLog() public virtual returns(bool) {
        emit Log(789);
        emit Log("qaz");

        return true;
    }
}

Guess you like

Origin blog.csdn.net/Meta_World/article/details/124701520