How to save logs of Node.js running in CyberPanel?

I am currently running a Cyber Panel v2.1 + Ubuntu + Node v14.4 on an EC2 T2.Small Instance. Using the context menu in the Open Lite Speed Interface, I run a Node.JS Website. However, their are issues with its functionality, particularly when I try to require the Mongoose or MongoDB Package.

As of now, I don’t have a way to see the error logs or console logs that are generating, only the access logs. Is their a way I can configure something on either CyberPanel or Node.Js that I can get the console logs & the access logs of the project so that I can fix the issue?

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:

  1. Install winston:
npm install winston
  1. 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');
  1. 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:

  1. Install PM2:
npm install pm2 -g
  1. Start your application with PM2:
pm2 start app.js
  1. View logs:
pm2 logs

Final Words

  1. Use console or a logging library like winston in your Node.js application to log errors.
  2. Check CyberPanel’s built-in logs for additional information.
  3. 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.