> 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="https://3988450783-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MWVjG_njKgBtvmnKaJh%2Fuploads%2FNDkkW9lMUE5f8KRU72P2%2Fimage.png?alt=media&amp;token=db92a777-a303-4eca-821b-56dd5dd17467" 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="https://3988450783-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MWVjG_njKgBtvmnKaJh%2Fuploads%2FGUiVSvl4fK1F4hlY6e2F%2Fimage.png?alt=media&amp;token=e1601d03-16af-4fbc-ab1c-c658734df37c" 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="https://3988450783-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MWVjG_njKgBtvmnKaJh%2Fuploads%2FAhMWes6IEUfZTXFzYEKo%2Fimage.png?alt=media&amp;token=01e04fdc-0661-40c0-b89d-e68d37be7ba5" alt=""><figcaption><p>console.logInt()</p></figcaption></figure>
