Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I enable node.js application logs on from cpanel?

I have a node.js application that sends email using nodemailer. It is working fine from localhost but when I try it from server I can't receive any mail.

Since, other activities of application are working fine means I assumed nodemailer is installed properly on server. I tried on both ways using host as my server and using gmail as on of the services. Here, is what I tried last time. This also works fine on local host. But, I don't get any response when I put this on server.

I have nodemailer's configuration as:


const output = `
    <h3>Limit Details</h3>
    <p>Admin has added ${limit} quota(s) to ${username}.</p>
  `;

  // create reusable transporter object using the default SMTP transport
  let transporter = nodemailer.createTransport({
    service: 'gmail',
    port: 25,
    secure: false, // true for 465, false for other ports
    auth: {
        user: '<gmail>', 
        pass: '<password>' 
    }
  });

  // setup email data with unicode symbols
  let mailOptions = {
      from: '"Sender" <sender gmail>', // sender address
      to: '<receiver gmail>', // list of receivers
      subject: 'Quota added information.', // Subject line
      html: output // html body
  };

  // send mail with defined transport object
  transporter.sendMail(mailOptions, (error, info) => {
      if (error) {
          return console.log(error);
      }
      console.log('Message sent: %s', info.messageId);   
      console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));
  });

For testing, I have consoled messageId and Preview Url So, from where I can view this console message in cpanel? How can I view such consoled stuffs from my application in cpanel??

like image 867
John Avatar asked Mar 01 '26 09:03

John


1 Answers

Unfortunately there is no easy way to access node's logs on a cpanel server as far as I know. What I usually do is I set up log4js to write logs to a file:

const log4js = require('log4js');

log4js.configure({
  appenders: { everything: { type: 'file', filename: 'logs.log' } },
  categories: { default: { appenders: ['everything'], level: 'ALL' } }
});

const logger = log4js.getLogger();

Then you can make logs with:

logger.debug('log message');

You can also serve the log file with the server:

app.get('/log', (req, res) => {
  res.sendFile(path.join(__dirname + '/logs.log'));
});
like image 166
Balint Avatar answered Mar 03 '26 01:03

Balint



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!