Hardhat provides a powerful console that allows developers to interact with their smart contracts and the Hardhat network. Customizing the console output can enhance the developer experience by making it easier to read and understand the logs and messages generated during development. In this article, we'll explore how to customize the Hardhat console output.

1. Using Custom Log Messages

One of the simplest ways to customize console output is by using custom log messages in your scripts. You can use the console.log function to print messages to the console, which can help you track the flow of your script and the values of variables.

const { ethers } = require("hardhat");

async function main() {
const MyContract = await ethers.getContractFactory("MyContract");
const myContract = await MyContract.deploy();
await myContract.deployed();

// Custom log message
console.log("MyContract deployed to:", myContract.address);
}

main().catch((error) => {
console.error("Error deploying contract:", error);
process.exit(1);
});

2. Formatting Console Output

You can also format your console output to improve readability. This can be done using template literals and string interpolation.

const { ethers } = require("hardhat");

async function main() {
const MyContract = await ethers.getContractFactory("MyContract");
const myContract = await MyContract.deploy();
await myContract.deployed();

// Formatted console output
console.log(`Contract deployed successfully! \nAddress: ${myContract.address}`);
}

main().catch((error) => {
console.error("Deployment failed:", error);
process.exit(1);
});

3. Using Colors in Console Output

To make your console output more visually appealing, you can use colors. The chalk library is a popular choice for adding colors to console messages. You can install it using npm:

npm install chalk

Here’s how to use chalk to customize your console output:

const { ethers } = require("hardhat");
const chalk = require("chalk");

async function main() {
const MyContract = await ethers.getContractFactory("MyContract");
const myContract = await MyContract.deploy();
await myContract.deployed();

// Colorful console output
console.log(chalk.green("Contract deployed successfully!"));
console.log(chalk.blue(`Address: ${myContract.address}`));
}

main().catch((error) => {
console.error(chalk.red("Deployment failed:"), error);
process.exit(1);
});

4. Customizing Hardhat's Built-in Console

Hardhat allows you to customize its built-in console output by modifying the configuration file. You can set the logging level and format by updating your hardhat.config.js file:

require("@nomiclabs/hardhat-waffle");

module.exports = {
solidity: "0.8.4",
networks: {
hardhat: {
logging: {
level: "debug", // Options: "debug", "info", "warn", "error"
},
},
},
};

5. Conclusion

Customizing the Hardhat console output can significantly enhance your development experience by making it easier to read logs and track your application's behavior. By using custom log messages, formatting output, adding colors, and configuring the built-in console, you can create a more informative and engaging development environment.