NodeJS ExpressJS - how to log errors for the production server?
Asked Answered
T

3

17

How can I log any errors from ExpressJS apps into a file?

I know I can log a Slim framework easily with monolog:

$app->get('/tickets', function (Request $request, Response $response) {
    $this->logger->addInfo("Something interesting happened");

    $mapper = new Simon\TicketMapper($this->db);
    $tickets = $mapper->getTickets();

    $response->getBody()->write(var_export($tickets, true));
    return $response;
});

In Express, I usually log the error to console for development:

Model.findOne(options, function(err, doc) {

    var output = {};
    if (err) {
        console.log("Error retrieving doc: " + err);
    }

But in my production server, I do need a helpful log file when the app goes wrong.

Any ideas?

In Express's app.js, it has this:

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {

  app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
        message: err.message,
        error: err
    });
  });

}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
        message: err.message,
        error: {}
    });
});

But how do I turn the production's one on? Is it the same as monolog that I need?

Tumer answered 27/3, 2017 at 18:47 Comment(1)
Should be as simple as outputting the error to a file instead of the console so that you can reference this file. #8394136Styria
S
13

winston is a popular library for logging.

You can transport all logs to one file or keep different files for error, debug and info logs.

Example:

  var winston = require('winston');

  var logger = new winston.Logger({
    level: 'error',
    transports: [
      new (winston.transports.File)({ filename: 'error.log' })
    ]
  });

In the code:

logger.log('error', 'test error message %s', 'my string');

You can also use winston daily rotate file with winston to rotate your log files based on size or date.

Superficies answered 27/3, 2017 at 18:57 Comment(0)
W
5

You can either add a logging library like morgan or if you have your node app running under a process manager, like pm2, then pm2 will keep track of the logging for you. I haven't used pm2 a ton, but from what I understand you could keep you console.log statements the same and pm2 will save all the output from your app into log files for you. You can read more about pm2's logging capabilities here.

Weighting answered 27/3, 2017 at 18:53 Comment(1)
I use PM2. But I prefer having a logger like monolog that I can manage it manually inside my code.Tumer
K
0

If you're fine with logging the errors to your database instead of a log file, check out npm library grackle_tracking https://www.npmjs.com/package/grackle_tracking It allows you to log to the console, catch & log caught and uncaught errors to the database. It also has the ability to track all your traffic.

Krasnoyarsk answered 20/9, 2022 at 17:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.