To view error and console logs for your Node.js application running on CyberPanel, you’ll need to set up logging in your application and ensure that it outputs to a log file or the console. Here’s how you can do that:
1. Modify Your Node.js Application for Logging
You can use the built-in console
methods to log errors or messages in your Node.js application. For example:
const mongoose = require('mongoose');
mongoose.connect('your_mongodb_uri', { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log('MongoDB connected'))
.catch(err => console.error('MongoDB connection error:', err));
2. Using a Logging Library
Consider using a logging library like winston
or morgan
to manage your logs more effectively. Here’s how to set up winston
:
- Install
winston
:
npm install winston
- Set up logging in your application:
const winston = require('winston');
const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
transports: [
new winston.transports.File({ filename: 'error.log', level: 'error' }),
new winston.transports.File({ filename: 'combined.log' })
]
});
// Log messages
logger.info('This is an info message');
logger.error('This is an error message');
- Ensure that the log files are writable. You may need to set the correct permissions for the directory where your logs will be saved.
3. Check CyberPanel Logs
CyberPanel maintains its own logs. You can find them in:
- Access logs:
/usr/local/lsws/logs/access.log
- Error logs:
/usr/local/lsws/logs/error.log
You can view these logs using:
tail -f /usr/local/lsws/logs/error.log
4. View Application Logs
After setting up logging in your Node.js app, you can view the log files created by winston
or the console outputs via SSH:
tail -f error.log
tail -f combined.log
5. Consider Using PM2
If you’re not already using it, consider running your Node.js app with PM2, which will help you manage your application and provides built-in logging:
- Install PM2:
npm install pm2 -g
- Start your application with PM2:
pm2 start app.js
- View logs:
pm2 logs
Final Words
- Use
console
or a logging library like winston
in your Node.js application to log errors.
- Check CyberPanel’s built-in logs for additional information.
- Optionally use PM2 for better management and logging of your Node.js application.
By implementing these steps, you should be able to capture and view the logs necessary to troubleshoot your application.