> 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>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://ret2basic.gitbook.io/ctfnote/web3-security-research/foundry/console-log.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
