> For the complete documentation index, see [llms.txt](https://ret2basic.gitbook.io/ctfnote/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ret2basic.gitbook.io/ctfnote/web3-security-research/foundry/console-log.md).

# Console Log

We can use `console.log()` to print out debugging info in Foundry just like in JavaScript. Import the following library:

```solidity
import "forge-std/console.sol";
```

Use `console.log()` in contract:

```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;

import "forge-std/console.sol";

contract Counter {
    uint public count;

    // Function to get the current count
    function get() public view returns (uint) {
        return count;
    }

    // Function to increment count by 1
    function inc() public {
        console.log("Calling inc(). Now count = ", count);
        count += 1;
    }

    // Function to decrement count by 1
    function dec() public {
        // This function will fail if count = 0
        count -= 1;
    }
}
```

Verbosity at least `-vv` must be used to print out `console.log()` messages:

```bash
forge test --match-path test/Counter.t.sol -vv
```

<figure><img src="/files/jW30lk31O7YbqBcAA9X4" alt=""><figcaption><p>console.log() in contract</p></figcaption></figure>

Of course you can also use `console.log()` in test file. For example, create a test file `Console.t.sol`:

```solidity
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

import "forge-std/Test.sol";

contract ConsoleTest is Test {
    function testLogSomething() view public {
        console.log("Log something here", 123);
    }
}
```

No need to import `forge-std/console.sol` because it is included in `forge-std/Test.sol`, which is already imported.

Run test with `-vv` verbosity:

```bash
forge test --match-path test/Console.t.sol -vv
```

<figure><img src="/files/T92PjJeyGt1ZmsKT8QH9" alt=""><figcaption><p>console.log() in test file</p></figcaption></figure>

One caveat here: `console.log()` can not log `int`. There is another function named `console.logInt()` for doing this job:

```solidity
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

import "forge-std/Test.sol";

contract ConsoleTest is Test {
    function testLogSomething() view public {
        console.log("Log something here", 123);

        int x = -1;
        console.logInt(x);
    }
}
```

<figure><img src="/files/hokQMAXaphDgBTEz1F2r" alt=""><figcaption><p>console.logInt()</p></figcaption></figure>
