|
1 | 1 | const Web3 = require('web3'); |
2 | | -//const MyNFT = require('../build/contracts/MyNFT.json'); |
3 | | -require('dotenv').config(); // To use environment variables |
| 2 | +require('dotenv').config(); |
| 3 | +const log4js = require('log4js'); |
4 | 4 |
|
5 | | -const web3 = new Web3('http://localhost:8545'); // Connect to Ganache |
| 5 | +// Configure log4js |
| 6 | +log4js.configure({ |
| 7 | + appenders: { |
| 8 | + console: { type: 'console' }, // Logs to the console |
| 9 | + file: { type: 'file', filename: 'nft.log' }, // Logs to a file |
| 10 | + // Add more appenders as needed (e.g., for different log levels) |
| 11 | + }, |
| 12 | + categories: { |
| 13 | + default: { appenders: ['console', 'file'], level: 'info' }, // Default category |
| 14 | + // You can create different categories for different parts of your app |
| 15 | + // nft: { appenders: ['file'], level: 'debug' } For more detailed NFT logs |
| 16 | + }, |
| 17 | +}); |
6 | 18 |
|
7 | | -//const contractAddress = MyNFT.networks['5777'].address; // Replace with your network ID if different |
8 | | -//const contract = new web3.eth.Contract(MyNFT.abi, contractAddress); |
| 19 | +const logger = log4js.getLogger(); // Get the default logger |
| 20 | +// const logger = log4js.getLogger('nft'); // To use the 'nft' category |
9 | 21 |
|
10 | | -const account = process.env.ACCOUNT_ADDRESS; // Use environment variable for account |
11 | 22 |
|
12 | | -console.log(account) |
| 23 | +const web3 = new Web3('http://localhost:8545'); |
| 24 | + |
| 25 | +const account = process.env.ACCOUNT_ADDRESS; |
| 26 | + |
| 27 | +logger.info(`Account address: ${account}`); // Log the account address |
| 28 | + |
| 29 | +// Example usage of different log levels: |
| 30 | +// logger.debug('This is a debug message.'); |
| 31 | +// logger.info('This is an info message.'); |
| 32 | +// logger.warn('This is a warning message.'); |
| 33 | +// logger.error('This is an error message.'); |
| 34 | + |
| 35 | + |
| 36 | +// const contractAddress = MyNFT.networks['5777'].address; |
| 37 | +// const contract = new web3.eth.Contract(MyNFT.abi, contractAddress); |
13 | 38 |
|
14 | 39 | // async function mintNFT() { |
15 | 40 | // try { |
16 | 41 | // const tx = await contract.methods.mintNFT(account).send({ from: account }); |
17 | | -// console.log('Transaction:', tx); |
| 42 | +// logger.info('Transaction:', tx); // Log the transaction details |
18 | 43 | // } catch (error) { |
19 | | -// console.error('Error minting NFT:', error); |
| 44 | +// logger.error('Error minting NFT:', error); // Log the error |
20 | 45 | // } |
21 | 46 | // } |
22 | 47 |
|
23 | 48 | // mintNFT(); |
| 49 | + |
| 50 | + |
| 51 | +// Example of logging Web3 connection status (you might put this in a function): |
| 52 | +async function checkWeb3Connection() { |
| 53 | + try { |
| 54 | + const isConnected = await web3.eth.net.isListening(); |
| 55 | + if (isConnected) { |
| 56 | + logger.info('Successfully connected to Ganache.'); |
| 57 | + const networkId = await web3.eth.net.getId(); |
| 58 | + logger.info(`Network ID: ${networkId}`); |
| 59 | + |
| 60 | + } else { |
| 61 | + logger.error('Failed to connect to Ganache.'); |
| 62 | + } |
| 63 | + } catch (error) { |
| 64 | + logger.error('Error checking Web3 connection:', error); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +checkWeb3Connection(); |
0 commit comments